This example demonstrates a typical cache-aside workflow with pacecache and an in-memory user repository.
- Configuring a bounded generic cache with TTL and jitter
- Configuring a default loader and loading missing values with
GetOrLoad - Reading cached values and expiration metadata with
GetandGetEntry - Handling missing loader results without caching them
- Deleting cached values and reloading them from the repository
- Inspecting cache state and cumulative statistics
Run the example from this directory:
go run .Or from the repository root:
go run ./examples/basiccache:
- first lookup: found=true user={ID:42 Name:Ada}
- direct lookup: user={ID:42 Name:Ada}
- entry has expiration: true
- repository loads: 1
not found:
- first lookup: found=false
- second lookup: found=false
- repository loads: 3
deletion:
- lookup after deletion: found=true user={ID:42 Name:Ada}
- repository loads: 4
stats:
- entries=1 hits=2 misses=4
- loads_found=2 loads_not_found=2 load_errors=0
- deleted_entries=1 evictions=0 expirations=0
- Cache-Aside Loading: The first lookup for user
42loads the value from the repository and stores it in the cache. SubsequentGetandGetEntryoperations return the cached value without calling the repository again, so the repository load count remains at1. - Missing Results: Missing results are not cached. Looking up user
404twice therefore invokes the repository twice and produces twonot_foundloader outcomes. - Deletion: After
Delete(42), the next lookup loads the value from the repository again and stores the fresh result in the cache, increasing the repository load count to4.