Class: Tina4::CacheBackends::BaseBackend
- Inherits:
-
Object
- Object
- Tina4::CacheBackends::BaseBackend
- Defined in:
- lib/tina4/cache_backends/base_backend.rb
Overview
Abstract cache backend (parity with Python _CacheBackend).
A backend is a key/value store with TTL semantics and stats. Values are arbitrary JSON-serialisable objects; backends round-trip them through JSON for the network/file/db tiers so a value stored by one process can be read by another. The factory (Tina4::CacheBackends.create_backend) picks the right implementation from env vars and falls back to the file backend when a configured network/driver backend is unreachable.
Direct Known Subclasses
DatabaseBackend, FileBackend, MemcachedBackend, MemoryBackend, MongoBackend, RedisBackend
Instance Method Summary collapse
-
#available? ⇒ Boolean
Whether this backend is actually usable (driver present + service reachable).
-
#clear ⇒ Object
Remove all entries owned by this cache.
-
#delete(_key) ⇒ Boolean
True if the key existed.
-
#get(_key) ⇒ Object?
Cached value or nil on miss/expiry.
-
#name ⇒ String
Backend name reported in stats.
- #set(_key, _value, _ttl) ⇒ Object
-
#stats ⇒ Hash
{ hits:, misses:, size:, backend: }.
-
#sweep ⇒ Integer
Evict expired entries and return HOW MANY were evicted.
Instance Method Details
#available? ⇒ Boolean
Whether this backend is actually usable (driver present + service reachable). Local backends are always available; network/driver backends override this so the factory can fall back to the file backend.
72 73 74 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 72 def available? true end |
#clear ⇒ Object
Remove all entries owned by this cache.
34 35 36 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 34 def clear raise NotImplementedError end |
#delete(_key) ⇒ Boolean
Returns true if the key existed.
29 30 31 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 29 def delete(_key) raise NotImplementedError end |
#get(_key) ⇒ Object?
Returns cached value or nil on miss/expiry.
16 17 18 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 16 def get(_key) raise NotImplementedError end |
#name ⇒ String
Returns backend name reported in stats.
44 45 46 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 44 def name raise NotImplementedError end |
#set(_key, _value, _ttl) ⇒ Object
23 24 25 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 23 def set(_key, _value, _ttl) raise NotImplementedError end |
#stats ⇒ Hash
Returns { hits:, misses:, size:, backend: }.
39 40 41 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 39 def stats raise NotImplementedError end |
#sweep ⇒ Integer
Evict expired entries and return HOW MANY were evicted.
The base answer is 0, and it is an honest one: redis, valkey, memcached and mongodb expire entries SERVER-SIDE, so by the time a sweep runs there is nothing left for this process to reclaim. Backends that hold entries locally - memory, file, database - override this with a real count.
It lives on the BASE so sweep is answerable on every provider. It used to exist only on the file backend, so sweep raised NoMethodError on the other six and every caller grew a respond_to?(:sweep) guard - and a guard cannot tell "not supported" from "evicted nothing", which are opposite readings for whoever is watching the number.
63 64 65 |
# File 'lib/tina4/cache_backends/base_backend.rb', line 63 def sweep 0 end |