Proposal: State and Cache providers
See original GitHub issueProposal
Memory usage is a big issue within the library, developers have minimal control on how entities are stored in state. The proposal is to allow developers to fully customize the way Discord.Net stores entities in memory.
Foxbot has the first part of this covered with the IStateProvider
allowing users to control how entities are fetched-stored to and from the cache. The state provider allows for downloading entities if they don’t exist within the cache for almost all gateway models and events.
The ICacheProvider
is something I’ve been thinking about for way too long. I’ve finalized my idea on implementing this into Discord.Net.
For each entity that can be cached ex IGuildMember
there will be a IGuildMemberModel
interface that allows us to have multiple ways to implement converting to and from strong-typed entities to models. We can make the api model Discord.API.Member
and make it inherit the IGuildMemberModel
interface and take in the interface for constructing a SocketGuildMember
or RestGuildMember
entity. Developers can create their own models that inherit IGuildMemberModel
for example a class that converts itself to a protobuf buffer and pass that to the state provider to use to create an entity.
The term “state provider” and “cache provider” are two separate things, the state provider determines how entities are retrieved and stored while the cache provider stores to and fetches from the cache. Take this flow diagram as some more explanation behind this:
Things to note
The state provider will have to return the interface types of entities ex IGuild
, IUser
, etc due to the ability to fetch from rest if the behavior is enabled, this will cause a lot of gateway events to have to use interfaces due to this.
Issue Analytics
- State:
- Created a year ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
Yeah since they’re task.run under the hood 👀
appreciate the updates. in the short term I’m unblocked by just holding onto object state in memory while requests are in-flight. I’m working on a project that is using this library so I’m here to help in any way I can.