Module: ScopedCacheKeys

Defined in:
lib/scoped_cache_keys.rb,
lib/scoped_cache_keys/version.rb

Constant Summary collapse

VERSION =
'0.3.0'

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.expire_scoped_cache_key_delayObject

If set, expire_scoped_cache_key will delay expiration by this many seconds globally.



7
8
9
# File 'lib/scoped_cache_keys.rb', line 7

def expire_scoped_cache_key_delay
  @expire_scoped_cache_key_delay
end

Instance Method Details

#expire_scoped_cache_key(scope, delay: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/scoped_cache_keys.rb', line 15

def expire_scoped_cache_key(scope, delay: nil)
  delay ||= ScopedCacheKeys.expire_scoped_cache_key_delay
  key = build_scoped_cache_key(scope)
  if delay
    entry = Rails.cache.read(key)
    Rails.cache.write(key, entry, expires_in: delay) if entry
  else
    Rails.cache.delete(key)
  end
end

#scoped_cache_key(scope, options = nil) ⇒ Object



10
11
12
13
# File 'lib/scoped_cache_keys.rb', line 10

def scoped_cache_key(scope, options = nil)
  base_key = Rails.cache.fetch(build_scoped_cache_key([scope]), options) { Time.now.to_f }
  build_scoped_cache_key [scope, base_key]
end