How go-cache works: Multi-layered caching and Cache-Aside
mastergo-cache is a multi-layered caching library that coordinates between a private (in-memory) cache and a shared cache (e.g., Redis).
Cache-Aside Data Flow
When an application requests data via Cache.Get() or Cache.MGet():
- Local Cache Hit: Data is returned immediately from the private in-memory cache.
- Local Miss / Shared Hit: If the local cache misses but the shared cache (Redis) hits, the data is fetched from the shared cache and then stored in the local cache for future requests.
- Total Miss: If both caches miss, the library executes a provided getter function (e.g., a database query). The resulting data is then stored in both the shared cache and the local cache before being returned to the application.
Cache Eviction
When Cache.Del() is called:
- The key is deleted from the shared cache.
- The key is deleted from the local cache.
- A
Pub-Submessage is broadcast to other distributed instances to ensure their local caches are also evicted, maintaining consistency across the system.