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

Flush the key and all keys depending on that key

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

Fetch the key from the cache, return the data or an undefined if not found (or not valid)

Fetch the key from the cache, return the data or an undefined if not found (or not valid)

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.

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

callback/0

-type callback() :: mfargs().

cleanup_state/0

-type cleanup_state() ::
          #cleanup_state{pid :: pid(),
                         tables :: tables(),
                         name :: atom(),
                         memory_max :: non_neg_integer(),
                         callback :: callback() | undefined}.

config/0

-type config() :: config_map() | config_proplist().

config_map/0

-type config_map() :: #{memory_max => non_neg_integer() | undefined, callback => callback() | undefined}.

config_proplist/0

-type config_proplist() ::
          [{memory_max, non_neg_integer | undefined} | {callback, callback() | undefined}].

depcache_server/0

-type depcache_server() :: pid() | atom().

depend/0

-type depend() :: #depend{key :: key(), serial :: non_neg_integer()}.

dependencies/0

-type dependencies() :: [key()].

key/0

-type key() :: any().

max_age_secs/0

-type max_age_secs() :: sec().

memo_fun/0

-type memo_fun() :: function() | mfargs() | {module(), atom()}.

meta/0

-type meta() ::
          #meta{key :: key(), expire :: sec(), serial :: non_neg_integer(), depend :: dependencies()}.

mfargs/0

-type mfargs() :: {module(), atom(), list()}.

proplist/0

-type proplist() :: proplists:proplist().

sec/0

-type sec() :: non_neg_integer().

state/0

-type state() ::
          #state{now :: sec(),
                 serial :: non_neg_integer(),
                 tables :: tables(),
                 wait_pids :: map(),
                 writers :: map()}.

tables/0

-type tables() :: #tables{meta_table :: ets:tab(), deps_table :: ets:tab(), data_table :: ets:tab()}.

Functions

cleanup(CleanUp_state)

-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.

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

-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.

flush(Server)

-spec flush(Server) -> Result when Server :: depcache_server(), Result :: ok.

Flush all keys from the caches


See also:gen_server:call/2.

flush(Key, Server)

-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.

flush_process_dict()

-spec flush_process_dict() -> Result when Result :: ok.

Flush all items memoized in the process dictionary.

get(Key, Server)

-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.

get(Key, SubKey, Server)

-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.

get_subkey(Key, SubKey, Server)

-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.

get_wait(Key, Server)

-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.

in_process(IsChaching)

-spec in_process(IsChaching) -> Result
                    when IsChaching :: undefined | boolean(), Result :: undefined | boolean().

Enable or disable the in-process caching using the process dictionary

in_process_server(Server)

-spec in_process_server(Server) -> Result when Server :: depcache_server(), Result :: boolean().

Check if we use a local process dict cache.

memo(Fun, Server)

-spec memo(Fun, Server) -> Result when Fun :: memo_fun(), Server :: depcache_server(), Result :: any().

Cache the result of the function for an hour.

memo(Fun, MaxAge_Key, Server)

-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.

memo(Fun, Key, MaxAge, Server)

-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.

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

-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.

set(Key, Data, Server)

-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.

set(Key, Data, MaxAge, Server)

-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.

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

-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.

size(Server)

-spec size(Server) -> Result when Server :: depcache_server(), Result :: non_neg_integer() | undefined.

Return the total memory size of all stored terms

start_link(Config)

-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

start_link(Name, Config)

-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