Repository navigation
Conversation
added 4 commits
October 24, 2026 02:10
8000
…lication, and expand test suite - Execute eviction callbacks strictly outside mutex locks to allow safe reentrant operations\n- Yield snapshots in iterators to prevent deadlocks when mutating cache during iteration\n- Add singleflight request coalescing to REST fallback wrapper to prevent concurrent miss request storms\n- Handle non-guild channel types gracefully and document GuildChannel cache limitation\n- Prevent double-wrapping in NewRestFallbackCaches\n- Invoke eviction callbacks consistently on Remove and GroupRemove\n- Document TTL <= 0 and Put expiration refresh semantics\n- Add comprehensive regression and concurrency unit tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds thread-safe LRU (Least Recently Used) and TTL (Time-To-Live) cache implementations for
cache.Cache[T]andcache.GroupedCache[T], as well as an optional REST fallback layer forcache.Caches.Implementations
1.
LRUCache[T any](implementsCache[T])head/tail) and hash map lookup.WithLRUTTL(duration). Expired entities are lazily evicted on access and filtered from iterations.WithLRUOnEvict(func(id snowflake.ID, entity T)).All() iter.Seq[T]skipping expired entries.NewTTLCache(...)for pure TTL-based caches without capacity limits.2.
GroupedLRUCache[T any](implementsGroupedCache[T])maxItemsPerGroup) for grouped entities (e.g. guild members or channel messages).WithGroupedLRUMaxGroups(maxGroups)) to bound the total number of cached groups.WithGroupedLRUTTL(duration).GroupRemove(groupID),GroupRemoveIf(groupID, filter).All() iter.Seq2[snowflake.ID, T]andGroupAll(groupID) iter.Seq[T].NewGroupedTTLCache(...).3.
RestFallbackCaches(implementsCaches)cache.Cachesthat fetches missing entities on cache misses viarest.Restand populates them into the underlying cache.RestFallbackFlagsallows granular control over which entities fallback to REST (e.g.RestFallbackFlagMembers,RestFallbackFlagRoles, etc.).cache.NewRestFallbackCaches(caches, rest, flags)cache.WithRestFallback(rest, flags...)option forcache.New(...)bot.WithRestFallback(flags...)top-level option fordisgo.New(...)Tests
cache/lru_cache_test.gowith 10 unit tests for LRU eviction, TTL expiration, group management, and multi-goroutine concurrency.cache/rest_fallback_test.gotesting cache miss fallback, subsequent cache hits, and disabled flags.