Class: ActiveRecordCache::RecordCache
- Inherits:
-
Object
- Object
- ActiveRecordCache::RecordCache
- Defined in:
- lib/active_record_cache/record_cache.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
Instance Method Summary collapse
-
#[](id) ⇒ Object
Get a value from the cache by primary key.
-
#cache_key(id) ⇒ Object
Generate a cache key for a record.
-
#default ⇒ Object
Get the default value for whether the cache is enabled or not.
-
#expire(id) ⇒ Object
Remove an entry from the cache.
-
#initialize(klass, options = {}) ⇒ RecordCache
constructor
A new instance of RecordCache.
- #read(ids) ⇒ Object
Constructor Details
#initialize(klass, options = {}) ⇒ RecordCache
Returns a new instance of RecordCache.
5 6 7 8 9 10 11 |
# File 'lib/active_record_cache/record_cache.rb', line 5 def initialize(klass, = {}) @klass = klass @cache = [:cache] || ActiveRecordCache.cache @expires_in = [:expires_in] || @cache.[:expires_in] @preload = [:preload] @default = ([:default].nil? ? false : !![:default]) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
3 4 5 |
# File 'lib/active_record_cache/record_cache.rb', line 3 def cache @cache end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
3 4 5 |
# File 'lib/active_record_cache/record_cache.rb', line 3 def expires_in @expires_in end |
Instance Method Details
#[](id) ⇒ Object
Get a value from the cache by primary key.
45 46 47 48 49 |
# File 'lib/active_record_cache/record_cache.rb', line 45 def [](id) @cache.fetch(cache_key(id), :expires_in => @expires_in) do finder.where(@klass.primary_key => id).first end end |
#cache_key(id) ⇒ Object
Generate a cache key for a record.
57 58 59 |
# File 'lib/active_record_cache/record_cache.rb', line 57 def cache_key(id) "#{@klass.model_name.cache_key}/#{id}" end |
#default ⇒ Object
Get the default value for whether the cache is enabled or not.
62 63 64 65 66 67 68 69 |
# File 'lib/active_record_cache/record_cache.rb', line 62 def default defaults = Thread.current[:active_record_cache_defaults] if defaults && defaults.include?(@klass.name) defaults[@klass.name] else @default end end |
#expire(id) ⇒ Object
Remove an entry from the cache.
52 53 54 |
# File 'lib/active_record_cache/record_cache.rb', line 52 def expire(id) @cache.delete(cache_key(id)) end |
#read(ids) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_record_cache/record_cache.rb', line 13 def read(ids) ids = ids.first if ids.is_a?(Array) && ids.size == 1 if ids.is_a?(Array) keys = [] id_key_map = {} ids.each do |id| key = cache_key(id) keys << key id_key_map[id] = key end record_map = @cache.read_multi(*keys) missing_ids = ids.reject{|id| record_map[id_key_map[id]]} unless missing_ids.empty? finder.where(@klass.primary_key => missing_ids).each do |record| key = id_key_map[record.id] record_map[key] = record @cache.write(key, record, :expires_in => @expires_in) end end records = [] ids.each do |id| record = record_map[id_key_map[id]] records << record if record end records else [self[ids]] end end |