Class: RackJwtAegis::MemoryAdapter

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

Overview

Memory-based cache adapter (for development/testing)

Since:

  • 0.1.0

Instance Method Summary collapse

Methods inherited from CacheAdapter

build, #deserialize_value, #exist?, #serialize_value

Constructor Details

#initialize(options = {}) ⇒ MemoryAdapter

Returns a new instance of MemoryAdapter.

Since:

  • 0.1.0



72
73
74
75
76
77
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 72

def initialize(options = {})
  super
  @store = {}
  @expires = {}
  @mutex = Mutex.new
end

Instance Method Details

#cleanup_expiredObject (private)

Since:

  • 0.1.0



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 121

def cleanup_expired
  return unless @expires.any?

  now = Time.now
  expired_keys = @expires.select { |_, expiry| expiry < now }.keys

  expired_keys.each do |key|
    @store.delete(key)
    @expires.delete(key)
  end
end

#clearObject

Since:

  • 0.1.0



111
112
113
114
115
116
117
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 111

def clear
  @mutex.synchronize do
    @store.clear
    @expires.clear
    true
  end
end

#delete(key) ⇒ Object

Since:

  • 0.1.0



102
103
104
105
106
107
108
109
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 102

def delete(key)
  @mutex.synchronize do
    key_str = key.to_s
    @store.delete(key_str)
    @expires.delete(key_str)
    true
  end
end

#read(key) ⇒ Object

Since:

  • 0.1.0



79
80
81
82
83
84
85
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 79

def read(key)
  @mutex.synchronize do
    cleanup_expired
    value = @store[key.to_s]
    deserialize_value(value)
  end
end

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

Since:

  • 0.1.0



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rack_jwt_aegis/cache_adapter.rb', line 87

def write(key, value, expires_in: nil)
  @mutex.synchronize do
    key_str = key.to_s
    @store[key_str] = serialize_value(value)

    if expires_in
      @expires[key_str] = Time.now + expires_in
    else
      @expires.delete(key_str)
    end

    true
  end
end