class Shamu::Entities::IdentityCache
Keeps a cache of {Entity} instances in memory for quick retrieval. Since entities are immutable, the cache
Attributes
Public Class Methods
Provide a block to automatically coerce keys to a known type. For example converting numeric strings (“123”) to Integer values.
@param [Symbol] coercion method to call on keys instead of providing a
block.
@yield (key) @yieldparam [Object] key to coerce @yieldreturn [Object] the coerced value of the key.
# File lib/shamu/entities/identity_cache.rb, line 16 def initialize( coercion = nil, &coercion_block ) @cache = {} @coercion = block_given? ? coercion_block : ( coercion && coercion.to_proc ) end
Public Instance Methods
Add a new entity to the cache. @param [Object] key of the entity. Typically the {Entity#id}. @param [Entity] entity to cache @return [entity]
# File lib/shamu/entities/identity_cache.rb, line 42 def add( key, entity ) cache[ coerce_key( key ) ] = entity end
Fetch an entity with the given key. @param [Object] key of the entity. Typically the {Entity#id}. @return [Entity] the entity if found, otherwise `nil`.
# File lib/shamu/entities/identity_cache.rb, line 24 def fetch( key ) cache.fetch( coerce_key( key ), nil ) end
Invalidate the cached entry for the {Entity} with the given `key`. @param [Object] key of the entity. Typically the {Entity#id}. @return [Entity] the entity that was at the given key if present.
# File lib/shamu/entities/identity_cache.rb, line 49 def invalidate( key ) cache.delete( key ) end
Filter the list of `keys` to those that haven't been cached yet. @param [Array] keys of the {Entity entities} that are about to be
{#fetch fetched}.
@return [Array] the uncached keys.
# File lib/shamu/entities/identity_cache.rb, line 32 def uncached_keys( keys ) uncached = Array( keys ).map { |k| coerce_key( k ) } uncached.reject! { |k| cache.key?( k ) } uncached end
Private Instance Methods
# File lib/shamu/entities/identity_cache.rb, line 58 def coerce_key( key ) return key unless coercion coercion.call( key ) end