Class: RackJwtAegis::RedisAdapter

Inherits:
CacheAdapter show all
Defined in:
lib/rack_jwt_aegis/cache_adapter.rb

Overview

Redis cache adapter

Since:

  • 0.1.0

Instance Method Summary collapse

Methods inherited from CacheAdapter

build, #deserialize_value, #exist?, #serialize_value

Constructor Details

#initialize(options = {}) ⇒ RedisAdapter

Returns a new instance of RedisAdapter.

Since:

  • 0.1.0



136
137
138
139
140
141
142
143
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 136

def initialize(options = {})
  super
  require 'redis' unless defined?(Redis)

  @redis = options[:redis_instance] || Redis.new(options)
rescue LoadError
  raise CacheError, "Redis gem not found. Add 'gem \"redis\"' to your Gemfile."
end

Instance Method Details

#clearObject

Since:

  • 0.1.0



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.message}"
end

#delete(key) ⇒ Object

Since:

  • 0.1.0



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.message}"
end

#read(key) ⇒ Object

Since:

  • 0.1.0



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.message}"
end

#write(key, value, expires_in: nil) ⇒ Object

Since:

  • 0.1.0



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.message}"
end