Class: ToggleCraft::Cache

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

Overview

Cache manager with TTL support and multiple adapters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter: :memory, ttl: 300) ⇒ Cache

Initialize the cache

Parameters:

  • adapter (Symbol, Object) (defaults to: :memory)

    The cache adapter (:memory or custom adapter instance)

  • ttl (Integer) (defaults to: 300)

    Default TTL in seconds (default: 300 = 5 minutes)



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

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/togglecraft/cache.rb', line 8

def adapter
  @adapter
end

#default_ttlObject (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_flagsHash

Get all cached flags (non-expired)

Returns:

  • (Hash)

    Hash of flag_key => flag_data



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

#clearObject

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

Parameters:

  • prefix (String)

    The prefix to match



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

Parameters:

  • key (String)

    The cache key

Returns:

  • (Boolean)

    true if deleted, false otherwise



53
54
55
# File 'lib/togglecraft/cache.rb', line 53

def delete(key)
  @adapter.delete(key)
end

#destroyObject

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

Parameters:

  • key (String)

    The cache key

Returns:

  • (Object, nil)

    The cached value or nil



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)

Parameters:

  • key (String)

    The cache key

Returns:

  • (Boolean)


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

Parameters:

  • key (String)

    The cache key

  • value (Object)

    The value to cache

  • ttl (Integer, nil) (defaults to: nil)

    Optional TTL override in seconds



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

Parameters:

  • flags (Hash)

    Hash of flag_key => flag_data

  • ttl (Integer, nil) (defaults to: nil)

    Optional TTL override



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