simple_mem_cache v0.1.1 SimpleMemCache

In-memory key-value cache with expiration-time after creation/modification/access, automatic value loading and time travel support.

Summary

Types

The table id (Tid) or table name

Functions

Get status and value of cached item. Status can be :ok, :expired or :not_cached

Get value of cached item. Nil if is not cached or when value is nil

returns function that generates Unix/Posix UTC time in seconds

Create or update an item in cache

Remove an item from cache. Returns the value of the removed object

Remove expired entries. This is automatically called once a minute during SimpleMemCache usage

Delete the ets table

Types

table()
table() :: number | atom

The table id (Tid) or table name

Functions

cache(table, key, minutes_valid \\ nil, keep_alive \\ false, f_new_value)
cache(table, Map.key, integer | nil, boolean | nil, (() -> any)) :: any

Cache value returned by the supplied function.

  • table - Name of the ets table.
  • key - Key name
  • minutes_valid - Minutes to keep the item in cache. Default: nil - do not expire.
  • keep_alive - Keep the item in cache if it still accessed. It expires if it is not retrieved in minutes_valid minutes. Default: false
  • f_new_value - function that supplies the value. Enables automatic value loading. When minutes_valid is not nil and keep_alive is false, this function can be launched in a new Erlang process, and it can do this proactively up to 30 seconds before expiration.

Examples

products = SimpleMemCache.cache(SimpleMemCache, "products", 30, true,
   fn() -> File.read!(filename) end
)

news_page = SimpleMemCache.cache(SimpleMemCache, "news", 10, &scrape_news/0)
get(table, key, minutes_keep_alive \\ nil)
get(table, Map.key, integer | nil) :: {term, any}

Get status and value of cached item. Status can be :ok, :expired or :not_cached

The minutes_keep_alive parameter is the number of minutes to keep the item at least in cache. Does not shorten a previously set expiration time (use put for that). However, if there wasn’t an expiration time it will take the new value. Default: nil - do not change the expire time.

Example

iex(1)> products = SimpleMemCache.get(SimpleMemCache, "products", 20)
{:expired, "Fret dots"}
get!(table, key, minutes_keep_alive \\ nil)
get!(table, Map.key, integer | nil) :: any

Get value of cached item. Nil if is not cached or when value is nil.

get_system_time_function(table)

returns function that generates Unix/Posix UTC time in seconds.

put(table, key, minutes_valid \\ nil, value)
put(table, Map.key, integer | nil, Map.value) :: any

Create or update an item in cache.

remove(table, key)
remove(table, Map.key) :: any

Remove an item from cache. Returns the value of the removed object.

remove_expired_entries(table)
remove_expired_entries(table) :: :ok

Remove expired entries. This is automatically called once a minute during SimpleMemCache usage.

set_system_time_function(table, f_system_time \\ nil)
set_system_time_function(table, (() -> integer) | nil) :: :ok

Time travel for SimpleMemCache.

The f_system_time parameter is meant for time travel support. You can provide a function that returns the Unix/Posix UTC time in seconds. Set nil to restore the normal system time.

stop(table)
stop(table) :: :ok

Delete the ets table