Class: Snails::Cache

Inherits:
Object show all
Defined in:
runtime/snails/cache.rbs,
runtime/snails/cache.rb

Constant Summary collapse

MAX_ENTRIES =

Entry cap. Reaching it flushes the whole store: the bound keeps growth finite rather than hitting a byte target, and a count avoids measuring every stored value.

5000
MISSING =

Distinguishes "no entry" from a stored nil value.

Object.new

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Cache

Returns a new instance of Cache.



8
# File 'runtime/snails/cache.rbs', line 8

def initialize: () -> void

Instance Method Details

#clear ⇒ void

remove every entry. The harness calls this before each case so cache values and rate-limit windows do not leak across tests.



150
# File 'runtime/snails/cache.rb', line 150

def clear: () -> void

#delete(key) ⇒ Boolean

remove one entry.

Parameters:

  • key (String) —

    the cache key

Returns:

  • (Boolean) —

    true when a live entry was removed; an expired entry counts as absent and is purged



93
# File 'runtime/snails/cache.rb', line 93

def delete: (untyped key) -> bool

#exist?(key) ⇒ Boolean

whether a live entry is stored.

Parameters:

  • key (String) —

    the cache key

Returns:

  • (Boolean) —

    false when missing or expired; the expired entry is purged



111
# File 'runtime/snails/cache.rb', line 111

def exist?: (untyped key) -> bool

#fetch(key, expires_in: nil) ⇒ Object

return the cached value, or run the block and store its result. A miss and a stored nil are different: the hit test uses an internal sentinel, so a nil-valued entry is not recomputed.

Examples:

Snails.cache.fetch("reports/#{id}", expires_in: 60) { build_report(id) }

Parameters:

Returns:

  • (Object) —

    the stored value, or the block's result on a miss

Raises:

  • (ArgumentError) —

    the block is missing or expires_in is not positive



53
# File 'runtime/snails/cache.rb', line 53

def fetch: (untyped key, ?expires_in: untyped) { () -> untyped } -> untyped

#increment(key, amount = 1, expires_in: nil) ⇒ Integer

add to a counter. The first write in a window starts it: a new entry is stored with expires_in, and later increments keep that expiry (a rate-limit window). A non-integer value is read through to_i.

Parameters:

  • key (String) —

    the cache key

  • amount (Integer) (defaults to: 1) —

    the increase

  • expires_in (Integer, Snails::Duration, nil) (defaults to: nil) —

    seconds until expiry for a new entry; nil never expires

Returns:

  • (Integer) —

    the value after the increase

Raises:

  • (ArgumentError) —

    amount is not an Integer or expires_in is not positive



126
# File 'runtime/snails/cache.rb', line 126

def increment: (untyped key, ?Integer amount, ?expires_in: untyped) -> Integer

#read(key) ⇒ Object?

the stored value, or nil on a miss or after expiry.

Parameters:

  • key (String) —

    the cache key

Returns:



70
# File 'runtime/snails/cache.rb', line 70

def read: (untyped key) -> untyped

#write(key, value, expires_in: nil) ⇒ Boolean

store a value.

Parameters:

  • key (String) —

    the cache key

  • value (Object) —

    the value to store; nil is a valid entry

  • expires_in (Integer, Snails::Duration, nil) (defaults to: nil) —

    seconds until expiry; nil never expires

Returns:

  • (Boolean) —

    true when the value was stored

Raises:

  • (ArgumentError) —

    expires_in is not positive



83
# File 'runtime/snails/cache.rb', line 83

def write: (untyped key, untyped value, ?expires_in: untyped) -> bool