Class: HatiConfig::Cache::MemoryAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/hati_config/cache.rb

Overview

MemoryAdapter class provides in-memory caching.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ MemoryAdapter

Returns a new instance of MemoryAdapter.



201
202
203
204
# File 'lib/hati_config/cache.rb', line 201

def initialize
  @store = {}
  @expiry = {}
end

Instance Method Details

#delete(key) ⇒ Object

Deletes a value from the cache.

Parameters:

  • key (String) —

    The cache key



229
230
231
232
# File 'lib/hati_config/cache.rb', line 229

def delete(key)
  @store.delete(key)
  @expiry.delete(key)
end

#get(key) ⇒ Object?

Gets a value from the cache.

Parameters:

  • key (String) —

    The cache key

Returns:

  • (Object, nil) —

    The cached value or nil if not found/expired



210
211
212
213
214
# File 'lib/hati_config/cache.rb', line 210

def get(key)
  return nil if expired?(key)

  @store[key]
end

#set(key, value, ttl) ⇒ Object

Sets a value in the cache.

Parameters:

  • key (String) —

    The cache key

  • value (Object) —

    The value to cache

  • ttl (Integer) —

    The TTL in seconds



221
222
223
224
# File 'lib/hati_config/cache.rb', line 221

def set(key, value, ttl)
  @store[key] = value
  @expiry[key] = Time.now + ttl if ttl
end