Class: Zipcoder::Cacher::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/zipcoder/cacher/redis.rb

Constant Summary

Constants inherited from Base

Base::KEY_BASE, Base::KEY_CITY, Base::KEY_COUNTY, Base::KEY_STATES, Base::KEY_STATE_CITIES, Base::KEY_STATE_COUNTIES, Base::KEY_ZIP

Instance Attribute Summary

Attributes inherited from Base

#loaded

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #iterate_cities, #iterate_zips, #load, #read_city_cache, #read_state_cities_cache, #read_state_counties_cache, #read_states, #read_zip_cache

Constructor Details

This class inherits a constructor from Zipcoder::Cacher::Base

Class Method Details

._create_redis_client(**kwargs) ⇒ Object

This is here for stubbing



10
11
12
# File 'lib/zipcoder/cacher/redis.rb', line 10

def self._create_redis_client(**kwargs)
  ::Redis.new(**kwargs)
end

Instance Method Details

#_empty_cacheObject



18
19
20
21
# File 'lib/zipcoder/cacher/redis.rb', line 18

def _empty_cache
  keys = @redis.keys("#{KEY_BASE}*")
  @redis.del(*keys) unless keys.empty?
end

#_init_cache(**kwargs) ⇒ Object



14
15
16
# File 'lib/zipcoder/cacher/redis.rb', line 14

def _init_cache(**kwargs)
  @redis = self.class._create_redis_client(**kwargs)
end

#_iterate_keys(**kwargs, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zipcoder/cacher/redis.rb', line 33

def _iterate_keys(**kwargs, &block)
  return if block == nil

  start_with = kwargs[:start_with] || KEY_BASE

  # Redis "keys" command will pre-filter the keys for us so no
  # need for "if" statement
  @redis.keys("#{start_with}*").each do |key|
    block.call(key)
  end
end

#_read_cache(key) ⇒ Object



28
29
30
31
# File 'lib/zipcoder/cacher/redis.rb', line 28

def _read_cache(key)
  data = @redis.get(key)
  data == nil ? nil : JSON.parse(data, :symbolize_names => true)
end

#_write_cache(key, value) ⇒ Object



23
24
25
26
# File 'lib/zipcoder/cacher/redis.rb', line 23

def _write_cache(key, value)
  return if value == nil
  @redis.set(key, value.to_json)
end