DCache (dcache v0.0.3)

Link to this section Summary

Functions

Clears the cache. While the cache is being cleared, concurrent activity is severely limited.

Creates a module to wrap cache operations. This is the preferred way to create a cache as it performs better.

Deletes the value from the cache, safe to call even if the key is not in the cache

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

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

Deletes and removes the value from the cache. Returns nil if not found. Returns {:ok, {key, value, expires}} if found

Link to this section Functions

Clears the cache. While the cache is being cleared, concurrent activity is severely limited.

Link to this macro

define(cache, max, opts \\ [])

(macro)

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
    end

opts:

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 100, 10, 3 or 1) depending on `max`

    purger: symbol | function
        The purger to use, defaults to `:default`
        Supported values: `:default`, `:no_spawn`, `:blocking` or a custom function
Link to this function

del(cache, key)

Deletes the value from the cache, safe to call even if the key is not in the cache

Link to this function

expires(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

Link to this function

fetch!(cache, key, fun, ttl \\ nil)

Same as fetch, but unwraps the returned value or raises on error.

Link to this function

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}`
Link to this function

get(cache, key)

Gets the value from the cache, returning nil if not found or expired

Link to this function

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

Link to this function

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 100)

Returns the total number of items in the cache, including expired items. This is O(N) over the number of segments

Link to this function

take(cache, key)

Deletes and removes the value from the cache. Returns nil if not found. Returns {:ok, {key, value, expires}} if found