github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/cache/README.md (about) 1 # Redis cache library for Golang 2 3 [](https://travis-ci.org/go-redis/cache) 4 [](https://pkg.go.dev/github.com/go-redis/cache/v9?tab=doc) 5 6 > go-redis/cache is brought to you by :star: 7 > [**uptrace/uptrace**](https://github.com/uptrace/uptrace). Uptrace is an open source and blazingly 8 > fast [distributed tracing tool](https://get.uptrace.dev/) powered by OpenTelemetry and ClickHouse. 9 > Give it a star as well! 10 11 go-redis/cache library implements a cache using Redis as a key/value storage. It uses 12 [MessagePack](https://github.com/vmihailenco/msgpack) to marshal values. 13 14 Optionally, you can use [TinyLFU](https://github.com/dgryski/go-tinylfu) or any other 15 [cache algorithm](https://github.com/vmihailenco/go-cache-benchmark) as a local in-process cache. 16 17 If you are interested in monitoring cache hit rate, see the guide for 18 [Monitoring using OpenTelemetry Metrics](https://blog.uptrace.dev/posts/opentelemetry-metrics-cache-stats/). 19 20 ## Installation 21 22 go-redis/cache supports 2 last Go versions and requires a Go version with 23 [modules](https://github.com/golang/go/wiki/Modules) support. So make sure to initialize a Go 24 module: 25 26 ```shell 27 go mod init github.com/my/repo 28 ``` 29 30 And then install go-redis/cache/v9 (note _v9_ in the import; omitting it is a popular mistake): 31 32 ```shell 33 go get github.com/go-redis/cache/v9 34 ``` 35 36 ## Quickstart 37 38 ```go 39 package cache_test 40 41 import ( 42 "context" 43 "fmt" 44 "time" 45 46 "github.com/redis/go-redis/v9" 47 "github.com/unionj-cloud/go-doudou/v2/toolkit/cache" 48 ) 49 50 type Object struct { 51 Str string 52 Num int 53 } 54 55 func Example_basicUsage() { 56 ring := redis.NewRing(&redis.RingOptions{ 57 Addrs: map[string]string{ 58 "server1": ":6379", 59 "server2": ":6380", 60 }, 61 }) 62 63 mycache := cache.New(&cache.Options{ 64 Redis: ring, 65 LocalCache: cache.NewTinyLFU(1000, time.Minute), 66 }) 67 68 ctx := context.TODO() 69 key := "mykey" 70 obj := &Object{ 71 Str: "mystring", 72 Num: 42, 73 } 74 75 if err := mycache.Set(&cache.Item{ 76 Ctx: ctx, 77 Key: key, 78 Value: obj, 79 TTL: time.Hour, 80 }); err != nil { 81 panic(err) 82 } 83 84 var wanted Object 85 if err := mycache.Get(ctx, key, &wanted); err == nil { 86 fmt.Println(wanted) 87 } 88 89 // Output: {mystring 42} 90 } 91 92 func Example_advancedUsage() { 93 ring := redis.NewRing(&redis.RingOptions{ 94 Addrs: map[string]string{ 95 "server1": ":6379", 96 "server2": ":6380", 97 }, 98 }) 99 100 mycache := cache.New(&cache.Options{ 101 Redis: ring, 102 LocalCache: cache.NewTinyLFU(1000, time.Minute), 103 }) 104 105 obj := new(Object) 106 err := mycache.Once(&cache.Item{ 107 Key: "mykey", 108 Value: obj, // destination 109 Do: func(*cache.Item) (interface{}, error) { 110 return &Object{ 111 Str: "mystring", 112 Num: 42, 113 }, nil 114 }, 115 }) 116 if err != nil { 117 panic(err) 118 } 119 fmt.Println(obj) 120 // Output: &{mystring 42} 121 } 122 ```