View Source depcache (depcache v2.0.0)

Depcache API

depcache API

flush/1, flush/2, get/2, get/3, get_subkey/3, get_wait/2, set/3, set/4, set/5, size/1
memo/2, memo/3, memo/4, memo/5
flush_process_dict/0, in_process/1, in_process_server/1

Internal

cleanup/1, cleanup/5

Summary

Functions

Cleanup process for the depcache. Periodically checks a batch of depcache items for their validity. Asks the depcache server to delete invalidated items. When the load of the data table is too high then This cleanup process starts to delete random entries. By using a random delete we don't need to keep a LRU list, which is a bit expensive.
Flush all keys from the caches
See also:gen_server:call/2.
Flush the key and all keys depending on that key
See also:gen_server:call/2.
Flush all items memoized in the process dictionary.
Fetch the key from the cache, return the data or an undefined if not found (or not valid).
See also:gen_server:call/2.
Fetch the key from the cache, return the data or an undefined if not found (or not valid)
See also:gen_server:call/2.
Fetch the key from the cache, return the data or an undefined if not found (or not valid)
See also:gen_server:call/2.
Fetch the key from the cache, when the key does not exist then lock the entry and let the calling process insert the value. All other processes requesting the key will wait till the key is updated and receive the key's new value.
See also:gen_server:call/3.
Enable or disable the in-process caching using the process dictionary
Check if we use a local process dict cache.
Cache the result of the function for an hour.
If Fun is a function then cache for an hour given the key. If Fun is a {M,F,A} tuple then derive the key from the tuple and cache for MaxAge seconds.
Cache the result of the function as Key for MaxAge seconds, flush the cached result if any of the dependencies is changed.
Add the key to the depcache, hold it for MaxAge seconds and check the dependencies.
Return the total memory size of all stored terms

Start a depcache process.
See also:gen_server:start_link/3.

Start a named depcache process.
See also:gen_server:start_link/4.

Types

-type callback() :: mfargs().
-type cleanup_state() ::
    #cleanup_state{pid :: pid(),
                   tables :: tables(),
                   name :: atom(),
                   memory_max :: non_neg_integer(),
                   callback :: callback() | undefined}.
-type config() :: config_map() | config_proplist().
-type config_map() :: #{memory_max => non_neg_integer() | undefined, callback => callback() | undefined}.
-type config_proplist() ::
    [{memory_max, non_neg_integer | undefined} | {callback, callback() | undefined}].
-type depcache_server() :: pid() | atom().
-type depend() :: #depend{key :: key(), serial :: non_neg_integer()}.
-type dependencies() :: [key()].
-type key() :: any().
-type max_age_secs() :: sec().
-type memo_fun() :: function() | mfargs() | {module(), atom()}.
-type meta() ::
    #meta{key :: key(), expire :: sec(), serial :: non_neg_integer(), depend :: dependencies()}.
-type mfargs() :: {module(), atom(), list()}.
-type proplist() :: proplists:proplist().
-type sec() :: non_neg_integer().
-type state() ::
    #state{now :: sec(),
           serial :: non_neg_integer(),
           tables :: tables(),
           wait_pids :: map(),
           writers :: map()}.
-type tables() :: #tables{meta_table :: ets:tab(), deps_table :: ets:tab(), data_table :: ets:tab()}.

Functions

-spec cleanup(CleanUp_state) -> Result when CleanUp_state :: cleanup_state(), Result :: no_return().

Equivalent to cleanup(CleanUp_state, SlotNr, Now, Mode, Ct).

Cleanup process for the depcache. Periodically checks a batch of depcache items for their validity. Asks the depcache server to delete invalidated items. When the load of the data table is too high then This cleanup process starts to delete random entries. By using a random delete we don't need to keep a LRU list, which is a bit expensive.
Link to this function

cleanup(State, SlotNr, Now, Mode, Ct)

View Source
-spec cleanup(State, SlotNr, Now, Mode, Ct) -> Result
           when
               State :: cleanup_state(),
               SlotNr :: '$end_of_table' | non_neg_integer(),
               Now :: sec(),
               Mode :: normal | cache_full,
               Ct :: integer(),
               Result :: no_return().
Cleanup process for the depcache. Periodically checks a batch of depcache items for their validity. Asks the depcache server to delete invalidated items. When the load of the data table is too high then This cleanup process starts to delete random entries. By using a random delete we don't need to keep a LRU list, which is a bit expensive.
-spec flush(Server) -> Result when Server :: depcache_server(), Result :: ok.
Flush all keys from the caches
See also:gen_server:call/2.
-spec flush(Key, Server) -> Result when Key :: key(), Server :: depcache_server(), Result :: ok.
Flush the key and all keys depending on that key
See also:gen_server:call/2.
-spec flush_process_dict() -> Result when Result :: ok.
Flush all items memoized in the process dictionary.
-spec get(Key, Server) -> Result
       when Key :: key(), Server :: depcache_server(), Result :: {ok, any()} | undefined.
Fetch the key from the cache, return the data or an undefined if not found (or not valid).
See also:gen_server:call/2.
Link to this function

get(Key, SubKey, Server)

View Source
-spec get(Key, SubKey, Server) -> Result
       when
           Key :: key(),
           SubKey :: key(),
           Server :: depcache_server(),
           Result :: {ok, any()} | undefined.
Fetch the key from the cache, return the data or an undefined if not found (or not valid)
See also:gen_server:call/2.
Link to this function

get_subkey(Key, SubKey, Server)

View Source
-spec get_subkey(Key, SubKey, Server) -> Result
              when
                  Key :: key(),
                  SubKey :: key(),
                  Server :: depcache_server(),
                  Result :: {ok, any()} | undefined.
Fetch the key from the cache, return the data or an undefined if not found (or not valid)
See also:gen_server:call/2.
-spec get_wait(Key, Server) -> Result
            when
                Key :: key(),
                Server :: depcache_server(),
                Result :: {ok, any()} | undefined | {throw, term()} | {error, premature_exit}.
Fetch the key from the cache, when the key does not exist then lock the entry and let the calling process insert the value. All other processes requesting the key will wait till the key is updated and receive the key's new value.
See also:gen_server:call/3.
-spec in_process(IsChaching) -> Result
              when IsChaching :: undefined | boolean(), Result :: undefined | boolean().
Enable or disable the in-process caching using the process dictionary
Link to this function

in_process_server(Server)

View Source
-spec in_process_server(Server) -> Result when Server :: depcache_server(), Result :: boolean().
Check if we use a local process dict cache.
-spec memo(Fun, Server) -> Result when Fun :: memo_fun(), Server :: depcache_server(), Result :: any().
Cache the result of the function for an hour.
Link to this function

memo(Fun, MaxAge_Key, Server)

View Source
-spec memo(Fun, MaxAge_Key, Server) -> Result
        when
            Fun :: memo_fun(),
            MaxAge_Key :: MaxAge | Key,
            MaxAge :: max_age_secs(),
            Key :: key(),
            Server :: depcache_server(),
            Result :: any().
If Fun is a function then cache for an hour given the key. If Fun is a {M,F,A} tuple then derive the key from the tuple and cache for MaxAge seconds.
Link to this function

memo(Fun, Key, MaxAge, Server)

View Source
-spec memo(Fun, Key, MaxAge, Server) -> Result
        when
            Fun :: memo_fun(),
            Key :: key(),
            MaxAge :: max_age_secs(),
            Server :: depcache_server(),
            Result :: any().

Equivalent to memo(Fun, Key, MaxAge, [], Server).

Cache the result of the function as Key for MaxAge seconds.
Link to this function

memo(Fun, Key, MaxAge, Dep, Server)

View Source
-spec memo(Fun, Key, MaxAge, Dep, Server) -> Result
        when
            Fun :: memo_fun(),
            Key :: undefined | key(),
            MaxAge :: max_age_secs(),
            Dep :: dependencies(),
            Server :: depcache_server(),
            Result :: any().
Cache the result of the function as Key for MaxAge seconds, flush the cached result if any of the dependencies is changed.
-spec set(Key, Data, Server) -> Result
       when Key :: key(), Data :: any(), Server :: depcache_server(), Result :: ok.

Equivalent to set(Key, Data, 3600, [], Server).

Add the key to the depcache, hold it for 3600 seconds and no dependencies.
Link to this function

set(Key, Data, MaxAge, Server)

View Source
-spec set(Key, Data, MaxAge, Server) -> Result
       when
           Key :: key(),
           Data :: any(),
           MaxAge :: max_age_secs(),
           Server :: depcache_server(),
           Result :: ok.

Equivalent to set(Key, Data, MaxAge, [], Server).

Add the key to the depcache, hold it for MaxAge seconds and no dependencies.
Link to this function

set(Key, Data, MaxAge, Depend, Server)

View Source
-spec set(Key, Data, MaxAge, Depend, Server) -> Result
       when
           Key :: key(),
           Data :: any(),
           MaxAge :: max_age_secs(),
           Depend :: dependencies(),
           Server :: depcache_server(),
           Result :: ok.
Add the key to the depcache, hold it for MaxAge seconds and check the dependencies.
-spec size(Server) -> Result when Server :: depcache_server(), Result :: non_neg_integer() | undefined.
Return the total memory size of all stored terms
-spec start_link(Config) -> Result
              when Config :: config(), Result :: {ok, pid()} | ignore | {error, term()}.

Start a depcache process.
See also:gen_server:start_link/3.

For Config, you can pass:
callback => {Module, Function, Arguments}
depcache event callback
memory_max => MaxMemoryInMB
number of MB to limit depcache size at
Link to this function

start_link(Name, Config)

View Source
-spec start_link(Name, Config) -> Result
              when
                  Name :: atom(),
                  Config :: config(),
                  Result :: {ok, pid()} | ignore | {error, term()}.

Start a named depcache process.
See also:gen_server:start_link/4.

For Config, you can pass:
callback => {Module, Function, Arguments}
depcache event callback
memory_max => MaxMemoryInMB
number of MB to limit depcache size at