You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.markdown
+30-24
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,13 @@
1
-
== Howto ==
2
-
=== What kinds of queries are supported? ===
1
+
## What is Cache Money ##
3
2
4
-
In general, any query involving equality (=) and conjunction (AND) is supported by `Cache Money`. Disjunction (OR) and inequality (!=, <=, etc.) are not typically materialized in a hash table style index and are unsupported at this time.
3
+
Cache Money is a write-through and read-through caching library for ActiveRecord.
4
+
5
+
Read-Through: Queries like `User.find(:all, :conditions => ...)` will first look in Memcached and then look in the database for the results of that query. If there is a cache miss, it will populate the cache.
6
+
7
+
Write-Through: As objects are created, updated, and deleted, all of the caches are *automatically* kept up-to-date and coherent.
8
+
9
+
## Howto ##
10
+
### What kinds of queries are supported? ###
5
11
6
12
Many styles of ActiveRecord usage are supported:
7
13
@@ -15,35 +21,35 @@ Many styles of ActiveRecord usage are supported:
15
21
16
22
As you can see, the `find_by_`, `find_all_by`, hash, array, and string forms are all supported.
17
23
18
-
Queries with joins/includes are unsupported at this time.
24
+
Queries with joins/includes are unsupported at this time. In general, any query involving just equality (=) and conjunction (AND) is supported by `Cache Money`. Disjunction (OR) and inequality (!=, <=, etc.) are not typically materialized in a hash table style index and are unsupported at this time.
19
25
20
26
Queries with limits and offsets are supported. In general, however, if you are running queries with limits and offsets you are dealing with large datasets. It's more performant to place a limit on the size of the `Cache Money` index like so:
21
27
22
28
DirectMessage.index :user_id, :limit => 1000
23
29
24
30
In this example, only queries whose limit and offset are less than 1000 will use the cache.
25
31
26
-
=== Multiple indices are supported ===
32
+
###Multiple indices are supported###
27
33
28
34
class User
29
35
index :id
30
36
index :screen_name
31
37
index :email
32
38
end
33
39
34
-
==== with_scope support ====
40
+
####with_scope support####
35
41
36
42
`with_scope` and the like (`named_scope`, `has_many`, `belongs_to`, etc.) are fully supported. For example, `user.devices.find(1)` will first look in the cache if there is an index like this:
37
43
38
44
class Device
39
45
index [:user_id, :id]
40
46
end
41
47
42
-
=== Ordered indices ===
48
+
###Ordered indices###
43
49
44
-
class Message
45
-
index :sender_id, :order => :desc
46
-
end
50
+
class Message
51
+
index :sender_id, :order => :desc
52
+
end
47
53
48
54
The order declaration will ensure that the index is kept in the correctly sorted order. Only queries with order clauses compatible with the ordering in the index will use the cache:
49
55
@@ -62,19 +68,19 @@ will support queries like:
62
68
63
69
Note that ascending order is implicit in index declarations (i.e., not specifying an order is the same as ascending). This is also true of queries (order is not nondeterministic as in MySQL).
64
70
65
-
=== Window indices ===
71
+
###Window indices###
66
72
67
-
class Message
68
-
index :sender_id, :limit => 500, :buffer => 100
69
-
end
73
+
class Message
74
+
index :sender_id, :limit => 500, :buffer => 100
75
+
end
70
76
71
77
With a limit attribute, indices will only store limit + buffer in the cache. As new objects are created the index will be truncated, and as objects are destroyed, the cache will be refreshed if it has fewer than the limit of items. The buffer is how many "extra" items to keep around in case of deletes.
72
78
73
-
=== Calculations ===
79
+
###Calculations###
74
80
75
81
`Message.count(:all, :conditions => {:sender_id => ...})` will use the cache rather than the database. This happens for "free" -- no additional declarations are necessary.
76
82
77
-
=== Transactions ===
83
+
###Transactions###
78
84
79
85
Because of the parallel requests writing to the same indices, race conditions are possible. We have created a pessimistic "transactional" memcache client to handle the locking issues.
80
86
@@ -89,7 +95,7 @@ The writes to the cache are buffered until the transaction is committed. Reads w
89
95
90
96
Writes are not truly atomic as reads do not pay attention to locks. Therefore, it is possible to peak inside a partially committed transaction. This is a performance compromise, since acquiring a lock for a read was deemed too expensive. Again, the critical region is as small as possible, reducing the frequency of such "peeks".
91
97
92
-
==== Rollbacks ====
98
+
####Rollbacks####
93
99
94
100
CACHE.transaction do
95
101
CACHE.set(k, v)
@@ -100,29 +106,29 @@ Because transactions buffer writes, an exception in a transaction ensures that t
100
106
101
107
Nested transactions are fully supported, with partial rollback and (apparent) partial commitment (this is simulated with nested buffers).
102
108
103
-
=== Mocks ===
109
+
###Mocks###
104
110
105
111
For your unit tests, it is faster to use a Memcached mock than the real deal. Just place this in your initializer for your test environment:
106
112
107
113
$memcache = Cash::Mock.new
108
114
109
-
=== Locks ===
115
+
###Locks###
110
116
111
-
In most locks are unnecessary; the transactional memcache client will take care locks for you automatically and guarantees that no deadlocks can occur. But for very complex distributed transactions, shared locks are necessary.
117
+
In most cases locks are unnecessary; the transactional Memcached client will take care locks for you automatically and guarantees that no deadlocks can occur. But for very complex distributed transactions, shared locks are necessary.
112
118
113
119
$lock.synchronize('lock_name') do
114
120
$memcache.set("key", "value")
115
121
end
116
122
117
-
=== Local Cache ===
123
+
###Local Cache###
118
124
119
125
Sometimes your code will request the same cache key twice in one request. You can avoid a round trip to the Memcached server by using a local, per-request cache. Add this to your initializer:
Place this in `config/initializers/cache_money.rb`
128
134
@@ -136,7 +142,7 @@ Place this in `config/initializers/cache_money.rb`
136
142
is_cached :repository => $cache
137
143
end
138
144
139
-
==== Step 2: Add indices to your ActiveRecord models ====
145
+
####Step 2: Add indices to your ActiveRecord models####
140
146
141
147
Queries like `User.find(1)` will use the cache automatically. For more complex queries you must add indices on the attributes that you will query on. For example, a query like `User.find(:all, :conditions => {:name => 'bob'})` will require an index like:
0 commit comments