Query Cache
Opened this issue · 1 comments
bounoable commented
Implement a flexible cache for event queries.
Features
- Automatic cache key based on the query filters
- Time-based and manual expiry
- Optional cache storage
Examples
Basic usage
package example
import (
"github.com/modernice/goes/event"
"github.com/modernice/goes/event/query"
)
func example(store event.Store, q event.Query) {
cache := query.NewCache(store)
result, err := cache.Result(context.TODO(), q)
// handle err
if result.Cached {
log.Println("cache returned cached result")
} else {
log.Println("cache executed query")
}
events, err := streams.Drain(context.TODO(), result.Events, result.Errs)
// handle err
// alternatively
str, errs, err := cache.Run(context.TODO(), q)
// handle err
events, err := streams.Drain(context.TODO(), str, errs)
}
bounoable commented
This would allow for an aggregate cache:
package example
func example(repo aggregate.Repository) {
cached := repository.Cached(repo)
var foo aggregate.Aggregate
err := cached.Fetch(context.TODO(), foo)
}