Skip to content

Commit

Permalink
Add a method to inspect cache contents per client
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Aug 19, 2023
1 parent 195d55e commit f09063c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/src/cache/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CacheConfig<T> {
typedef _CacheKey = ({String identifier, Snowflake key});

class _CacheEntry {
dynamic value;
Object? value;
int accessCount;

_CacheEntry(this.value) : accessCount = 0;
Expand Down Expand Up @@ -129,6 +129,22 @@ class Cache<T> with MapMixin<Snowflake, T> {
T? remove(Object? key) {
return _store.remove((identifier: identifier, key: key))?.value as T?;
}

/// Return a mapping of identifier to cache contents for all caches associated with [client].
static Map<String, Map<Snowflake, Object?>> cachesFor(Nyxx client) {
final store = _stores[client];
if (store == null) {
return {};
}

final result = <String, Map<Snowflake, Object?>>{};

for (final entry in store.entries) {
(result[entry.key.identifier] ??= {})[entry.key.key] = entry.value.value;
}

return result;
}
}

extension SnowflakeCache<T extends SnowflakeEntity<T>> on Cache<T> {
Expand Down

0 comments on commit f09063c

Please sign in to comment.