Class: DiscordRDA::MemoryStore

Inherits:
CacheStore
  • Object
show all
Defined in:
lib/discord_rda/cache/memory_store.rb

Overview

In-memory LRU cache store. Provides fast, thread-safe caching with size limits and TTL.

Constant Summary collapse

DEFAULT_SIZE =

Default cache size

10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size: DEFAULT_SIZE) ⇒ MemoryStore

Initialize memory store

Parameters:

  • max_size (Integer) (defaults to: DEFAULT_SIZE)

    Maximum cache size



21
22
23
24
25
26
# File 'lib/discord_rda/cache/memory_store.rb', line 21

def initialize(max_size: DEFAULT_SIZE)
  @max_size = max_size
  @cache = LruRedux::Cache.new(max_size)
  @ttl_data = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#max_sizeInteger (readonly)

Returns Maximum cache size.

Returns:

  • (Integer)

    Maximum cache size



14
15
16
# File 'lib/discord_rda/cache/memory_store.rb', line 14

def max_size
  @max_size
end

#ttl_dataHash (readonly)

Returns TTL tracking.

Returns:

  • (Hash)

    TTL tracking



17
18
19
# File 'lib/discord_rda/cache/memory_store.rb', line 17

def ttl_data
  @ttl_data
end

Instance Method Details

#clearvoid

This method returns an undefined value.

Clear all cached values



72
73
74
75
76
77
# File 'lib/discord_rda/cache/memory_store.rb', line 72

def clear
  @mutex.synchronize do
    @cache.clear
    @ttl_data.clear
  end
end

#delete(key) ⇒ void

This method returns an undefined value.

Delete a value from cache

Parameters:

  • key (String)

    Cache key



53
54
55
56
57
58
# File 'lib/discord_rda/cache/memory_store.rb', line 53

def delete(key)
  @mutex.synchronize do
    @cache.delete(key)
    @ttl_data.delete(key)
  end
end

#exist?(key) ⇒ Boolean

Check if key exists and is not expired

Parameters:

  • key (String)

    Cache key

Returns:

  • (Boolean)

    True if exists



63
64
65
66
67
68
# File 'lib/discord_rda/cache/memory_store.rb', line 63

def exist?(key)
  @mutex.synchronize do
    check_ttl(key)
    @cache.key?(key)
  end
end

#get(key) ⇒ Object?

Get a value from cache

Parameters:

  • key (String)

    Cache key

Returns:

  • (Object, nil)

    Cached value or nil if expired/missing



31
32
33
34
35
36
# File 'lib/discord_rda/cache/memory_store.rb', line 31

def get(key)
  @mutex.synchronize do
    check_ttl(key)
    @cache[key]
  end
end

#keys(pattern) ⇒ Array<String>

Get keys matching a pattern

Parameters:

  • pattern (String, Regexp)

    Pattern to match (supports globs like “member:123:*”)

Returns:

  • (Array<String>)

    Matching keys



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/discord_rda/cache/memory_store.rb', line 104

def keys(pattern)
  @mutex.synchronize do
    clean_expired

    regex = if pattern.is_a?(Regexp)
              pattern
            else
              # Convert glob pattern to regex
              regex_str = pattern.gsub('.', '\\.')
                               .gsub('*', '.*')
                               .gsub('?', '.')
              Regexp.new("^#{regex_str}$")
            end

    @cache.keys.select { |k| k.match?(regex) }
  end
end

#set(key, value, ttl: nil) ⇒ void

This method returns an undefined value.

Set a value in cache

Parameters:

  • key (String)

    Cache key

  • value (Object)

    Value to cache

  • ttl (Integer) (defaults to: nil)

    Time to live in seconds



43
44
45
46
47
48
# File 'lib/discord_rda/cache/memory_store.rb', line 43

def set(key, value, ttl: nil)
  @mutex.synchronize do
    @cache[key] = value
    @ttl_data[key] = Time.now.utc + ttl if ttl
  end
end

#sizeInteger

Get current cache size

Returns:

  • (Integer)

    Number of cached items



81
82
83
84
85
86
# File 'lib/discord_rda/cache/memory_store.rb', line 81

def size
  @mutex.synchronize do
    clean_expired
    @cache.size
  end
end

#statsHash

Get cache statistics

Returns:

  • (Hash)

    Statistics



90
91
92
93
94
95
96
97
98
99
# File 'lib/discord_rda/cache/memory_store.rb', line 90

def stats
  @mutex.synchronize do
    clean_expired
    {
      size: @cache.size,
      max_size: @max_size,
      ttl_entries: @ttl_data.size
    }
  end
end