Module: Redcache
- Defined in:
- lib/redcache.rb,
lib/redcache/version.rb,
lib/redcache/configuration.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VERSION =
"0.0.3"
Class Attribute Summary collapse
Class Method Summary collapse
- .cache(redis_key, &block) ⇒ Object
- .cache_time ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .decrypt(value) ⇒ Object
- .encrypt(value) ⇒ Object
- .encrypt? ⇒ Boolean
- .fernet ⇒ Object
- .get_value(key) ⇒ Object
- .key_stale?(redis_key) ⇒ Boolean
- .log(str, key) ⇒ Object
- .log_prefix(str) ⇒ Object
- .prep_value(value) ⇒ Object
- .read_from_cache(redis_key, block) ⇒ Object
- .redis ⇒ Object
- .redis_up? ⇒ Boolean
- .refresh_cache(redis_key, block) ⇒ Object
- .secret ⇒ Object
- .set_value(key, value) ⇒ Object
- .skip_cache? ⇒ Boolean
- .stale_time ⇒ Object
- .test? ⇒ Boolean
- .with_redis(&block) ⇒ Object
- .write_into_cache(redis_key, value) ⇒ Object
Class Attribute Details
.configuration ⇒ Object
79 80 81 |
# File 'lib/redcache.rb', line 79 def configuration @configuration ||= Configuration.new end |
Class Method Details
.cache(redis_key, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/redcache.rb', line 17 def cache(redis_key, &block) # return immediately if we shouldn't or can't cache return block.call if skip_cache? || !redis_up? with_redis do # attempt to read from cache, running and caching the block if cold value = read_from_cache(redis_key, block) if value.nil? value = block.call if value.nil? write_into_cache(redis_key, value) end return value end end |
.cache_time ⇒ Object
101 102 103 |
# File 'lib/redcache.rb', line 101 def cache_time configuration.cache_time end |
.configure {|configuration| ... } ⇒ Object
13 14 15 |
# File 'lib/redcache.rb', line 13 def configure yield(configuration) end |
.decrypt(value) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/redcache.rb', line 71 def decrypt(value) return nil if value.nil? return value unless encrypt? verifier = fernet.verifier(secret, value) return MultiJson.load(verifier.) if verifier.valid? return nil end |
.encrypt(value) ⇒ Object
66 67 68 69 |
# File 'lib/redcache.rb', line 66 def encrypt(value) return prep_value(value) unless encrypt? fernet.generate(secret, prep_value(value)) end |
.encrypt? ⇒ Boolean
121 122 123 |
# File 'lib/redcache.rb', line 121 def encrypt? configuration.encrypt end |
.fernet ⇒ Object
137 138 139 |
# File 'lib/redcache.rb', line 137 def fernet ::Fernet end |
.get_value(key) ⇒ Object
53 54 55 |
# File 'lib/redcache.rb', line 53 def get_value(key) decrypt redis.get(key) end |
.key_stale?(redis_key) ⇒ Boolean
61 62 63 64 |
# File 'lib/redcache.rb', line 61 def key_stale?(redis_key) ttl = redis.ttl(redis_key) return ttl < (configuration.cache_time - configuration.stale_time) end |
.log(str, key) ⇒ Object
129 130 131 |
# File 'lib/redcache.rb', line 129 def log(str, key) configuration.logger.log(log_prefix(str) => 1, :key => key) unless configuration.silent end |
.log_prefix(str) ⇒ Object
133 134 135 |
# File 'lib/redcache.rb', line 133 def log_prefix(str) [configuration.log_prefix, str].join(".") end |
.prep_value(value) ⇒ Object
117 118 119 |
# File 'lib/redcache.rb', line 117 def prep_value(value) MultiJson.encode(value) end |
.read_from_cache(redis_key, block) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/redcache.rb', line 31 def read_from_cache(redis_key, block) value = get_value(redis_key) value.nil? ? log("cache.miss", redis_key) : log("cache.hit", redis_key) refresh_cache(redis_key, block) if key_stale?(redis_key) && !value.nil? return value end |
.redis ⇒ Object
83 84 85 |
# File 'lib/redcache.rb', line 83 def redis configuration.redis end |
.redis_up? ⇒ Boolean
87 88 89 90 91 92 93 94 95 |
# File 'lib/redcache.rb', line 87 def redis_up? begin redis.ping rescue Redis::CannotConnectError puts "Redis is DOWN! :shitsonfire:" return false end return true end |
.refresh_cache(redis_key, block) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/redcache.rb', line 38 def refresh_cache(redis_key, block) log("cache.stale_refresh", redis_key) Thread.new do write_into_cache(redis_key, block.call) end end |
.secret ⇒ Object
125 126 127 |
# File 'lib/redcache.rb', line 125 def secret configuration.secret end |
.set_value(key, value) ⇒ Object
57 58 59 |
# File 'lib/redcache.rb', line 57 def set_value(key, value) redis.setex key, configuration.cache_time, encrypt(value) end |
.skip_cache? ⇒ Boolean
109 110 111 |
# File 'lib/redcache.rb', line 109 def skip_cache? configuration.skip_cache end |
.stale_time ⇒ Object
105 106 107 |
# File 'lib/redcache.rb', line 105 def stale_time configuration.stale_time end |
.test? ⇒ Boolean
113 114 115 |
# File 'lib/redcache.rb', line 113 def test? ENV["RACK_ENV"] == 'test' end |
.with_redis(&block) ⇒ Object
97 98 99 |
# File 'lib/redcache.rb', line 97 def with_redis(&block) block.call if redis_up? end |
.write_into_cache(redis_key, value) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/redcache.rb', line 45 def write_into_cache(redis_key, value) with_redis do log("cache.write", redis_key) set_value(redis_key, value) end value end |