arid_cache 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +1 -3
- data/VERSION +1 -1
- data/arid_cache.gemspec +1 -1
- data/lib/arid_cache/cache_proxy.rb +61 -35
- data/lib/arid_cache/store.rb +10 -9
- data/lib/arid_cache.rb +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -129,9 +129,7 @@ You can configure your caches in this manner wherever you want, but I think the
|
|
129
129
|
...
|
130
130
|
end
|
131
131
|
|
132
|
-
==
|
133
|
-
|
134
|
-
=== Cache Keys
|
132
|
+
== Cache Keys
|
135
133
|
|
136
134
|
AridCache cache keys are defined based on the methods you call to interact with the cache. For example:
|
137
135
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/arid_cache.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module AridCache
|
2
2
|
class CacheProxy
|
3
|
-
attr_accessor :object, :key, :opts, :blueprint, :cached, :cache_key, :block, :records, :combined_options
|
3
|
+
attr_accessor :object, :key, :opts, :blueprint, :cached, :cache_key, :block, :records, :combined_options, :klass
|
4
4
|
|
5
5
|
# AridCache::CacheProxy::Result
|
6
6
|
#
|
@@ -89,25 +89,8 @@ module AridCache
|
|
89
89
|
execute_find
|
90
90
|
elsif cached.is_a?(AridCache::CacheProxy::Result)
|
91
91
|
if cached.has_ids?
|
92
|
-
klass = cached.klass || object_base_class
|
93
|
-
|
94
|
-
if combined_options.include?(:order) # order and paginate in the database
|
95
|
-
klass.paginate(cached.ids, opts_for_find.merge(opts_for_paginate(klass)))
|
96
|
-
else # paginate in memory
|
97
|
-
paged_ids = cached.ids.paginate(opts_for_paginate(klass))
|
98
|
-
paged_ids.replace(klass.find(paged_ids, preserve_order(opts_for_find, paged_ids)))
|
99
|
-
end
|
100
|
-
elsif combined_options.include?(:limit) || combined_options.include?(:offset)
|
101
|
-
if combined_options.include?(:order) # limit and offset in the database
|
102
|
-
klass.find(cached.ids, opts_for_find)
|
103
|
-
else # limit and offset in memory
|
104
|
-
offset, limit = combined_options.delete(:offset) || 0, combined_options.delete(:limit) || cached.count
|
105
|
-
ids = cached.ids[offset, limit]
|
106
|
-
klass.find(ids, preserve_order(opts_for_find, ids))
|
107
|
-
end
|
108
|
-
else
|
109
|
-
klass.find(cached.ids, opts_for_find)
|
110
|
-
end
|
92
|
+
self.klass = cached.klass || object_base_class
|
93
|
+
fetch_from_cache
|
111
94
|
else
|
112
95
|
execute_find
|
113
96
|
end
|
@@ -118,6 +101,43 @@ module AridCache
|
|
118
101
|
|
119
102
|
private
|
120
103
|
|
104
|
+
def fetch_from_cache
|
105
|
+
if paginate?
|
106
|
+
fetch_and_paginate
|
107
|
+
elsif limit_or_offset?
|
108
|
+
fetch_and_limit
|
109
|
+
else
|
110
|
+
klass.find(cached.ids, opts_for_find)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def fetch_and_paginate
|
115
|
+
if combined_options.include?(:order) # order and paginate in the database
|
116
|
+
klass.paginate(cached.ids, opts_for_find.merge(opts_for_paginate))
|
117
|
+
else # paginate in memory
|
118
|
+
paged_ids = cached.ids.paginate(opts_for_paginate)
|
119
|
+
paged_ids.replace(klass.find(paged_ids, opts_for_find(paged_ids)))
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def fetch_and_limit
|
124
|
+
if combined_options.include?(:order)
|
125
|
+
klass.find(cached.ids, opts_for_find)
|
126
|
+
else
|
127
|
+
offset, limit = combined_options.delete(:offset) || 0, combined_options.delete(:limit) || cached.count
|
128
|
+
ids = cached.ids[offset, limit]
|
129
|
+
klass.find(ids, opts_for_find(ids))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def paginate?
|
134
|
+
combined_options.include?(:page)
|
135
|
+
end
|
136
|
+
|
137
|
+
def limit_or_offset?
|
138
|
+
combined_options.include?(:limit) || combined_options.include?(:offset)
|
139
|
+
end
|
140
|
+
|
121
141
|
def refresh_cache?
|
122
142
|
cached.nil? || opts[:force]
|
123
143
|
end
|
@@ -212,9 +232,8 @@ module AridCache
|
|
212
232
|
|
213
233
|
OPTIONS_FOR_PAGINATE = [:page, :per_page, :total_entries]
|
214
234
|
|
215
|
-
#
|
216
|
-
|
217
|
-
def opts_for_paginate(klass = nil)
|
235
|
+
# Filter options for paginate, if *klass* is set, we get the :per_page value from it.
|
236
|
+
def opts_for_paginate
|
218
237
|
paginate_opts = combined_options.reject { |k,v| !OPTIONS_FOR_PAGINATE.include?(k) }
|
219
238
|
paginate_opts[:per_page] = klass.per_page if klass && !paginate_opts.include?(:per_page)
|
220
239
|
paginate_opts
|
@@ -222,8 +241,15 @@ module AridCache
|
|
222
241
|
|
223
242
|
OPTIONS_FOR_FIND = [ :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from, :lock ]
|
224
243
|
|
225
|
-
|
226
|
-
|
244
|
+
# Preserve the original order of the results if no :order option is specified.
|
245
|
+
#
|
246
|
+
# @arg ids array of ids to order by unless an :order option is specified. If not
|
247
|
+
# specified, cached.ids is used.
|
248
|
+
def opts_for_find(ids=nil)
|
249
|
+
ids ||= cached.ids
|
250
|
+
find_opts = combined_options.reject { |k,v| !OPTIONS_FOR_FIND.include?(k) }
|
251
|
+
find_opts[:order] = preserve_order(ids) unless find_opts.include?(:order)
|
252
|
+
find_opts
|
227
253
|
end
|
228
254
|
|
229
255
|
OPTIONS_FOR_CACHE = [ :expires_in ]
|
@@ -238,25 +264,25 @@ module AridCache
|
|
238
264
|
combined_options.reject { |k,v| !OPTIONS_FOR_CACHE_KEY.include?(k) }
|
239
265
|
end
|
240
266
|
|
241
|
-
def object_base_class
|
267
|
+
def object_base_class #:nodoc:
|
242
268
|
object.is_a?(Class) ? object : object.class
|
243
269
|
end
|
244
270
|
|
245
|
-
#
|
271
|
+
# Generate an ORDER BY clause that preserves the ordering of the ids in *ids*.
|
272
|
+
#
|
246
273
|
# The method we use depends on the database adapter because only MySQL
|
247
|
-
# supports the ORDER BY FIELD() function.
|
248
|
-
#
|
249
|
-
#
|
250
|
-
|
251
|
-
|
274
|
+
# supports the ORDER BY FIELD() function. For other databases we use
|
275
|
+
# a CASE statement.
|
276
|
+
#
|
277
|
+
# TODO: is it quicker to sort in memory?
|
278
|
+
def preserve_order(ids)
|
252
279
|
if ::ActiveRecord::Base.is_mysql_adapter?
|
253
|
-
|
280
|
+
"FIELD(id,#{ids.join(',')})"
|
254
281
|
else
|
255
282
|
order = ''
|
256
283
|
ids.each_index { |i| order << "WHEN id=#{ids[i]} THEN #{i+1} " }
|
257
|
-
|
284
|
+
"CASE " + order + " END"
|
258
285
|
end
|
259
|
-
options
|
260
286
|
end
|
261
287
|
end
|
262
288
|
end
|
data/lib/arid_cache/store.rb
CHANGED
@@ -65,8 +65,8 @@ module AridCache
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
-
class InstanceCacheConfiguration < Configuration; end
|
69
|
-
class ClassCacheConfiguration < Configuration; end
|
68
|
+
class InstanceCacheConfiguration < Configuration; end #:nodoc:
|
69
|
+
class ClassCacheConfiguration < Configuration; end #:nodoc:
|
70
70
|
|
71
71
|
def has?(object, key)
|
72
72
|
self.include?(object_store_key(object, key))
|
@@ -86,20 +86,17 @@ module AridCache
|
|
86
86
|
end
|
87
87
|
|
88
88
|
def add_instance_cache_configuration(klass, key, opts, proc)
|
89
|
-
|
90
|
-
self[store_key] = AridCache::Store::Blueprint.new(klass, key, opts, proc)
|
89
|
+
add_generic_cache_configuration(instance_store_key(klass, key), klass, key, opts, proc)
|
91
90
|
end
|
92
91
|
|
93
92
|
def add_class_cache_configuration(klass, key, opts, proc)
|
94
|
-
|
95
|
-
self[store_key] = AridCache::Store::Blueprint.new(klass, key, opts, proc)
|
93
|
+
add_generic_cache_configuration(class_store_key(klass, key), klass, key, opts, proc)
|
96
94
|
end
|
97
95
|
|
98
96
|
def add_object_cache_configuration(object, key, opts, proc)
|
99
|
-
|
100
|
-
self[store_key] = AridCache::Store::Blueprint.new(object, key, opts, proc)
|
97
|
+
add_generic_cache_configuration(object_store_key(object, key), object, key, opts, proc)
|
101
98
|
end
|
102
|
-
|
99
|
+
|
103
100
|
def find_or_create(object, key)
|
104
101
|
store_key = object_store_key(object, key)
|
105
102
|
if self.include?(store_key)
|
@@ -111,6 +108,10 @@ module AridCache
|
|
111
108
|
|
112
109
|
protected
|
113
110
|
|
111
|
+
def add_generic_cache_configuration(store_key, *args)
|
112
|
+
self[store_key] = AridCache::Store::Blueprint.new(*args)
|
113
|
+
end
|
114
|
+
|
114
115
|
def initialize
|
115
116
|
end
|
116
117
|
|
data/lib/arid_cache.rb
CHANGED