Crux vdev Crux.Cache behaviour View Source

Behaviour all caches must implement. (Looking at custom ones you may want to write)

There are exceptions:

  • User cache:

    • Implement a me/1 function setting the own user id
    • A me/0 and me!/0 function getting the own user
  • Guild cache:

    • A bit more, you probably want to take a look at the code of the Crux.Cache.Guild module

Link to this section Summary

Types

Default caches are using Discord Snowflakes as identifiers

Functions

Fetches the module handling the guild cache

Fetches the module handling the emoji cache

Fetches the module handling the guild caching

Fetches the module handling the presence cache

Fetches the module handling the user cache

Callbacks

Deletes data from the cache by key

Fetches data from the cache by key

Fetches data from the cache by key, raises if not found

Inserts data into the cache

Used to start anything fitting under a supervision tree, like for example a GenServer, instructed with handling the cache

Inserts data into the cache

Link to this section Types

Default caches are using Discord Snowflakes as identifiers.

Custom caches my obviously implement their own key type.

Link to this section Functions

Link to this function

channel_cache() View Source
channel_cache() :: module()

Fetches the module handling the guild cache.

Defaults to Crux.Cache.Channel.

Link to this function

emoji_cache() View Source
emoji_cache() :: module()

Fetches the module handling the emoji cache.

Defaults to Crux.Cache.Emoji.

Link to this function

guild_cache() View Source
guild_cache() :: module()

Fetches the module handling the guild caching.

Defaults to Crux.Cache.Guild.

Link to this function

presence_cache() View Source
presence_cache() :: module()

Fetches the module handling the presence cache.

Defaults to Crux.Cache.Presence.

Link to this function

user_cache() View Source
user_cache() :: module()

Fetches the module handling the user cache.

Defaults to Crux.Cache.User.

Link to this section Callbacks

Link to this callback

delete(id) View Source
delete(id :: key()) :: :ok

Deletes data from the cache by key.

Always returns :ok, even when the key did not exist.

Link to this callback

fetch(id) View Source
fetch(id :: key()) :: {:ok, term()} | :error

Fetches data from the cache by key.

Link to this callback

fetch!(id) View Source
fetch!(id :: identifier()) :: term() | :error

Fetches data from the cache by key, raises if not found.

Link to this callback

insert(data) View Source
insert(data :: term()) :: term()

Inserts data into the cache.

Returns the atomified data allowing the operation to be chained.

For example something like that:

id =
  raw_data
  |> Cache.insert()
  |> Map.get(:id)
Link to this callback

start_link(args) View Source (optional)
start_link(args :: term()) :: Supervisor.on_start()

Used to start anything fitting under a supervision tree, like for example a GenServer, instructed with handling the cache.

Optional, you maybe want to use external caching, e.g. Redis, not requiring anything like that.

Link to this callback

update(data) View Source
update(data :: term()) :: term()

Inserts data into the cache.

Returns "updated" data including changes by merging.
For example from a message embed update to a full message object

content =
  partial_message # only contains `:id`, `:channel_id`, and `:embeds`
  |> Cache.update()
  |> Map.get(:content) # present if the message was cached previously