ConCache.start_link
start_link
, go back to ConCache module for more information.
Specs
start_link(options()) :: Supervisor.on_start()
Starts the server and creates an ETS table.
Options:
{:name, atom} - A name of the cache process.
{:ttl_check_interval, time_ms | false}
- Required. A check interval for TTL expiry. Provide a positive integer for expiry to work, or passfalse
to disable ttl checks. See below for more details on expiry.{:global_ttl, time_ms | :infinity}
- The time after which an item expires. When an item expires, it is removed from the cache. Updating the item extends its expiry time.{:touch_on_read, true | false}
- Controls whether read operation extends expiry of items. False by default.{:callback, callback_fun}
- If provided, this function is invoked after an item is inserted or updated, or before it is deleted.{:acquire_lock_timeout, timeout_ms}
- The time a client process waits for the lock. Default is 5000.{:ets_options, [ets_option]
– The options for ETS process.{:n_lock_partitions, pos_integer}
- A positive integer representing the desired number of lock partitions
In addition, following ETS options are supported:
:set
- An ETS table will be of the:set
type (default).:ordered_set
- An ETS table will be of the:ordered_set
type.:bag
- An ETS table will be of the:bag
type.:duplicate_bag
- An ETS table will be of the:duplicate_bag
type.:named_table
:name
:heir
:write_concurrency
:read_concurrency
:decentralized_counters
Child specification
To insert your cache into the supervision tree, pass the child specification
in the shape of {ConCache, con_cache_options}
. For example:
{ConCache, [name: :my_cache, ttl_check_interval: false]}
Expiry
To configure expiry, you need to provide positive integer for the
:ttl_check_interval
option. This integer represents the millisecond interval
in which the expiry is performed. You also need to provide the :global_ttl
option, which represents the default TTL time for the item.
TTL of each item is by default extended only on modifications. This can be
changed with the touch_on_read: true
option.
If you need a granular control of expiry per each item, you can pass a
ConCache.Item
struct when storing data.
If you don't want a modification of an item to extend its TTL, you can pass a
ConCache.Item
struct, with :ttl
field set to :no_update
.
Choosing ttl_check_interval time
When expiry is configured, the owner process works in discrete steps, doing
cleanups every ttl_check_interval
milliseconds. This approach allows the owner
process to do fairly small amount of work in each discrete step.
Assuming there's no huge system overload, an item's max lifetime is thus
global_ttl + ttl_check_interval
[ms], after the last item's update.
Thus, a lower value of ttl_check_interval time means more frequent purging which may reduce your memory consumption, but could also cause performance penalties. Higher values put less pressure on processing, but item expiry is less precise.