Class: ToggleCraft::CacheAdapters::MemoryAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/togglecraft/cache_adapters/memory_adapter.rb

Overview

In-memory cache adapter using thread-safe Concurrent::Map Suitable for single-process applications

Instance Method Summary collapse

Constructor Details

#initializeMemoryAdapter

Returns a new instance of MemoryAdapter.



10
11
12
# File 'lib/togglecraft/cache_adapters/memory_adapter.rb', line 10

def initialize
  @store = Concurrent::Map.new
end

Instance Method Details

#clearObject

Clear all values from the store



36
37
38
# File 'lib/togglecraft/cache_adapters/memory_adapter.rb', line 36

def clear
  @store.clear
end

#delete(key) ⇒ Boolean

Delete a value from the store

Parameters:

  • key (String)

    The cache key

Returns:

  • (Boolean)

    true if deleted, false otherwise



31
32
33
# File 'lib/togglecraft/cache_adapters/memory_adapter.rb', line 31

def delete(key)
  !@store.delete(key).nil?
end

#get(key) ⇒ Object?

Get a value from the store

Parameters:

  • key (String)

    The cache key

Returns:

  • (Object, nil)

    The stored value or nil



17
18
19
# File 'lib/togglecraft/cache_adapters/memory_adapter.rb', line 17

def get(key)
  @store[key]
end

#has?(key) ⇒ Boolean

Check if a key exists in the store

Parameters:

  • key (String)

    The cache key

Returns:

  • (Boolean)


43
44
45
# File 'lib/togglecraft/cache_adapters/memory_adapter.rb', line 43

def has?(key)
  @store.key?(key)
end

#keysArray<String>

Get all keys in the store

Returns:

  • (Array<String>)


49
50
51
# File 'lib/togglecraft/cache_adapters/memory_adapter.rb', line 49

def keys
  @store.keys
end

#set(key, value) ⇒ Object

Set a value in the store

Parameters:

  • key (String)

    The cache key

  • value (Object)

    The value to store



24
25
26
# File 'lib/togglecraft/cache_adapters/memory_adapter.rb', line 24

def set(key, value)
  @store[key] = value
end