Class: RackJwtAegis::RedisAdapter
- Inherits:
-
CacheAdapter
- Object
- CacheAdapter
- RackJwtAegis::RedisAdapter
- Defined in:
- lib/rack_jwt_aegis/cache_adapter.rb
Overview
Redis cache adapter
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(options = {}) ⇒ RedisAdapter
constructor
A new instance of RedisAdapter.
- #read(key) ⇒ Object
- #write(key, value, expires_in: nil) ⇒ Object
Methods inherited from CacheAdapter
Constructor Details
#initialize(options = {}) ⇒ RedisAdapter
Returns a new instance of RedisAdapter.
136 137 138 139 140 141 142 143 |
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 136 def initialize( = {}) super require 'redis' unless defined?(Redis) @redis = [:redis_instance] || Redis.new() rescue LoadError raise CacheError, "Redis gem not found. Add 'gem \"redis\"' to your Gemfile." end |
Instance Method Details
#clear ⇒ Object
173 174 175 176 177 178 |
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 173 def clear @redis.flushdb true rescue StandardError => e raise CacheError, "Redis clear error: #{e.}" end |
#delete(key) ⇒ Object
167 168 169 170 171 |
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 167 def delete(key) @redis.del(key.to_s).positive? rescue StandardError => e raise CacheError, "Redis delete error: #{e.}" end |
#read(key) ⇒ Object
145 146 147 148 149 150 |
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 145 def read(key) value = @redis.get(key.to_s) deserialize_value(value) rescue StandardError => e raise CacheError, "Redis read error: #{e.}" end |
#write(key, value, expires_in: nil) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 152 def write(key, value, expires_in: nil) key_str = key.to_s serialized_value = serialize_value(value) if expires_in @redis.setex(key_str, expires_in.to_i, serialized_value) else @redis.set(key_str, serialized_value) end true rescue StandardError => e raise CacheError, "Redis write error: #{e.}" end |