Remora / Remora.Discord

A data-oriented C# Discord library, focused on high-performance concurrency and robust design.
GNU Lesser General Public License v3.0
246 stars 44 forks source link

[Caching] Allow getting all instances based on key type #333

Closed Foxtrek64 closed 2 months ago

Foxtrek64 commented 2 months ago

Description

As it says on the tin, we should be able to pull all instances of a key type rather than just a specific key.

// Get specific user.
var userResult = await cacheService.TryGetValueAsync(new KeyHelpers.GuildMemberKey(guildId, userId), ct);

// Get all users
IEnumerable<IGuildUser> = await cacheService.GetAllAsync<KeyHelpers.GuildMemberKey>(ct);

Why This is Needed

I am using the CacheService for purposes outside of Discord, and I need to get all instances of tracked files to iterate through them and figure out if they've been processed yet.

Alternatives Considered

My original implementation relied on using a MemoryCache directly, but I like Remora's caching implementation.

Additional Details

No response

VelvetToroyashi commented 2 months ago

I personally think that you need to get back a bunch of cache keys as a list, it should be stored as a list to begin with. This design also introduces complexity in providers, such as Redis, as there'd be a hefty penalty to scanning across the entire cache to return all applicable results (and, if this were to be implemented, it should be IAsyncEnumerable for this purpose)

Nihlus commented 2 months ago

I'm in agreement with Velvet here - I don't think this is the way to go, especially considering a cache's opaque key-value store nature. I'd be open to revisiting this if more compelling use cases show up, but for now I'm closing as wontfix.