Class: Woods::Cache::SolidCacheStore

Inherits:
CacheStore show all
Defined in:
lib/woods/cache/solid_cache_store.rb

Overview

SolidCache-backed (or any ActiveSupport::Cache::Store) cache store.

Delegates to a Rails-compatible cache backend. Values are JSON-serialized to avoid Marshal dependency issues across Ruby versions. TTL is passed as ‘expires_in:` to the underlying cache.

Examples:

With SolidCache

store = SolidCacheStore.new(cache: SolidCache::Store.new, default_ttl: 3600)
store.write("ci:emb:abc", [0.1, 0.2], ttl: 86_400)
store.read("ci:emb:abc") # => [0.1, 0.2]

With Rails.cache (any backend)

store = SolidCacheStore.new(cache: Rails.cache)

Instance Method Summary collapse

Methods inherited from CacheStore

#fetch

Constructor Details

#initialize(cache:, default_ttl: nil) ⇒ SolidCacheStore

Returns a new instance of SolidCacheStore.

Parameters:

  • cache (ActiveSupport::Cache::Store)

    A SolidCache or compatible cache instance

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

    Default TTL in seconds (nil = no expiry)



25
26
27
28
29
# File 'lib/woods/cache/solid_cache_store.rb', line 25

def initialize(cache:, default_ttl: nil)
  super()
  @cache = cache
  @default_ttl = default_ttl
end

Instance Method Details

#clear(namespace: nil) ⇒ void

This method returns an undefined value.

Clear cached entries by namespace or all woods cache keys.

Uses ‘delete_matched` if the underlying cache supports it (Redis, Memcached). Falls back to a no-op if pattern deletion is not available (some backends like SolidCache don’t support wildcard deletion).

Parameters:

  • namespace (Symbol, nil) (defaults to: nil)

    Domain to clear, or nil for all cache keys



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/woods/cache/solid_cache_store.rb', line 95

def clear(namespace: nil)
  pattern = clear_pattern(namespace)

  unless @cache.respond_to?(:delete_matched)
    logger.warn("[Woods] Cache#clear(namespace: #{namespace.inspect}) is a no-op: " \
                "backend #{@cache.class} does not support delete_matched")
    return
  end

  @cache.delete_matched(pattern)
rescue StandardError => e
  logger.warn("[Woods] SolidCacheStore#clear failed: #{e.message}")
  nil
end

#delete(key) ⇒ void

This method returns an undefined value.

Delete a key from the cache.

Parameters:

  • key (String)

    Cache key



69
70
71
72
73
74
# File 'lib/woods/cache/solid_cache_store.rb', line 69

def delete(key)
  @cache.delete(key)
rescue StandardError => e
  logger.warn("[Woods] SolidCacheStore#delete failed for #{key}: #{e.message}")
  nil
end

#exist?(key) ⇒ Boolean

Check if a key exists in the cache.

Parameters:

  • key (String)

    Cache key

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/woods/cache/solid_cache_store.rb', line 80

def exist?(key)
  @cache.exist?(key)
rescue StandardError => e
  logger.warn("[Woods] SolidCacheStore#exist? failed for #{key}: #{e.message}")
  false
end

#read(key) ⇒ Object?

Read a value from the cache.

Parameters:

  • key (String)

    Cache key

Returns:

  • (Object, nil)

    Deserialized value or nil



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/woods/cache/solid_cache_store.rb', line 35

def read(key)
  raw = @cache.read(key)
  return nil unless raw

  JSON.parse(raw)
rescue JSON::ParserError
  delete_silently(key)
  nil
rescue StandardError => e
  logger.warn("[Woods] SolidCacheStore#read failed for #{key}: #{e.message}")
  nil
end

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

This method returns an undefined value.

Write a value with optional TTL.

Parameters:

  • key (String)

    Cache key

  • value (Object)

    Value to cache (must be JSON-serializable)

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

    TTL in seconds (falls back to default_ttl)



54
55
56
57
58
59
60
61
62
63
# File 'lib/woods/cache/solid_cache_store.rb', line 54

def write(key, value, ttl: nil)
  serialized = JSON.generate(value)
  effective_ttl = ttl || @default_ttl

  opts = effective_ttl ? { expires_in: effective_ttl } : {}
  @cache.write(key, serialized, **opts)
rescue StandardError => e
  logger.warn("[Woods] SolidCacheStore#write failed for #{key}: #{e.message}")
  nil
end