class SgPostcode::CacheAdapter

Public Class Methods

hash_name=(hash_name) click to toggle source

Redefine @@hash_name (key_name in Redis hash store)

@example

SgPostcode::CacheAdapter.hash_name = "test_redis_store"
# File lib/sg_postcode/services/cache_adapter.rb, line 27
def self.hash_name=(hash_name)
  @@hash_name = hash_name
end
new(key) click to toggle source

Init a new CacheAdapter object

@dependencies: redis

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

@params

@cache
@key in this context is a postcode
# File lib/sg_postcode/services/cache_adapter.rb, line 17
def initialize(key)
  @cache = Redis.new
  @key = key
end

Public Instance Methods

fetch() click to toggle source

Fetch request data from cache

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

# File lib/sg_postcode/services/cache_adapter.rb, line 36
def fetch
  value_of @key
end
store(key, value) click to toggle source

Store new postcode and request_data to Redis

# 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) click to toggle source

Get Value of a postcode from Redis Hash Store

# File lib/sg_postcode/services/cache_adapter.rb, line 49
def value_of(key)
  @cache.hget(@@hash_name, key)
end