arid_cache 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
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
- == Cache Keys & Managing your Caches
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
1
+ 0.2.2
data/arid_cache.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{arid_cache}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Karl Varga"]
@@ -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
- if combined_options.include?(:page)
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
- # @arg klass target class, if supplied and no :per_page option is specified, calls
216
- # klass.per_page to get it
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
- def opts_for_find
226
- combined_options.reject { |k,v| !OPTIONS_FOR_FIND.include?(k) }
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
- # Preserve the original order of the results if no :order option is specified.
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
- # @arg options hash options for find
249
- # @arg ids array of ids to order by
250
- def preserve_order(options, ids)
251
- return options if options.include?(:order)
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
- options[:order] = "FIELD(id,#{ids.join(',')})"
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
- options[:order] = "CASE " + order + " END"
284
+ "CASE " + order + " END"
258
285
  end
259
- options
260
286
  end
261
287
  end
262
288
  end
@@ -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
- store_key = instance_store_key(klass, key)
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
- store_key = class_store_key(klass, key)
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
- store_key = object_store_key(object, key)
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
@@ -8,7 +8,7 @@ require 'arid_cache/cache_proxy'
8
8
 
9
9
  module AridCache
10
10
  extend AridCache::Helpers
11
- class Error < StandardError; end
11
+ class Error < StandardError; end #:nodoc:
12
12
 
13
13
  def self.cache
14
14
  AridCache::CacheProxy
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arid_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Varga