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



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

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

Instance Method Details

#clear_cacheObject



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

def clear_cache
  @cache.clear
  @timestamps.clear
end

#delete(key) ⇒ Object



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

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



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

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



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

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