|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/dgraph-io/ristretto" |
| 9 | + "github.com/eko/gocache/lib/v4/cache" |
| 10 | + "github.com/eko/gocache/lib/v4/metrics" |
| 11 | + "github.com/eko/gocache/lib/v4/store" |
| 12 | + store_ristretto "github.com/eko/gocache/store/ristretto/v4" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + ctx := context.Background() |
| 17 | + |
| 18 | + ristrettoClient1, err := ristretto.NewCache( |
| 19 | + &ristretto.Config{ |
| 20 | + NumCounters: 1_000, |
| 21 | + MaxCost: 100_000_000, |
| 22 | + BufferItems: 64, |
| 23 | + }, |
| 24 | + ) |
| 25 | + if err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + |
| 29 | + ristrettoClient2, err := ristretto.NewCache( |
| 30 | + &ristretto.Config{ |
| 31 | + NumCounters: 1_000, |
| 32 | + MaxCost: 100_000_000, |
| 33 | + BufferItems: 64, |
| 34 | + }, |
| 35 | + ) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + |
| 40 | + // First: memory store |
| 41 | + memoryStore1 := store_ristretto.NewRistretto( |
| 42 | + ristrettoClient1, |
| 43 | + store.WithExpiration(1*time.Minute), |
| 44 | + ) |
| 45 | + memoryCache1 := cache.New[[]byte](memoryStore1) |
| 46 | + |
| 47 | + // Second: memory store |
| 48 | + memoryStore2 := store_ristretto.NewRistretto( |
| 49 | + ristrettoClient2, |
| 50 | + store.WithExpiration(1*time.Minute), |
| 51 | + ) |
| 52 | + memoryCache2 := cache.New[[]byte](memoryStore2) |
| 53 | + |
| 54 | + // Chain cache |
| 55 | + chainCache := cache.NewChain[[]byte]( |
| 56 | + memoryCache1, |
| 57 | + memoryCache2, |
| 58 | + ) |
| 59 | + |
| 60 | + // Wrap chain cache with metrics cache |
| 61 | + promMetrics := metrics.NewPrometheus( |
| 62 | + "manifest-api", |
| 63 | + ) |
| 64 | + |
| 65 | + metricCache := cache.NewMetric[[]byte]( |
| 66 | + promMetrics, |
| 67 | + chainCache, |
| 68 | + ) |
| 69 | + |
| 70 | + // Loadable |
| 71 | + loadableCache := cache.NewLoadable[[]byte]( |
| 72 | + func(ctx context.Context, key any) ([]byte, []store.Option, error) { |
| 73 | + return []byte(`ok-1`), nil, nil |
| 74 | + }, |
| 75 | + metricCache, |
| 76 | + ) |
| 77 | + |
| 78 | + loadableCache.Set(ctx, "my-key-1", []byte(`value-1`)) |
| 79 | + time.Sleep(100 * time.Millisecond) |
| 80 | + |
| 81 | + // Remove from first cache, will be fetch from second cache and |
| 82 | + // set back to first cache. |
| 83 | + memoryCache1.Delete(ctx, "my-key-1") |
| 84 | + time.Sleep(100 * time.Millisecond) |
| 85 | + |
| 86 | + // Ensure value has been cached in both. |
| 87 | + value1, _ := memoryCache1.Get(ctx, "my-key-1") |
| 88 | + time.Sleep(100 * time.Millisecond) |
| 89 | + fmt.Println("value1:", string(value1)) |
| 90 | + |
| 91 | + value2, _ := memoryCache2.Get(ctx, "my-key-1") |
| 92 | + time.Sleep(100 * time.Millisecond) |
| 93 | + fmt.Println("value2:", string(value2)) |
| 94 | + |
| 95 | + // Retrieve final value. |
| 96 | + value, err := loadableCache.Get(ctx, "my-key-1") |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + time.Sleep(100 * time.Millisecond) |
| 101 | + fmt.Println("final:", string(value)) |
| 102 | + |
| 103 | + // Retrieve from every cache independently. |
| 104 | + value12, _ := memoryCache1.Get(ctx, "my-key-1") |
| 105 | + time.Sleep(100 * time.Millisecond) |
| 106 | + fmt.Println("value1:", string(value12)) |
| 107 | + |
| 108 | + value22, _ := memoryCache2.Get(ctx, "my-key-1") |
| 109 | + time.Sleep(100 * time.Millisecond) |
| 110 | + fmt.Println("value2:", string(value22)) |
| 111 | +} |
0 commit comments