Class: ObjectCache::RailsStore

Inherits:
CacheStore show all
Defined in:
lib/object_cache/rails_store.rb

Class Method Summary collapse

Methods inherited from CacheStore

array_or_value, fetch_fresh_results, fetch_from_array

Class Method Details

.cache_key(klass, id) ⇒ Object



34
35
36
# File 'lib/object_cache/rails_store.rb', line 34

def cache_key(klass, id)
  "object_cache:#{klass}:#{id}"
end

.fetch(klass, ids) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/object_cache/rails_store.rb', line 7

def fetch(klass, ids)
  results = []
  ids_to_refresh = []

  [*ids].each do |id|
    if Rails.cache.exist?(cache_key(klass, id))
      results << get(klass, id)
    else
      ids_to_refresh << id
    end
  end

  fresh_results = fetch_fresh_results(klass, ids_to_refresh)
  results += fresh_results if fresh_results

  array_or_value(results, ids)
end

.get(klass, id) ⇒ Object



25
26
27
# File 'lib/object_cache/rails_store.rb', line 25

def get(klass, id)
  Rails.cache.read(cache_key(klass, id))
end

.hash_empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/object_cache/rails_store.rb', line 50

def hash_empty?
  false
end

.hash_get(key) ⇒ Object



38
39
40
# File 'lib/object_cache/rails_store.rb', line 38

def hash_get(key)
  Rails.cache.read(hash_key(key))
end

.hash_key(key) ⇒ Object



46
47
48
# File 'lib/object_cache/rails_store.rb', line 46

def hash_key(key)
  "object_cache:#{key}"
end

.hash_set(key, value) ⇒ Object



42
43
44
# File 'lib/object_cache/rails_store.rb', line 42

def hash_set(key, value)
  Rails.cache.write(hash_key(key), value, expires_at: ObjectCache.ttl)
end

.set(klass, id, value) ⇒ Object



29
30
31
32
# File 'lib/object_cache/rails_store.rb', line 29

def set(klass, id, value)
  Rails.cache.write(cache_key(klass, id), value, expires_at: ObjectCache.ttl)
  return value
end