Module depcache

Depcache API.

Copyright © 2009-2020 Marc Worrell, Arjan Scherpenisse

Behaviours: gen_server.

Authors: Arjan Scherpenisse.

Description

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

Data Types

callback()

callback() = mfargs()

cleanup_state()

cleanup_state() = #cleanup_state{pid = pid(), tables = tables(), name = atom(), memory_max = non_neg_integer(), callback = callback() | undefined}

config()

config() = config_map() | config_proplist()

config_map()

config_map() = #{memory_max => non_neg_integer() | undefined, callback => callback() | undefined}

config_proplist()

config_proplist() = [{memory_max, non_neg_integer | undefined} | {callback, callback() | undefined}]

depcache_server()

depcache_server() = pid() | atom()

dependencies()

dependencies() = [key()]

key()

key() = any()

max_age_secs()

max_age_secs() = sec()

memo_fun()

memo_fun() = function() | mfargs() | {module(), atom()}

mfargs()

mfargs() = {module(), atom(), list()}

sec()

sec() = non_neg_integer()

tables()

tables() = #tables{meta_table = ets:tab(), deps_table = ets:tab(), data_table = ets:tab()}

Function Index

cleanup/1Cleanup process for the depcache.
cleanup/5Cleanup process for the depcache.
flush/1Flush all keys from the caches.
flush/2Flush the key and all keys depending on that key.
flush_process_dict/0Flush all items memoized in the process dictionary.
get/2Fetch the key from the cache, return the data or an undefined if not found (or not valid).
get/3Fetch the key from the cache, return the data or an undefined if not found (or not valid).
get_subkey/3Fetch the key from the cache, return the data or an undefined if not found (or not valid).
get_wait/2Fetch the key from the cache, when the key does not exist then lock the entry and let the calling process insert the value.
in_process/1Enable or disable the in-process caching using the process dictionary.
in_process_server/1Check if we use a local process dict cache.
memo/2Cache the result of the function for an hour.
memo/3If Fun is a function then cache for an hour given the key.
memo/4Cache the result of the function as Key for MaxAge seconds.
memo/5Cache the result of the function as Key for MaxAge seconds, flush the cached result if any of the dependencies is changed.
set/3Add the key to the depcache, hold it for 3600 seconds and no dependencies.
set/4Add the key to the depcache, hold it for MaxAge seconds and no dependencies.
set/5Add the key to the depcache, hold it for MaxAge seconds and check the dependencies.
size/1Return the total memory size of all stored terms.
start_link/1Start a depcache process.
start_link/2Start a named depcache process.

Function Details

cleanup/1

cleanup(CleanUp_state) -> Result

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/5

cleanup(State, SlotNr, Now, Mode, Ct) -> Result

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/1

flush(Server) -> Result

Flush all keys from the caches
See also: gen_server:call/2.

flush/2

flush(Key, Server) -> Result

Flush the key and all keys depending on that key
See also: gen_server:call/2.

flush_process_dict/0

flush_process_dict() -> Result

Flush all items memoized in the process dictionary.

get/2

get(Key, Server) -> Result

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/3

get(Key, SubKey, Server) -> Result

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/3

get_subkey(Key, SubKey, Server) -> Result

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/2

get_wait(Key, Server) -> Result

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/1

in_process(IsChaching) -> Result

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

in_process_server/1

in_process_server(Server) -> Result

Check if we use a local process dict cache.

memo/2

memo(Fun, Server) -> Result

Fun: a function for producing a value

returns: cached value

Cache the result of the function for an hour.

memo/3

memo(Fun, MaxAge_Key, Server) -> Result

Fun: a function for producing a value1

returns: cached value

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/4

memo(Fun, Key, MaxAge, Server) -> Result

returns: cached value

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

Cache the result of the function as Key for MaxAge seconds.

memo/5

memo(Fun, Key, MaxAge, Dep, Server) -> Result

returns: cached value

Cache the result of the function as Key for MaxAge seconds, flush the cached result if any of the dependencies is changed.

set/3

set(Key, Data, Server) -> Result

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

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

set/4

set(Key, Data, MaxAge, Server) -> Result

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

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

set/5

set(Key, Data, MaxAge, Depend, Server) -> Result

MaxAge: maximum lifetime of an element in the cache
Depend: list of subkeys
See also: gen_server:call/2.

Add the key to the depcache, hold it for MaxAge seconds and check the dependencies.

size/1

size(Server) -> Result

Return the total memory size of all stored terms

start_link/1

start_link(Config) -> Result

Config: configuration options

returns: Result of starting gen_server item.

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/2

start_link(Name, Config) -> Result

Name: of process
Config: configuration options

returns: Result of starting gen_server item.

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


Generated by EDoc