Skip to content

Proposal: State and Cache providers #2238

Open
@quinchs

Description

@quinchs

Proposal

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:

dnet-state-cache-flow

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.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Proposals

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions