https://github.com/naughtygopher/pocache Skip to content Navigation Menu Toggle navigation Sign in * Product + GitHub Copilot Write better code with AI + Security Find and fix vulnerabilities + Actions Automate any workflow + Codespaces Instant dev environments + Issues Plan and track work + Code Review Manage code changes + Discussions Collaborate outside of code + Code Search Find more, search less Explore + All features + Documentation + GitHub Skills + Blog * Solutions By size + Enterprise + Teams + Startups By industry + Healthcare + Financial services + Manufacturing By use case + CI/CD & Automation + DevOps + DevSecOps * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} naughtygopher / pocache Public * Notifications You must be signed in to change notification settings * Fork 1 * Star 105 Pocache is a minimal cache package which focuses on a preemptive optimistic caching strategy License MIT license 105 stars 1 fork Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights naughtygopher/pocache This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main BranchesTags Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 25 Commits .github/workflows .github/ workflows .editorconfig .editorconfig .gitignore .gitignore LICENSE LICENSE README.md README.md cache.go cache.go cache_test.go cache_test.go go.mod go.mod go.sum go.sum View all files Repository files navigation * README * MIT license pocache gopher [badge] Go Reference Go Report Card Coverage Status License: MIT Pocache Pocache (poh-cash (/poU kaeS/)), Preemptive optimistic cache, is a lightweight in-app caching package. It introduces preemptive cache updates, optimizing performance in concurrent environments by reducing redundant database calls while maintaining fresh data. It uses Hashicorp's Go LRU package as the default storage. Key Features 1. Preemptive Cache Updates: Automatically updates cache entries nearing expiration. 2. Threshold Window: Configurable time window before cache expiration to trigger updates. 3. Serve stale: Opt-in configuration to serve even expired cache and do a background refresh. 4. Debounced Updates: Prevents excessive I/O calls by debouncing concurrent requests for the same key. 5. Custom store: customizable underlying storage to extend in-app cache to external database How does it work? Given a cache expiration time and a threshold window, Pocache triggers a preemptive cache update when a value is accessed within the threshold window. Example: * Cache expiration: 10 minutes * Threshold window: 1 minute |______________________ __threshold window__________ ______________| 0 min 9 mins 10 mins Add key here Get key within window Key expires When a key is fetched between 9-10 minutes (within the threshold window), Pocache initiates an update for that key (preemptive). This ensures fresh data availability, anticipating future usage ( optimistic). Why use preemptive updates? In highly concurrent environments (e.g., web servers), multiple requests might try to access the same cache entry simultaneously. Without preemptive updates, the system would query the underlying database multiple times until the cache is refreshed. Additionally by debouncing these requests, Pocache ensures only a single update is triggered, reducing load on both the underlying storage and the application itself. Full example package main import ( "context" "fmt" "time" "github.com/naughtygopher/pocache" ) type Item struct { ID string Name string Description string } func newItem(key string) *Item { return &Item{ ID: fmt.Sprintf("%d", time.Now().Nanosecond()), Name: "name::" + key, Description: "description::" + key, } } func updater(ctx context.Context, key string) (*Item, error) { return newItem(key), nil } func onErr(err error) { panic(fmt.Sprintf("this should never have happened!: %+v", err)) } func main() { cache, err := pocache.New(pocache.Config[string, *Item]{ // LRUCacheSize is the number of keys to be maintained in the cache (Optional, default 1000) LRUCacheSize: 100000, // QLength is the length of update and delete queue (Optional, default 1000) QLength: 1000, // CacheAge is for how long the cache would be maintained, apart from the LRU eviction // It's maintained to not maintain stale data if/when keys are not evicted based on LRU // (Optional, default 1minute) CacheAge: time.Hour, // Threshold is the duration prior to expiry, when the key is considered eligible to be updated // (Optional, default 1 second) Threshold: time.Minute * 5, // ServeStale will not return error if the cache has expired. It will return the stale // value, and trigger an update as well. This is useful for usecases where it's ok // to serve stale values and data consistency is not of paramount importance. // (Optional, default false) ServeStale: false, // UpdaterTimeout is the context time out for when the updater function is called // (Optional, default 1 second) UpdaterTimeout: time.Second * 15, // Updater is optional, but without it it's a basic LRU cache Updater: updater, // ErrWatcher is called when there's any error when trying to update cache (Optional) ErrWatcher: onErr, }) if err != nil { panic(err) } const key = "hello" item := newItem(key) e := cache.Add(key, item) fmt.Println("evicted:", e) ee := cache.BulkAdd([]pocache.Tuple[string, *Item]{ {Key: key + "2", Value: newItem(key + "2")}, }) fmt.Println("evicted list:", ee) ii := cache.Get(key) if ii.Found { fmt.Println("value:", ii.V) } ii = cache.Get(key + "2") if ii.Found { fmt.Println("value:", ii.V) } } The gopher The gopher used here was created using Gopherize.me. Incache helps you keep your application latency low and your database destressed. About Pocache is a minimal cache package which focuses on a preemptive optimistic caching strategy Topics go golang caching cache caching-strategies caching-library golang-cache go-cache Resources Readme License MIT license Activity Custom properties Stars 105 stars Watchers 1 watching Forks 1 fork Report repository Releases 1 tags Packages 0 No packages published Languages * Go 100.0% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.