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
Functions
Cache value returned by the supplied function
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
Time travel for SimpleMemCache
Delete the ets table
Types
Functions
Cache value returned by the supplied function.
table
- Name of the ets table.key
- Key nameminutes_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 inminutes_valid
minutes. Default: falsef_new_value
- function that supplies the value. Enables automatic value loading. Whenminutes_valid
is not nil andkeep_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 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 value of cached item. Nil if is not cached or when value is nil.
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.
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.
Delete the ets table