-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathristretto_test.go
More file actions
44 lines (35 loc) · 873 Bytes
/
ristretto_test.go
File metadata and controls
44 lines (35 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package example
import (
"context"
"fmt"
"time"
"github.com/abema/crema"
cremaristretto "github.com/abema/crema/ext/ristretto"
dgraphristretto "github.com/dgraph-io/ristretto"
)
func ExampleRistrettoCacheProvider() {
cache, err := dgraphristretto.NewCache(&dgraphristretto.Config{
NumCounters: 1e6,
MaxCost: 1 << 30,
BufferItems: 64,
})
if err != nil {
fmt.Println(err)
return
}
provider, err := cremaristretto.NewRistrettoCacheProvider[crema.CacheObject[string]](cache)
if err != nil {
fmt.Println(err)
return
}
cremaCache := crema.NewCache(provider, crema.NoopCacheStorageCodec[string]{})
value, err := cremaCache.GetOrLoad(context.Background(), "greeting", time.Minute, func(ctx context.Context) (string, error) {
return "hello", nil
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(value)
// Output: hello
}