View Source Nostrum.Cache.PresenceCache behaviour (Nostrum v0.8.0)

Cache behaviour & dispatcher for Discord presences.

By default, Elixir.Nostrum.Cache.PresenceCache.ETS will be use for caching presences. You can override this in the :caches option of the nostrum application by setting the :presences fields to a different module implementing the Nostrum.Cache.PresenceCache behaviour. Any module below Nostrum.Cache.PresenceCache implements this behaviour and can be used as a cache.

writing-your-own-presence-cache

Writing your own presence cache

As with the other caches, the presence cache API consists of two parts:

  • The functions that nostrum calls, such as create/1 or update/1. These do not create any objects in the Discord API, they are purely created to update the cached data from data that Discord sends us. If you want to create objects on Discord, use the functions exposed by Nostrum.Api instead.

  • the QLC query handle for read operations, query_handle/0, and

  • the child_spec/1 callback for starting the cache under a supervisor.

You need to implement both of them for nostrum to work with your custom cache.

Link to this section Summary

Types

Represents a presence as received from Discord. See Presence Update.

Callbacks

Bulk create multiple presences for the given guild in the cache.

Retrieve the child specification for starting this mapping under a supervisor.

Create a presence in the cache.

Return a QLC query handle for cache read operations.

Update the given presence in the cache from upstream data.

A function that should wrap any :qlc operations.

Functions

Retrieves a presence for a user from the cache by guild and id.

Return the QLC handle of the configured cache.

Call wrap_qlc/1 on the given cache, if implemented.

Link to this section Types

Link to this opaque

presence()

View Source (opaque) (since 0.5.0)
@opaque presence()

Represents a presence as received from Discord. See Presence Update.

Link to this section Callbacks

Link to this callback

bulk_create(id, list)

View Source (since 0.5.0)
@callback bulk_create(Nostrum.Struct.Guild.id(), [presence()]) :: :ok

Bulk create multiple presences for the given guild in the cache.

Link to this callback

child_spec(term)

View Source (since 0.5.0)
@callback child_spec(term()) :: Supervisor.child_spec()

Retrieve the child specification for starting this mapping under a supervisor.

Link to this callback

create(presence)

View Source (since 0.5.0)
@callback create(presence()) :: :ok

Create a presence in the cache.

Link to this callback

query_handle()

View Source (since 0.8.0)
@callback query_handle() :: :qlc.query_handle()

Return a QLC query handle for cache read operations.

This is used by nostrum to provide any read operations on the cache. Write operations still need to be implemented separately.

The Erlang manual on Implementing a QLC Table contains examples for implementation. To prevent full table scans, accept match specifications in your TraverseFun and implement a LookupFun as documented.

The query handle must return items in the form {{guild_id, user_id}, presence}, where:

If your cache needs some form of setup or teardown for QLC queries (such as opening connections), see wrap_qlc/1.

Link to this callback

update(map)

View Source (since 0.5.0)
@callback update(map()) ::
  {Nostrum.Struct.Guild.id(), old_presence :: presence() | nil,
   new_presence :: presence()}
  | :noop

Update the given presence in the cache from upstream data.

return-value

Return value

Return the guild ID along with the old presence (if it was cached, otherwise nil) and the updated presence structure. If the :activities or :status fields of the presence did not change, return :noop.

Link to this callback

wrap_qlc(function)

View Source (optional) (since 0.8.0)
@callback wrap_qlc((() -> result)) :: result when result: term()

A function that should wrap any :qlc operations.

If you implement a cache that is backed by a database and want to perform cleanup and teardown actions such as opening and closing connections, managing transactions and so on, you want to implement this function. nostrum will then effectively call wrap_qlc(fn -> :qlc.e(...) end).

If your cache does not need any wrapping, you can omit this.

Link to this section Functions

Link to this function

get(guild_id, user_id, cache \\ Nostrum.Cache.PresenceCache.ETS)

View Source (since 0.5.0)
@spec get(Nostrum.Struct.Guild.id(), Nostrum.Struct.User.id(), module()) ::
  {:ok, presence()} | {:error, :presence_not_found}

Retrieves a presence for a user from the cache by guild and id.

If successful, returns {:ok, presence}. Otherwise returns {:error, reason}.

example

Example

case Nostrum.Cache.PresenceCache.get(111133335555, 222244446666) do
  {:ok, presence} ->
    "They're #{presence.status}"
  {:error, _reason} ->
    "They're dead Jim"
end
Link to this function

get!(guild_id, user_id, cache \\ Nostrum.Cache.PresenceCache.ETS)

View Source (since 0.5.0)

Same as get/1, but raise Nostrum.Error.CacheError in case of a failure.

Link to this function

query_handle()

View Source (since 0.8.0)

Return the QLC handle of the configured cache.

Link to this function

wrap_qlc(cache \\ Nostrum.Cache.PresenceCache.ETS, fun)

View Source (since 0.8.0)
@spec wrap_qlc(module(), (() -> result)) :: result when result: term()

Call wrap_qlc/1 on the given cache, if implemented.

If no cache is given, calls out to the default cache.