Class: Tina4::CacheBackends::BaseBackend

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

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.

Returns:

  • (Boolean)


72
73
74
# File 'lib/tina4/cache_backends/base_backend.rb', line 72

def available?
  true
end

#clearObject

Remove all entries owned by this cache.

Raises:

  • (NotImplementedError)


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.

Parameters:

  • key (String)

Returns:

  • (Boolean)

    true if the key existed

Raises:

  • (NotImplementedError)


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.

Parameters:

  • key (String)

Returns:

  • (Object, nil)

    cached value or nil on miss/expiry

Raises:

  • (NotImplementedError)


16
17
18
# File 'lib/tina4/cache_backends/base_backend.rb', line 16

def get(_key)
  raise NotImplementedError
end

#nameString

Returns backend name reported in stats.

Returns:

  • (String)

    backend name reported in stats

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/tina4/cache_backends/base_backend.rb', line 44

def name
  raise NotImplementedError
end

#set(_key, _value, _ttl) ⇒ Object

Parameters:

  • key (String)
  • value (Object)
  • ttl (Integer)

    seconds; <= 0 means no expiry

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/tina4/cache_backends/base_backend.rb', line 23

def set(_key, _value, _ttl)
  raise NotImplementedError
end

#statsHash

Returns { hits:, misses:, size:, backend: }.

Returns:

  • (Hash)

    { hits:, misses:, size:, backend: }

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/tina4/cache_backends/base_backend.rb', line 39

def stats
  raise NotImplementedError
end

#sweepInteger

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.

Returns:

  • (Integer)

    number of entries evicted, never negative



63
64
65
# File 'lib/tina4/cache_backends/base_backend.rb', line 63

def sweep
  0
end