Class: Parse::AtlasSearch::Session::MemoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/atlas_search/session.rb

Overview

Default cache: in-memory hash with per-entry TTL, guarded by a Mutex. Suitable for single-process apps. Apps running multi-process (Puma workers, Sidekiq processes) get a per- process cache — install a shared cache through Parse::AtlasSearch.session_cache= for cross-process sharing.

Instance Method Summary collapse

Constructor Details

#initializeMemoryCache

Returns a new instance of MemoryCache.



58
59
60
61
# File 'lib/parse/atlas_search/session.rb', line 58

def initialize
  @data = {}
  @mutex = Mutex.new
end

Instance Method Details

#clearObject

Drop every entry. Used by Parse::AtlasSearch::Session.reset_caches! and by tests that need a clean slate.



95
96
97
# File 'lib/parse/atlas_search/session.rb', line 95

def clear
  @mutex.synchronize { @data.clear }
end

#get(key) ⇒ Object?

Returns the cached value, or nil when the key is missing or its TTL has elapsed. Expired entries are evicted lazily on read.

Parameters:

Returns:

  • (Object, nil)

    the cached value, or nil when the key is missing or its TTL has elapsed. Expired entries are evicted lazily on read.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/parse/atlas_search/session.rb', line 67

def get(key)
  @mutex.synchronize do
    entry = @data[key]
    return nil if entry.nil?
    if entry[:expires_at] < Time.now
      @data.delete(key)
      return nil
    end
    entry[:value]
  end
end

#invalidate(key) ⇒ Object

Parameters:

  • key (String)

    cache key to forget.



89
90
91
# File 'lib/parse/atlas_search/session.rb', line 89

def invalidate(key)
  @mutex.synchronize { @data.delete(key) }
end

#set(key, value, ttl:) ⇒ Object

Parameters:

  • key (String)
  • value (Object)
  • ttl (Numeric)

    seconds until the entry expires.



82
83
84
85
86
# File 'lib/parse/atlas_search/session.rb', line 82

def set(key, value, ttl:)
  @mutex.synchronize do
    @data[key] = { value: value, expires_at: Time.now + ttl }
  end
end