Class: Dbviewer::Cache::InMemory

Inherits:
Base
  • Object
show all
Defined in:
lib/dbviewer/cache/in_memory.rb

Overview

InMemory cache storage for Dbviewer It provides an abstraction layer for managing caches efficiently

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Dbviewer::Cache::Base

Instance Method Details

#delete(key) ⇒ Object?

Delete a specific cache entry by key

Parameters:

  • key (String)

    Cache key to delete

Returns:

  • (Object, nil)

    The deleted value or nil if not found



28
29
30
31
# File 'lib/dbviewer/cache/in_memory.rb', line 28

def delete(key)
  deleted_entry = @unified_cache.delete(key)
  deleted_entry&.fetch(:value)
end

#fetch(key, options = {}) { ... } ⇒ Object

Fetch data from cache or execute block if not found/expired

Parameters:

  • key (String)

    Cache key

  • options (Hash) (defaults to: {})

    Options for the cache entry

Options Hash (options):

  • :expires_in (Integer)

    Custom expiry time in seconds

Yields:

  • Block to execute if cache miss or expired

Returns:

  • (Object)

    Cached value or result of block execution



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dbviewer/cache/in_memory.rb', line 12

def fetch(key, options = {}, &block)
  cache_entry = @unified_cache[key]
  custom_expiry = options[:expires_in] || @cache_expiry
  return cache_entry[:value] if cache_entry && !cache_expired?(cache_entry, custom_expiry)

  result = block.call
  @unified_cache[key] = {
    value: result,
    created_at: Time.now
  }
  result
end