ShieldedCache v1.2.2 ShieldedCache.Cache behaviour View Source

ShieldedCache.Cache

The main behaviour module for a ShieldedCache module.

Callbacks

  • start_link/1: The start_link function for starting the module under the ShieldedCache Supervisor
  • get/3: Gets a value
  • set/4: Sets a value
  • invalidate/2: Invalidates a value in the cache
  • serialize/1: Serializes a value before saving it to the cache
  • deserialize/1: Deserializes a value before returning it to the client

Defaults

  • key/1: Uses the serialize!/1 function to serialize whatever the cache request is into a key for the cache.
  • serialize/1: Uses a simple JSON Encoder to encode the value as a String
  • deserialize/1: Uses a simple JSON Decoder to decode the value to a usable data structure

Link to this section Summary

Types

Return values of all functions for the cache

Return values of deserialize functions

Return values of serialize functions

Link to this section Types

Link to this type cache_return() View Source
cache_return() ::
  :ok
  | {:ok, value :: any()}
  | {:expired, expired_value :: any()}
  | {:error, reason :: any()}
  | value() :: any()

Return values of all functions for the cache

Link to this type on_deserialize() View Source
on_deserialize() ::
  {:ok, deserialized_value :: any()} | {:error, reason :: any()}

Return values of deserialize functions

Link to this type on_serialize() View Source
on_serialize() ::
  {:ok, serialized_value :: String.t()} | {:error, reason :: any()}

Return values of serialize functions

Link to this section Functions

Link to this function default_deserializer(value) View Source
default_deserializer(value :: String.t()) ::
  {:ok, deserialized_value :: any()} | {:error, reason :: any()}

default_deserializer/1

The default deserializer, which uses a simple JSON Decoder to decode the value to the corresponding object.

Return {:ok, value} or {:error, reason}.

Link to this function default_serializer(value) View Source
default_serializer(value :: any()) ::
  {:ok, serialized_value :: String.t()} | {:error, reason :: any()}

default_serializer/1

The default serializer, which uses a simple JSON Encoder to encode the value as a String.

Return {:ok, value} or {:error, reason}.

Link to this function is_cache_module?(module) View Source
is_cache_module?(module :: term()) :: true | false

is_cache_module/1

A utility function to determine if the provided argument is a module and if it implements the ShieldedCache.Cache behaviour.

Link to this section Callbacks

Link to this callback deserialize(serialized_value) View Source
deserialize(serialized_value :: String.t()) :: on_deserialize()
Link to this callback get(caching_module_name, key, ttl) View Source
get(caching_module_name :: atom(), key :: String.t(), ttl :: non_neg_integer()) ::
  cache_return()
Link to this callback invalidate(caching_module_name, key) View Source
invalidate(caching_module_name :: atom(), key :: String.t()) :: cache_return()
Link to this callback serialize(value) View Source
serialize(value :: any()) :: on_serialize()
Link to this callback set(caching_module_name, key, value, ttl) View Source
set(
  caching_module_name :: atom(),
  key :: String.t(),
  value :: any(),
  ttl :: non_neg_integer()
) :: cache_return()
Link to this callback start_link(opts) View Source
start_link(opts :: Keyword.t()) :: on_start :: any()