class Startback::Caching::EntityCache

A overriable caching abstraction aiming at making Entity-based caching easy.

This class MUST be overriden:

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.

Attributes

default_ttl[W]

Default time to live, in seconds

context[R]
store[R]

Public Class Methods

default_ttl() click to toggle source
# 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
new(store, context = nil) click to toggle source
# File lib/startback/caching/entity_cache.rb, line 43
def initialize(store, context = nil)
  @store = store
  @context = context
end

Public Instance Methods

get(candidate_key, caching_options = default_caching_options) click to toggle source

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.

# File lib/startback/caching/entity_cache.rb, line 53
def get(candidate_key, caching_options = default_caching_options)
  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, caching_options)
  }
end
invalidate(candidate_key) click to toggle source

Invalidates the cache under a given key.

# 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

Protected Instance Methods

cache_hit(pkey, cached) click to toggle source
# File lib/startback/caching/entity_cache.rb, line 80
def cache_hit(pkey, cached)
  log(:debug, self, "cache_hit", context, op_data: pkey)
end
cache_miss(pkey) click to toggle source
# File lib/startback/caching/entity_cache.rb, line 88
def cache_miss(pkey)
  log(:info, self, "cache_miss", context, op_data: pkey)
end
cache_outdated(pkey, cached) click to toggle source
# File lib/startback/caching/entity_cache.rb, line 84
def cache_outdated(pkey, cached)
  log(:info, self, "cache_outdated", context, op_data: pkey)
end
context_free_key(primary_key) click to toggle source

Converts a primary_key to a context_free_key, using the context (instance variable) to encode the context itself into the actual cache key.

The default implementation simply returns the primary key and MAY be overriden.

# File lib/startback/caching/entity_cache.rb, line 130
def context_free_key(primary_key)
  full_key(primary_key)
end
default_caching_options() click to toggle source
# File lib/startback/caching/entity_cache.rb, line 92
def default_caching_options
  { ttl: self.class.default_ttl }
end
encode_key(context_free_key) click to toggle source

Encodes a context free key to an actual cache key.

Default implementation uses JSON.fast_generate but MAY be overriden.

# File lib/startback/caching/entity_cache.rb, line 110
def encode_key(context_free_key)
  JSON.fast_generate(context_free_key)
end
full_key(primary_key) click to toggle source

Deprecated, will be removed in 0.6.0. Use context_free_key instead.

# File lib/startback/caching/entity_cache.rb, line 136
def full_key(primary_key)
  primary_key
end
load_entity(primary_key) click to toggle source

Actually loads the entity using the given primary key, and possibly the cache context.

This method MUST be implemented and raises a NotImplementedError by default.

# File lib/startback/caching/entity_cache.rb, line 145
def load_entity(primary_key)
  load_raw_data(primary_key)
end
load_raw_data(*args, &bl) click to toggle source

Deprecated, will be removed in 0.6.0. Use load_entity instead.

# File lib/startback/caching/entity_cache.rb, line 151
def load_raw_data(*args, &bl)
  raise NotImplementedError, "#{self.class.name}#load_entity"
end
primary_key(candidate_key) click to toggle source

Converts a candidate key to a primary key, so as to prevent cache duplicates if callers are allowed to request an entity through various keys.

The default implementation returns the candidate key and MAY be overriden.

# File lib/startback/caching/entity_cache.rb, line 102
def primary_key(candidate_key)
  candidate_key
end
valid?(primary_key, cached) click to toggle source

Returns whether `cached` entity seems fresh enough to be returned as a cache hit.

This method provides a way to check freshness using, e.g. `updated_at` or `etag` kind of entity fields. The default implementation returns true and MAY be overriden.

# File lib/startback/caching/entity_cache.rb, line 120
def valid?(primary_key, cached)
  true
end