PayDayLoan.EtsBackend (pay_day_loan v0.8.2)

ETS-based backend capable of handling raw values, pids, or callbacks.

This is the default backend used by PayDayLoan and is designed for storing process ids. However, it can be used with raw values or callback functions.

With pids, special care is taken to keep the cache state consistent with the "alive" state of the processes. If a process is found to be dead, the key is removed from cache. The PayDayLoan.ProcessMonitor process monitors pids, and we check for alive-ness whenever we resolve a value.

If a callback is stored, then the callback is executed whenever we attempt to resolve a value - e.g., on get or reduce or values calls. The callback must return a tuple with {:ok, value} on success or {:error, :not_found} on failure.

The functions in this module are documented only to aid in understanding how the default backend works. They should not be called directly - only through the PDL API.

Link to this section Summary

Functions

Remove a key from cache

Remove a value from cache

Get the value corresponding to the given key

Returns a list of all cached keys

Add a value to the cache and monitor it if it is a pid.

Perform Enum.reduce on the ETS table

Setup callback, creates the underlying ETS table

Returns the number of cached keys

Returns a list of all cached values

Link to this section Functions

Link to this function

delete(pay_day_loan, key)

@spec delete(PayDayLoan.t(), PayDayLoan.key()) :: :ok

Remove a key from cache

Link to this function

delete_value(pay_day_loan, value)

@spec delete_value(PayDayLoan.t(), term()) :: :ok

Remove a value from cache

@spec get(PayDayLoan.t(), PayDayLoan.key()) :: {:ok, term()} | {:error, :not_found}

Get the value corresponding to the given key

If the value is a process that is not alive, deletes the entry and returns {:error, :not_found}.

@spec keys(PayDayLoan.t()) :: [PayDayLoan.key()]

Returns a list of all cached keys

Link to this function

put(pdl, key, value)

@spec put(PayDayLoan.t(), PayDayLoan.key(), term()) :: :ok

Add a value to the cache and monitor it if it is a pid.

Link to this function

reduce(pdl, acc0, reducer)

@spec reduce(PayDayLoan.t(), term(), ({PayDayLoan.key(), pid()}, term() -> term())) ::
  term()

Perform Enum.reduce on the ETS table

Link to this function

setup(pay_day_loan)

@spec setup(PayDayLoan.t()) :: :ok

Setup callback, creates the underlying ETS table

Link to this function

size(pay_day_loan)

@spec size(PayDayLoan.t()) :: non_neg_integer()

Returns the number of cached keys

@spec values(PayDayLoan.t()) :: [term()]

Returns a list of all cached values