cronokirby/alchemy

[feature | breaking] Smart cache for Client.get* methods

Opened this issue · 2 comments

Motivation
Currently the library presents two options when accessing some data with a snowflake e.g. user, channel, guild, etc:

  • Get from cache via Cache
  • Fetch from the API

A common requirement is that the user needs to fetch some data by using the cache if it exists and the API if it doesn't.

Description
Add options keyword list to every function in Alchemy.Client which supports :cached. The default value is true. When true, fetch from the cache if it exists, otherwise carry out the API request. When false simply carry out the API request.

Affected Functions

Client...
+ get_user/1
+ get_current_guilds/1
+ get_channel/1
+ get_guild/1
+ get_channels/1
+ get_member/1
+ get_roles/1
# get_member_list/1
# get_DMs/1
# get_message/1
# get_reactions/1
# get_channel_invites/1
# get_pins/1
# get_invite/1
# get_invites/1
& get_bans/1
& get_regions/1
& get_intergrations/1
& create_DM/1

Legend
+ Cached already
# Probably should be cached (if not already)
& Maybe should be cached?

Note: I don't know what the library does to cache information that these methods would provide as they are received as events. I still think we should add caching of that information as that is done by many other bot libraries and there is no reason not to remember what Discord tells us. A few of the methods maybe we shouldn't cache depending on if there is an event which would tell us if that information changes (get_integrations?).

Some of these APIs don't map 1:1 onto a caching Cache. function, but we should still try and pull stuff from the cache even if it means multiple calls.

Do you think all the methods I listed should have smart cache? On the topic of cache, I've noticed that member caching seems to be a little... non-existent. On what circumstances does a member get cached right now? The function Cache.load_guild_members also seems like it does nothing related to the cache.