Sovite.Abuse.Cache (sovite v0.6.0)

Copy Markdown View Source

A small in-memory cache with per-entry expiry, for example to remember clients that passed anti-abuse checks.

children = [
  {Sovite.Abuse.Cache, name: MyApp.PassedClients, max_entries: 50_000}
]

Cache.put(MyApp.PassedClients, ip, :passed, 86_400_000)
Cache.get(MyApp.PassedClients, ip)

Callers read and write an ETS table directly and never wait on the process, which only owns the table and purges expired entries. Once the cache holds :max_entries entries, new keys are refused until some expire, so whoever fills it cannot exhaust memory. Expired entries count until the next purge. State is in memory: a restart empties the cache.

Options

  • :name - an atom, also used as the ETS table name. Required.
  • :max_entries - defaults to 100_000.
  • :cleanup_interval - milliseconds between purges of expired entries. Defaults to one minute.

Summary

Functions

Removes key.

Returns the value stored under key, unless it has expired.

Stores value under key for ttl milliseconds. Returns :full, and stores nothing, if key is new and the cache is full.

Returns the number of entries, including expired ones not yet purged.

Starts the cache.

Functions

delete(name, key)

@spec delete(atom(), term()) :: :ok

Removes key.

get(name, key)

@spec get(atom(), term()) :: {:ok, term()} | :error

Returns the value stored under key, unless it has expired.

put(name, key, value, ttl)

@spec put(atom(), term(), term(), non_neg_integer()) :: :ok | :full

Stores value under key for ttl milliseconds. Returns :full, and stores nothing, if key is new and the cache is full.

size(name)

@spec size(atom()) :: non_neg_integer()

Returns the number of entries, including expired ones not yet purged.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the cache.