View Source Nostrum.Cache.MessageCache.Mnesia (Nostrum v0.10.0)

An Mnesia-based cache for messages.

Please note that this module is only compiled if Mnesia is available on your system. See the Mnesia section of the State documentation for more information.

To retrieve the table name used by this cache, use table/0.

By default, the cache will store up to 10_000 messages, and will evict the 100 oldest messages when the limit is reached.

The reason for the eviction count is that with mnesia it is more efficient to find X oldest records and delete them all at once than to find the oldest record and delete it each time a new record is added.

The Mnesia cache supports the following configuration options:

  • size_limit: The maximum number of messages to store in the cache. default: 10_000
  • eviction_count: The number of messages to evict when the cache is full. default: 100
  • table_name: The name of the Mnesia table to use for the cache. default: :nostrum_messages
  • compressed: Whether to use compressed in memory storage for the table. default: false
  • type: Sets the type of Mnesia table created to cache messages. Can be either :set or :ordered_set, by choosing :ordered_set the eviction of the oldest messages will be more efficient, however it means that the table cannot be changed to only store its contents on disk later. default: :ordered_set

To change this configuration, you can add the following to your config.exs:

config :nostrum,
  caches: %{
    messages: {Nostrum.Cache.MessageCache.Mnesia,
               size_limit: 1000, eviction_count: 50,
               table_name: :my_custom_messages_table_name,
               compressed: true, type: :set}
  }

You can also change the table name used by the cache by setting the table_name field in the configuration for the messages cache.

Summary

Functions

Removes and returns a list of messages from the cache. Messages not found in the cache will not be included in the returned list.

Removes all messages for a channel which was deleted.

Returns a specification to start this module under a supervisor.

Clear any objects in the cache.

Adds a message to the cache.

Removes a message from the cache.

Retrieve a single message from the cache by id.

Retrieve a list of messages from the cache with a given author ID, optionally after a given date, and before a given date.

Retrieve a list of messages from the cache with a given channel ID, optionally after a given date, and before a given date.

Retrieve a list of messages from the cache with a given channel ID and author ID, optionally after a given date, and before a given date.

Set up the cache's Mnesia table.

Return a QLC query handle for the cache for read operations.

Start the supervisor.

Retrieve the Mnesia table name used for the cache.

Drop the table used for caching.

Updates a message in the cache.

Functions

Link to this function

bulk_delete(channel_id, message_ids)

View Source (since 0.10.0)

Removes and returns a list of messages from the cache. Messages not found in the cache will not be included in the returned list.

Link to this function

channel_delete(channel_id)

View Source (since 0.10.0)
@spec channel_delete(Nostrum.Struct.Channel.id()) :: :ok

Removes all messages for a channel which was deleted.

Link to this function

child_spec(init_arg)

View Source (since 0.10.0)

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec clear() :: :ok

Clear any objects in the cache.

Link to this function

create(payload)

View Source (since 0.10.0)
@spec create(map()) :: Nostrum.Struct.Message.t()

Adds a message to the cache.

Link to this function

delete(channel_id, message_id)

View Source (since 0.10.0)

Removes a message from the cache.

Link to this function

get(message_id)

View Source (since 0.10.0)
@spec get(Nostrum.Struct.Message.id()) ::
  {:ok, Nostrum.Struct.Message.t()} | {:error, :not_found}

Retrieve a single message from the cache by id.

Link to this function

get_by_author(author_id, after_timestamp \\ 0, before_timestamp \\ :infinity)

View Source (since 0.10.0)

Retrieve a list of messages from the cache with a given author ID, optionally after a given date, and before a given date.

Integers are treated as snowflakes, and the atom :infinity when given as a before date will be treated as the maximum possible date.

Link to this function

get_by_channel(channel_id, after_timestamp \\ 0, before_timestamp \\ :infinity)

View Source (since 0.10.0)

Retrieve a list of messages from the cache with a given channel ID, optionally after a given date, and before a given date.

Link to this function

get_by_channel_and_author(channel_id, author_id, after_timestamp \\ 0, before_timestamp \\ :infinity)

View Source (since 0.10.0)

Retrieve a list of messages from the cache with a given channel ID and author ID, optionally after a given date, and before a given date.

Link to this function

init(init_arg)

View Source (since 0.10.0)

Set up the cache's Mnesia table.

Link to this function

query_handle()

View Source (since 0.10.0)
@spec query_handle() :: :qlc.query_handle()

Return a QLC query handle for the cache for read operations.

Link to this function

start_link(init_arg)

View Source (since 0.10.0)

Start the supervisor.

@spec table() :: atom()

Retrieve the Mnesia table name used for the cache.

Link to this function

teardown()

View Source (since 0.10.0)
@spec teardown() :: {:atomic, :ok} | {:aborted, term()}

Drop the table used for caching.

Link to this function

update(payload)

View Source (since 0.10.0)
@spec update(map()) ::
  {old_message :: Nostrum.Struct.Message.t() | nil,
   updated_message :: Nostrum.Struct.Message.t()}

Updates a message in the cache.