feat(19.3-01): add cooler.Cache with otter memory and Redis drivers
- Cache: Get, Set, Has, Forget, Remember (degrades on cache errors), Increment, Flush with per-entry TTLs under <prefix>cache: - memory driver on otter v2.3.0 bounded by cache.memory.max_entries; Redis driver with SET PX, INCRBY and a Flush limited to SCAN MATCH <prefix>cache:* plus UNLINK - Install publishes the Cache (sharing one Redis client with the rate limiter); FromApp serves jobs and commands without a dial - new docs/services/caching.md, configuration rows, cooler README
This commit is contained in:
42
modules/cooler/example_test.go
Normal file
42
modules/cooler/example_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package cooler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/cooler"
|
||||
)
|
||||
|
||||
func ExampleFromApp() {
|
||||
// serve publishes the Cache at start-up; a job or console command asks
|
||||
// FromApp, which builds the configured driver (memory here) once.
|
||||
app := backpack.New(nil)
|
||||
cache, err := cooler.FromApp(app)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
ctx := context.Background()
|
||||
loads := 0
|
||||
popular := func(context.Context) ([]byte, error) {
|
||||
loads++
|
||||
return json.Marshal([]string{"Hello", "Summer"})
|
||||
}
|
||||
var titles []string
|
||||
for range 2 {
|
||||
raw, err := cache.Remember(ctx, "blog:popular", 10*time.Minute, popular)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if err := json.Unmarshal(raw, &titles); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Println(titles, loads)
|
||||
// Output: [Hello Summer] 1
|
||||
}
|
||||
Reference in New Issue
Block a user