Class: Cache::Object::MultiGet
- Inherits:
-
Object
- Object
- Cache::Object::MultiGet
- Defined in:
- lib/cache/object/multi_get.rb
Instance Attribute Summary collapse
-
#clazz ⇒ Object
readonly
Returns the value of attribute clazz.
Instance Method Summary collapse
- #cached_objects(ids) ⇒ Object
- #fetch_all(ids) ⇒ Object
-
#initialize(clazz) ⇒ MultiGet
constructor
A new instance of MultiGet.
- #load_from_db(ids) ⇒ Object
- #missed_ids(ids, fetched_objects) ⇒ Object
- #object_keys(ids) ⇒ Object
Constructor Details
#initialize(clazz) ⇒ MultiGet
Returns a new instance of MultiGet.
6 7 8 |
# File 'lib/cache/object/multi_get.rb', line 6 def initialize(clazz) @clazz = clazz end |
Instance Attribute Details
#clazz ⇒ Object (readonly)
Returns the value of attribute clazz.
4 5 6 |
# File 'lib/cache/object/multi_get.rb', line 4 def clazz @clazz end |
Instance Method Details
#cached_objects(ids) ⇒ Object
26 27 28 29 |
# File 'lib/cache/object/multi_get.rb', line 26 def cached_objects(ids) keys = object_keys(ids) Cache::Object.adapter.read_multi(keys).values end |
#fetch_all(ids) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/cache/object/multi_get.rb', line 10 def fetch_all(ids) objs = cached_objects(ids) remaining = missed_ids(ids, objs) return objs if remaining.empty? objs + load_from_db(remaining) end |
#load_from_db(ids) ⇒ Object
17 18 19 20 |
# File 'lib/cache/object/multi_get.rb', line 17 def load_from_db(ids) primary_key = clazz.primary_key.to_sym clazz.where(primary_key => ids).to_a.each(&:write_cache!) end |
#missed_ids(ids, fetched_objects) ⇒ Object
31 32 33 |
# File 'lib/cache/object/multi_get.rb', line 31 def missed_ids(ids, fetched_objects) ids - fetched_objects.map(&:id) end |
#object_keys(ids) ⇒ Object
22 23 24 |
# File 'lib/cache/object/multi_get.rb', line 22 def object_keys(ids) ids.map { |id| Cache::Object::KeyGenerator.key_for_object(clazz.name, id) } end |