Class: Blocks::Sdk::Cache::MemoryCache
- Inherits:
-
Object
- Object
- Blocks::Sdk::Cache::MemoryCache
- Defined in:
- lib/blocks/sdk/cache/memory_cache.rb
Overview
Thread-safe in-memory cache for Blocks Service data
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all cache.
-
#delete(key) ⇒ Object
Delete a specific key from cache.
-
#exists?(key) ⇒ Boolean
Check if a key exists in cache.
-
#get(key) ⇒ Object?
Get cached value for a key.
-
#initialize ⇒ MemoryCache
constructor
A new instance of MemoryCache.
-
#invalidate(service:, module_name:, language: nil) ⇒ Object
Invalidate cache for a service/module/language combination.
-
#keys ⇒ Object
Get all cache keys (useful for debugging).
-
#set(key, value) ⇒ Object
Set a value in cache.
Constructor Details
#initialize ⇒ MemoryCache
Returns a new instance of MemoryCache.
8 9 10 11 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 8 def initialize @cache = {} @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
Clear all cache
61 62 63 64 65 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 61 def clear @mutex.synchronize do @cache.clear end end |
#delete(key) ⇒ Object
Delete a specific key from cache
33 34 35 36 37 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 33 def delete(key) @mutex.synchronize do @cache.delete(key) end end |
#exists?(key) ⇒ Boolean
Check if a key exists in cache
77 78 79 80 81 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 77 def exists?(key) @mutex.synchronize do @cache.key?(key) end end |
#get(key) ⇒ Object?
Get cached value for a key
16 17 18 19 20 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 16 def get(key) @mutex.synchronize do @cache[key] end end |
#invalidate(service:, module_name:, language: nil) ⇒ Object
Invalidate cache for a service/module/language combination
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 43 def invalidate(service:, module_name:, language: nil) @mutex.synchronize do if language # Invalidate specific language key = build_key(service, module_name, language) @cache.delete(key) else # Invalidate all languages for this module @cache.keys.each do |key| if key.start_with?("#{service}:#{module_name}:") @cache.delete(key) end end end end end |
#keys ⇒ Object
Get all cache keys (useful for debugging)
68 69 70 71 72 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 68 def keys @mutex.synchronize do @cache.keys.dup end end |
#set(key, value) ⇒ Object
Set a value in cache
25 26 27 28 29 |
# File 'lib/blocks/sdk/cache/memory_cache.rb', line 25 def set(key, value) @mutex.synchronize do @cache[key] = value end end |