Class: KeeperSecretsManager::Storage::CachingStorage

Inherits:
Object
  • Object
show all
Includes:
KeyValueStorage
Defined in:
lib/keeper_secrets_manager/storage.rb

Overview

Cacheable storage wrapper

Instance Method Summary collapse

Methods included from KeyValueStorage

#contains?, #get_bytes, #save_bytes

Constructor Details

#initialize(base_storage, ttl_seconds = 600) ⇒ CachingStorage

Returns a new instance of CachingStorage.



203
204
205
206
207
208
# File 'lib/keeper_secrets_manager/storage.rb', line 203

def initialize(base_storage, ttl_seconds = 600)
  @base_storage = base_storage
  @ttl_seconds = ttl_seconds
  @cache = {}
  @timestamps = {}
end

Instance Method Details

#clear_cacheObject



240
241
242
243
# File 'lib/keeper_secrets_manager/storage.rb', line 240

def clear_cache
  @cache.clear
  @timestamps.clear
end

#delete(key) ⇒ Object



233
234
235
236
237
238
# File 'lib/keeper_secrets_manager/storage.rb', line 233

def delete(key)
  key_str = key.to_s
  @base_storage.delete(key)
  @cache.delete(key_str)
  @timestamps.delete(key_str)
end

#get_string(key) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/keeper_secrets_manager/storage.rb', line 210

def get_string(key)
  key_str = key.to_s

  # Check cache validity
  return @cache[key_str] if @cache.key?(key_str) && !expired?(key_str)

  # Fetch from base storage
  value = @base_storage.get_string(key)
  if value
    @cache[key_str] = value
    @timestamps[key_str] = Time.now
  end

  value
end

#save_string(key, value) ⇒ Object



226
227
228
229
230
231
# File 'lib/keeper_secrets_manager/storage.rb', line 226

def save_string(key, value)
  key_str = key.to_s
  @base_storage.save_string(key, value)
  @cache[key_str] = value.to_s
  @timestamps[key_str] = Time.now
end