forked from unionj-cloud/go-doudou
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,313 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
semi: false | ||
singleQuote: true | ||
proseWrap: always | ||
printWidth: 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Changelog | ||
|
||
> :heart: [**Uptrace.dev** - distributed traces, logs, and errors in one place](https://uptrace.dev) | ||
## v8 | ||
|
||
- Added s2 (snappy) compression. That means that v8 can't read the data set by v7. | ||
- Replaced LRU with TinyLFU for local cache. | ||
- Requires go-redis v8. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (c) 2016 The github.com/go-redis/cache Contributors. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
all: | ||
go test ./... | ||
go test ./... -short -race | ||
go test ./... -run=NONE -bench=. -benchmem | ||
env GOOS=linux GOARCH=386 go test ./... | ||
golangci-lint run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Redis cache library for Golang | ||
|
||
[![Build Status](https://travis-ci.org/go-redis/cache.svg)](https://travis-ci.org/go-redis/cache) | ||
[![GoDoc](https://godoc.org/github.com/go-redis/cache?status.svg)](https://pkg.go.dev/github.com/go-redis/cache/v9?tab=doc) | ||
|
||
> go-redis/cache is brought to you by :star: | ||
> [**uptrace/uptrace**](https://github.com/uptrace/uptrace). Uptrace is an open source and blazingly | ||
> fast [distributed tracing tool](https://get.uptrace.dev/) powered by OpenTelemetry and ClickHouse. | ||
> Give it a star as well! | ||
go-redis/cache library implements a cache using Redis as a key/value storage. It uses | ||
[MessagePack](https://github.com/vmihailenco/msgpack) to marshal values. | ||
|
||
Optionally, you can use [TinyLFU](https://github.com/dgryski/go-tinylfu) or any other | ||
[cache algorithm](https://github.com/vmihailenco/go-cache-benchmark) as a local in-process cache. | ||
|
||
If you are interested in monitoring cache hit rate, see the guide for | ||
[Monitoring using OpenTelemetry Metrics](https://blog.uptrace.dev/posts/opentelemetry-metrics-cache-stats/). | ||
|
||
## Installation | ||
|
||
go-redis/cache supports 2 last Go versions and requires a Go version with | ||
[modules](https://github.com/golang/go/wiki/Modules) support. So make sure to initialize a Go | ||
module: | ||
|
||
```shell | ||
go mod init github.com/my/repo | ||
``` | ||
|
||
And then install go-redis/cache/v9 (note _v9_ in the import; omitting it is a popular mistake): | ||
|
||
```shell | ||
go get github.com/go-redis/cache/v9 | ||
``` | ||
|
||
## Quickstart | ||
|
||
```go | ||
package cache_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
"github.com/unionj-cloud/go-doudou/v2/toolkit/cache" | ||
) | ||
|
||
type Object struct { | ||
Str string | ||
Num int | ||
} | ||
|
||
func Example_basicUsage() { | ||
ring := redis.NewRing(&redis.RingOptions{ | ||
Addrs: map[string]string{ | ||
"server1": ":6379", | ||
"server2": ":6380", | ||
}, | ||
}) | ||
|
||
mycache := cache.New(&cache.Options{ | ||
Redis: ring, | ||
LocalCache: cache.NewTinyLFU(1000, time.Minute), | ||
}) | ||
|
||
ctx := context.TODO() | ||
key := "mykey" | ||
obj := &Object{ | ||
Str: "mystring", | ||
Num: 42, | ||
} | ||
|
||
if err := mycache.Set(&cache.Item{ | ||
Ctx: ctx, | ||
Key: key, | ||
Value: obj, | ||
TTL: time.Hour, | ||
}); err != nil { | ||
panic(err) | ||
} | ||
|
||
var wanted Object | ||
if err := mycache.Get(ctx, key, &wanted); err == nil { | ||
fmt.Println(wanted) | ||
} | ||
|
||
// Output: {mystring 42} | ||
} | ||
|
||
func Example_advancedUsage() { | ||
ring := redis.NewRing(&redis.RingOptions{ | ||
Addrs: map[string]string{ | ||
"server1": ":6379", | ||
"server2": ":6380", | ||
}, | ||
}) | ||
|
||
mycache := cache.New(&cache.Options{ | ||
Redis: ring, | ||
LocalCache: cache.NewTinyLFU(1000, time.Minute), | ||
}) | ||
|
||
obj := new(Object) | ||
err := mycache.Once(&cache.Item{ | ||
Key: "mykey", | ||
Value: obj, // destination | ||
Do: func(*cache.Item) (interface{}, error) { | ||
return &Object{ | ||
Str: "mystring", | ||
Num: 42, | ||
}, nil | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(obj) | ||
// Output: &{mystring 42} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cache_test | ||
|
||
import ( | ||
"github.com/unionj-cloud/go-doudou/v2/toolkit/cache" | ||
"strings" | ||
"testing" | ||
|
||
) | ||
|
||
func BenchmarkOnce(b *testing.B) { | ||
mycache := newCacheWithLocal(newRing()) | ||
obj := &Object{ | ||
Str: strings.Repeat("my very large string", 10), | ||
Num: 42, | ||
} | ||
|
||
b.ResetTimer() | ||
|
||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
var dst Object | ||
err := mycache.Once(&cache.Item{ | ||
Key: "bench-once", | ||
Value: &dst, | ||
Do: func(*cache.Item) (interface{}, error) { | ||
return obj, nil | ||
}, | ||
}) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if dst.Num != 42 { | ||
b.Fatalf("%d != 42", dst.Num) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkSet(b *testing.B) { | ||
mycache := newCacheWithLocal(newRing()) | ||
obj := &Object{ | ||
Str: strings.Repeat("my very large string", 10), | ||
Num: 42, | ||
} | ||
|
||
b.ResetTimer() | ||
|
||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
if err := mycache.Set(&cache.Item{ | ||
Key: "bench-set", | ||
Value: obj, | ||
}); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.