View Source Nostrum.Cache.UserCache behaviour (Nostrum v0.10.0)

Cache behaviour & dispatcher for users.

You can call the functions provided by this module independent of which cache is configured, and it will dispatch to the configured cache implementation.

By default, Elixir.Nostrum.Cache.UserCache.ETS will be used for caching users. You can override this in the :caches option of the :nostrum application by setting the :users field to a different module implementing the behaviour defined by this module.

See the documentation for the Nostrum.Cache.GuildCache module for more details.

Summary

Callbacks

Bulk add multiple users to the cache at once.

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

Add a new user to the cache based on the Discord Gateway payload.

Delete a user by ID.

Retrieve a user from the cache by id.

Update a user in the cache based on payload sent via the Gateway.

A function that should wrap any :qlc operations.

Functions

Retrieve a user from the cache by ID.

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

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

Callbacks

Link to this callback

bulk_create(user_payloads)

View Source
@callback bulk_create(user_payloads :: Enum.t()) :: :ok

Bulk add multiple users to the cache at once.

Returns :ok.

@callback child_spec(term()) :: Supervisor.child_spec()

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

@callback create(payload :: map()) :: Nostrum.Struct.User.t()

Add a new user to the cache based on the Discord Gateway payload.

Returns a Nostrum.Struct.User.t/0 struct representing the created user.

@callback delete(snowflake :: Nostrum.Struct.User.id()) :: :noop | Nostrum.Struct.User.t()

Delete a user by ID.

Returns the deleted user if present in the cache, or :noop if the user was not cached.

@callback get(Nostrum.Struct.User.id()) ::
  {:ok, Nostrum.Struct.User.t()} | {:error, atom()}

Retrieve a user from the cache by id.

@callback update(payload :: map()) ::
  {Nostrum.Struct.User.t() | nil, Nostrum.Struct.User.t()}

Update a user in the cache based on payload sent via the Gateway.

Returns :noop if the user has not been updated in the cache, or {old_user, new_user} is the user has been written to the cache.

Link to this callback

wrap_query(function)

View Source (optional) (since 0.8.0)
@callback wrap_query((-> 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_query(fn -> ... end).

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

Functions

@spec get(Nostrum.Struct.User.id()) ::
  {:ok, Nostrum.Struct.User.t()} | {:error, atom()}

Retrieve a user from the cache by ID.

Example

case Nostrum.Cache.UserCache.get(1111222233334444) do
  {:ok, user} ->
    "We found " <> user.username
  {:error, _reason} ->
    "No es bueno"
end

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

Link to this function

wrap_query(cache \\ Nostrum.Cache.UserCache.ETS, fun)

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

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

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