Skip to content

Commit

Permalink
Merge pull request #513 from Leastrio/noop
Browse files Browse the repository at this point in the history
Add NoOp implementations for the rest of the caches
  • Loading branch information
jchristgit authored Jun 17, 2023
2 parents fc0a4ba + 4cce67b commit 26d629d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/nostrum/cache/channel_cache/noop.ex
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions lib/nostrum/cache/channel_guild_mapping/noop.ex
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions lib/nostrum/cache/member_cache/noop.ex
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions lib/nostrum/cache/user_cache/noop.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 26d629d

Please # to comment.