DCache (dcache v0.0.1)
Link to this section Summary
Functions
Creates a module to wrap cache operations. This is the preferred way to create a cache as it performs better.
Delets the value from the cache, safe to call even if the key is not in the cache
Same as fetch, but unwraps the returned value or raises on error.
Gets the value from the cache. Executes fun/1 if the value is not found.
fun/1 receives the cache key being looked up and should return one of
Gets the value from the cache, returning nil if not found or expired
Puts the value in the cache. TTL is a relative time in second. For example, 300 would mean that the value would expire in 5 minutes
Creates the cache opts
Returns the total number of items in the cache, including expired items. This is O(N) over the number of segments
Gets the unix time in seconds when the value will be considered expired.
Rrturns nil if the value is not found. The return value can
be in the past
Link to this section Functions
Creates a module to wrap cache operations. This is the preferred way to create a cache as it performs better.
defmodule App.Cache do
require DCache
DCache.define(Users, 100_000) # creates an App.Cache.Users module
endopts:
cache: Module
The name of the module to create. This will be nested within the calling module
max: integer
The maximum number of items to hold in the cache
opts:
segments: integer
The number of segments to create (defaults to 10)
del(cache, key)
Delets the value from the cache, safe to call even if the key is not in the cache
fetch!(cache, key, fun, ttl \\ nil)
Same as fetch, but unwraps the returned value or raises on error.
fetch(cache, key, fun, ttl \\ nil)
Gets the value from the cache. Executes fun/1 if the value is not found.
fun/1 receives the cache key being looked up and should return one of:
* `{:ok, value}`
* `{:ok, value, ttl}`
* `{:skip, value}`
* `{:error, term}`
get(cache, key)
Gets the value from the cache, returning nil if not found or expired
put(cache, key, value, ttl)
Puts the value in the cache. TTL is a relative time in second. For example, 300 would mean that the value would expire in 5 minutes
setup(cache, max, opts \\ [])
Creates the cache opts:
cache: atom
The name of the cache
max: integer
The maximum number of items to hold in the cache
opts:
segments: integer
The number of segments to create (defaults to 10)
size(cache)
Returns the total number of items in the cache, including expired items. This is O(N) over the number of segments
ttl(cache, key)
Gets the unix time in seconds when the value will be considered expired.
Rrturns nil if the value is not found. The return value can
be in the past