- 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
43 lines
928 B
Go
43 lines
928 B
Go
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
|
|
}
|