Class: Startback::Caching::EntityCache
- Inherits:
-
Object
- Object
- Startback::Caching::EntityCache
- Includes:
- Support::Robustness
- Defined in:
- lib/startback/caching/entity_cache.rb
Overview
A overriable caching abstraction aiming at making Entity-based caching easy.
This class MUST be overriden:
-
the ‘load_entity` protected method MUST be implemented to load data from a primary & context unaware key.
-
the ‘primary_key` protected method MAY be implemented to convert candidate keys (received from ultimate callers) to primary keys. The method is also a good place to check and/or log the keys actually used by callers.
-
the ‘context_free_key` protected method MAY be overriden to provide domain unrelated caching keys from primary keys, e.g. by encoding the context into the caching key itself, if needed.
-
the ‘valid?` protected method MAY be overriden to check validity of data extracted from the cache and force a refresh even if found.
An EntityCache takes an actual store at construction. The object must meet the specification writtern in Store. The ‘cache’ ruby gem can be used in practice.
Cache hits, outdated and miss are logged in debug, info, and info severity. The ‘cache_hit`, `cache_outdated`, `cache_miss` protected methods MAY be overriden to change that behavior.
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#get(candidate_key, caching_options = default_caching_options) ⇒ Object
Returns the entity corresponding to a given key.
-
#initialize(store, context = nil) ⇒ EntityCache
constructor
class DSL.
-
#invalidate(candidate_key) ⇒ Object
Invalidates the cache under a given key.
Methods included from Support::Robustness
#log, #monitor, #stop_errors, #try_max_times
Constructor Details
#initialize(store, context = nil) ⇒ EntityCache
class DSL
43 44 45 46 |
# File 'lib/startback/caching/entity_cache.rb', line 43 def initialize(store, context = nil) @store = store @context = context end |
Class Attribute Details
.default_ttl ⇒ Object
37 38 39 |
# File 'lib/startback/caching/entity_cache.rb', line 37 def default_ttl @default_ttl || (superclass.respond_to?(:default_ttl, true) && superclass.default_ttl) || 3600 end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
47 48 49 |
# File 'lib/startback/caching/entity_cache.rb', line 47 def context @context end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
47 48 49 |
# File 'lib/startback/caching/entity_cache.rb', line 47 def store @store end |
Instance Method Details
#get(candidate_key, caching_options = default_caching_options) ⇒ Object
Returns the entity corresponding to a given key.
If the entity is not in cache, loads it and puts it in cache using the caching options passed as second parameter.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/startback/caching/entity_cache.rb', line 53 def get(candidate_key, = ) pkey = primary_key(candidate_key) cache_key = encode_key(context_free_key(pkey)) if store.exist?(cache_key) cached = store.get(cache_key) if valid?(pkey, cached) cache_hit(pkey, cached) return cached else cache_outdated(pkey, cached) end end cache_miss(pkey) load_entity(pkey).tap{|to_cache| store.set(cache_key, to_cache, ) } end |
#invalidate(candidate_key) ⇒ Object
Invalidates the cache under a given key.
72 73 74 75 76 |
# File 'lib/startback/caching/entity_cache.rb', line 72 def invalidate(candidate_key) pkey = primary_key(candidate_key) cache_key = encode_key(context_free_key(pkey)) store.delete(cache_key) end |