Class: Hold::Serialized::HashRepository

Inherits:
Object
  • Object
show all
Includes:
HashRepository
Defined in:
lib/hold/serialized/hash_repository.rb

Overview

A repository which caches serialized versions of an entity in a string-based key/value cache.

Wraps a string-based HashRepository, and requires a serializer responding to ‘serialize’ and ‘deserialize’.

May optionally have a ‘key_prefix’, which is a prefixed namespace added to the cache keys before getting/setting the serialized values in the underlying cache.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashRepository

#can_get_class?, #can_set_class?, #key_cell

Constructor Details

#initialize(cache, serializer, key_prefix = nil) ⇒ HashRepository

Returns a new instance of HashRepository.



17
18
19
20
21
# File 'lib/hold/serialized/hash_repository.rb', line 17

def initialize(cache, serializer, key_prefix=nil)
  @cache = cache
  @serializer = serializer
  @key_prefix = key_prefix
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



15
16
17
# File 'lib/hold/serialized/hash_repository.rb', line 15

def cache
  @cache
end

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



15
16
17
# File 'lib/hold/serialized/hash_repository.rb', line 15

def key_prefix
  @key_prefix
end

#serializerObject (readonly)

Returns the value of attribute serializer.



15
16
17
# File 'lib/hold/serialized/hash_repository.rb', line 15

def serializer
  @serializer
end

Instance Method Details

#cache_key(key) ⇒ Object



23
24
25
# File 'lib/hold/serialized/hash_repository.rb', line 23

def cache_key(key)
  @key_prefix ? @key_prefix + key.to_s : key.to_s
end

#clear_key(key) ⇒ Object



45
46
47
# File 'lib/hold/serialized/hash_repository.rb', line 45

def clear_key(key)
  @cache.clear_key(cache_key(key))
end

#get_many_with_keys(keys) ⇒ Object



35
36
37
38
# File 'lib/hold/serialized/hash_repository.rb', line 35

def get_many_with_keys(keys)
  jsons = @cache.get_many_with_keys(*keys.map {|key| cache_key(key)})
  jsons.map {|json| json && @serializer.deserialize(json)}
end

#get_with_key(key) ⇒ Object



31
32
33
# File 'lib/hold/serialized/hash_repository.rb', line 31

def get_with_key(key)
  json = @cache.get_with_key(cache_key(key)) and @serializer.deserialize(json)
end

#has_key?(key) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)


40
41
42
# File 'lib/hold/serialized/hash_repository.rb', line 40

def has_key?(key)
  @cache.has_key?(cache_key(key))
end

#set_with_key(key, entity) ⇒ Object



27
28
29
# File 'lib/hold/serialized/hash_repository.rb', line 27

def set_with_key(key, entity)
  @cache.set_with_key(cache_key(key), @serializer.serialize(entity))
end