Class: ToggleCraft::Cache
- Inherits:
-
Object
- Object
- ToggleCraft::Cache
- Defined in:
- lib/togglecraft/cache.rb
Overview
Cache manager with TTL support and multiple adapters
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#default_ttl ⇒ Object
readonly
Returns the value of attribute default_ttl.
Instance Method Summary collapse
-
#all_flags ⇒ Hash
Get all cached flags (non-expired).
-
#clear ⇒ Object
Clear all cache entries.
-
#clear_by_prefix(prefix) ⇒ Object
Delete all keys matching a prefix.
-
#delete(key) ⇒ Boolean
Delete a value from cache.
-
#destroy ⇒ Object
Clean up resources and stop background cleanup.
-
#get(key) ⇒ Object?
Get a value from cache Returns nil if key doesn’t exist or is expired.
-
#has?(key) ⇒ Boolean
Check if a key exists in cache (and is not expired).
-
#initialize(adapter: :memory, ttl: 300) ⇒ Cache
constructor
Initialize the cache.
-
#set(key, value, ttl: nil) ⇒ Object
Set a value in cache with optional TTL override.
-
#set_all_flags(flags, ttl: nil) ⇒ Object
Store all flags at once.
Constructor Details
#initialize(adapter: :memory, ttl: 300) ⇒ Cache
Initialize the cache
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/togglecraft/cache.rb', line 13 def initialize(adapter: :memory, ttl: 300) @default_ttl = ttl @adapter = case adapter when :memory CacheAdapters::MemoryAdapter.new else adapter end @cleanup_thread = nil start_cleanup end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
8 9 10 |
# File 'lib/togglecraft/cache.rb', line 8 def adapter @adapter end |
#default_ttl ⇒ Object (readonly)
Returns the value of attribute default_ttl.
8 9 10 |
# File 'lib/togglecraft/cache.rb', line 8 def default_ttl @default_ttl end |
Instance Method Details
#all_flags ⇒ Hash
Get all cached flags (non-expired)
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/togglecraft/cache.rb', line 82 def all_flags flags = {} @adapter.keys.each do |key| next unless key.start_with?('flag:') flag_key = key.sub('flag:', '') value = get(key) flags[flag_key] = value if value end flags end |
#clear ⇒ Object
Clear all cache entries
58 59 60 |
# File 'lib/togglecraft/cache.rb', line 58 def clear @adapter.clear end |
#clear_by_prefix(prefix) ⇒ Object
Delete all keys matching a prefix
75 76 77 78 |
# File 'lib/togglecraft/cache.rb', line 75 def clear_by_prefix(prefix) keys_to_delete = @adapter.keys.select { |key| key.start_with?(prefix) } keys_to_delete.each { |key| delete(key) } end |
#delete(key) ⇒ Boolean
Delete a value from cache
53 54 55 |
# File 'lib/togglecraft/cache.rb', line 53 def delete(key) @adapter.delete(key) end |
#destroy ⇒ Object
Clean up resources and stop background cleanup
104 105 106 107 |
# File 'lib/togglecraft/cache.rb', line 104 def destroy stop_cleanup clear end |
#get(key) ⇒ Object?
Get a value from cache Returns nil if key doesn’t exist or is expired
29 30 31 32 33 34 35 |
# File 'lib/togglecraft/cache.rb', line 29 def get(key) entry = @adapter.get(key) return nil unless entry return nil if expired?(entry) entry[:value] end |
#has?(key) ⇒ Boolean
Check if a key exists in cache (and is not expired)
65 66 67 68 69 70 71 |
# File 'lib/togglecraft/cache.rb', line 65 def has?(key) entry = @adapter.get(key) return false unless entry return false if expired?(entry) true end |
#set(key, value, ttl: nil) ⇒ Object
Set a value in cache with optional TTL override
41 42 43 44 45 46 47 48 |
# File 'lib/togglecraft/cache.rb', line 41 def set(key, value, ttl: nil) entry = { value: value, timestamp: Time.now.to_f, ttl: ttl || @default_ttl } @adapter.set(key, entry) end |
#set_all_flags(flags, ttl: nil) ⇒ Object
Store all flags at once
97 98 99 100 101 |
# File 'lib/togglecraft/cache.rb', line 97 def set_all_flags(flags, ttl: nil) flags.each do |flag_key, flag_data| set("flag:#{flag_key}", flag_data, ttl: ttl) end end |