Class: SgPostcode::CacheAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/sg_postcode/services/cache_adapter.rb

Constant Summary collapse

@@hash_name =
"SgPostcodeCaching"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ CacheAdapter

Init a new CacheAdapter object

@dependencies: redis

Redis.new will get REDIS_URL as host you can customize it by set value for ‘ENV`



17
18
19
20
# File 'lib/sg_postcode/services/cache_adapter.rb', line 17

def initialize(key)
  @cache = Redis.new
  @key = key
end

Class Method Details

.hash_name=(hash_name) ⇒ Object

Redefine @@hash_name (key_name in Redis hash store)

Examples:

SgPostcode::CacheAdapter.hash_name = "test_redis_store"


27
28
29
# File 'lib/sg_postcode/services/cache_adapter.rb', line 27

def self.hash_name=(hash_name)
  @@hash_name = hash_name
end

Instance Method Details

#fetchObject

Fetch request data from cache

TODO: will split #value_of method to sub-class cause Adapter shouldn’t call Redis directly



36
37
38
# File 'lib/sg_postcode/services/cache_adapter.rb', line 36

def fetch
  value_of @key
end

#store(key, value) ⇒ Object

Store new postcode and request_data to Redis



42
43
44
45
# File 'lib/sg_postcode/services/cache_adapter.rb', line 42

def store(key, value)
  @cache.hset(@@hash_name, key, value)
  value
end

#value_of(key) ⇒ Object

Get Value of a postcode from Redis Hash Store



49
50
51
# File 'lib/sg_postcode/services/cache_adapter.rb', line 49

def value_of(key)
  @cache.hget(@@hash_name, key)
end