DCache (dcache v0.0.4)

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

Destroys the cache. Any call to the cache once destroy is called will raise an ArgumentError.

Iterates each segment name. fun will receive the segment name.

Gets the entry from the cache, or nil if not found. The entry will be returned even if it has expired. The entry is an internal representation that can change. You can use DCache.Entry.key/1, DCache.Entry.value/1 and DCache.Entry.ttl/1 to extract the key, value and ttl from an entry

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

Reduces each segment name. fun will receive the segment name (which is the name of an ETS table) plus the accumulator

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

Returns the time in second until the value expires. nil if the key isn't found. Can return a negative value if the item has expired but has not been purged yet

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`, `:none` 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

Destroys the cache. Any call to the cache once destroy is called will raise an ArgumentError.

Link to this function

each_segments(cache, fun)

Iterates each segment name. fun will receive the segment name.

Link to this function

entry(cache, key)

Gets the entry from the cache, or nil if not found. The entry will be returned even if it has expired. The entry is an internal representation that can change. You can use DCache.Entry.key/1, DCache.Entry.value/1 and DCache.Entry.ttl/1 to extract the key, value and ttl from an entry

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

reduce_segments(cache, acc, fun)

Reduces each segment name. fun will receive the segment name (which is the name of an ETS table) plus the accumulator

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

Link to this function

ttl(cache, key)

Returns the time in second until the value expires. nil if the key isn't found. Can return a negative value if the item has expired but has not been purged yet