From 4cce67b3ffb841f781c359e2def6c060eec32791 Mon Sep 17 00:00:00 2001 From: Leastrio Date: Sat, 17 Jun 2023 13:38:35 -0600 Subject: [PATCH] Add NoOp implementations for the rest of the caches --- lib/nostrum/cache/channel_cache/noop.ex | 38 +++++++++++++++++++ .../cache/channel_guild_mapping/noop.ex | 30 +++++++++++++++ lib/nostrum/cache/member_cache/noop.ex | 38 +++++++++++++++++++ lib/nostrum/cache/user_cache/noop.ex | 36 ++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 lib/nostrum/cache/channel_cache/noop.ex create mode 100644 lib/nostrum/cache/channel_guild_mapping/noop.ex create mode 100644 lib/nostrum/cache/member_cache/noop.ex create mode 100644 lib/nostrum/cache/user_cache/noop.ex diff --git a/lib/nostrum/cache/channel_cache/noop.ex b/lib/nostrum/cache/channel_cache/noop.ex new file mode 100644 index 000000000..d09f2d672 --- /dev/null +++ b/lib/nostrum/cache/channel_cache/noop.ex @@ -0,0 +1,38 @@ +defmodule Nostrum.Cache.ChannelCache.NoOp do + @moduledoc """ + A NoOp implementation for the ChannelCache + + This cache does nothing, enable it if you dont need to cache channels + """ + + @behaviour Nostrum.Cache.ChannelCache + + alias Nostrum.Cache.ChannelCache + alias Nostrum.Struct.Channel + use Supervisor + + @doc "Start the supervisor." + def start_link(init_arg) do + Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) + end + + @impl Supervisor + def init(_init_arg) do + Supervisor.init([], strategy: :one_for_one) + end + + @impl ChannelCache + def create(channel), do: convert(channel) + + @impl ChannelCache + def update(channel), do: {nil, convert(channel)} + + @impl ChannelCache + def delete(_id), do: :noop + + @impl ChannelCache + def query_handle, do: :qlc.string_to_handle('[].') + + defp convert(%{__struct__: _} = struct), do: struct + defp convert(map), do: Channel.to_struct(map) +end diff --git a/lib/nostrum/cache/channel_guild_mapping/noop.ex b/lib/nostrum/cache/channel_guild_mapping/noop.ex new file mode 100644 index 000000000..14445cd72 --- /dev/null +++ b/lib/nostrum/cache/channel_guild_mapping/noop.ex @@ -0,0 +1,30 @@ +defmodule Nostrum.Cache.ChannelGuildMapping.NoOp do + @moduledoc """ + NoOp implementation for the Channel Guild map + """ + + alias Nostrum.Cache.ChannelGuildMapping + + @behaviour ChannelGuildMapping + + use Supervisor + + @doc "Start the supervisor." + def start_link(init_arg) do + Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) + end + + @impl Supervisor + def init(_init_arg) do + Supervisor.init([], strategy: :one_for_one) + end + + @impl ChannelGuildMapping + def create(_channel_id, _guild_id), do: true + + @impl ChannelGuildMapping + def get(_channel_id), do: nil + + @impl ChannelGuildMapping + def delete(_channel_id), do: true +end diff --git a/lib/nostrum/cache/member_cache/noop.ex b/lib/nostrum/cache/member_cache/noop.ex new file mode 100644 index 000000000..a1c397f3a --- /dev/null +++ b/lib/nostrum/cache/member_cache/noop.ex @@ -0,0 +1,38 @@ +defmodule Nostrum.Cache.MemberCache.NoOp do + @moduledoc """ + A NoOp implementation for the MemberCache + + This cache does nothing, enable it if you dont need to cache members + """ + @behaviour Nostrum.Cache.MemberCache + + alias Nostrum.Cache.MemberCache + alias Nostrum.Struct.Guild.Member + alias Nostrum.Util + use Supervisor + + @doc "Start the supervisor." + def start_link(init_arg) do + Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) + end + + @impl Supervisor + def init(_init_arg) do + Supervisor.init([], strategy: :one_for_one) + end + + @impl MemberCache + def create(_guild_id, payload), do: Util.cast(payload, {:struct, Member}) + + @impl MemberCache + def update(guild_id, payload), do: {guild_id, nil, Util.cast(payload, {:struct, Member})} + + @impl MemberCache + def delete(_guild_id, _user_id), do: :noop + + @impl MemberCache + def bulk_create(_guild_id, _members), do: true + + @impl MemberCache + def query_handle, do: :qlc.string_to_handle('[].') +end diff --git a/lib/nostrum/cache/user_cache/noop.ex b/lib/nostrum/cache/user_cache/noop.ex new file mode 100644 index 000000000..361d5f984 --- /dev/null +++ b/lib/nostrum/cache/user_cache/noop.ex @@ -0,0 +1,36 @@ +defmodule Nostrum.Cache.UserCache.NoOp do + @moduledoc """ + A NoOp implementation for the UserCache + + This cache does nothing, enable it if you dont need to cache users + """ + @behaviour Nostrum.Cache.UserCache + + alias Nostrum.Struct.User + use Supervisor + + @doc "Start the supervisor." + def start_link(init_arg) do + Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__) + end + + @impl Supervisor + def init(_init_arg) do + Supervisor.init([], strategy: :one_for_one) + end + + @impl Nostrum.Cache.UserCache + def bulk_create(_users), do: :ok + + @impl Nostrum.Cache.UserCache + def create(payload), do: User.to_struct(payload) + + @impl Nostrum.Cache.UserCache + def update(info), do: {nil, User.to_struct(info)} + + @impl Nostrum.Cache.UserCache + def delete(_id), do: :noop + + @impl Nostrum.Cache.UserCache + def query_handle, do: :qlc.string_to_handle('[].') +end