Skip to content

Commit 2a92702

Browse files
authored
Merge pull request #183 from eko/store-modules
Move each store into its own go module
2 parents 0b81e39 + e68f580 commit 2a92702

File tree

1,275 files changed

+1382
-410969
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,275 files changed

+1382
-410969
lines changed

.github/workflows/all.yml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,39 @@ on:
99
- '*'
1010

1111
jobs:
12-
test:
12+
test_lib:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
1616
go_version: [ '1.18', '1.19' ]
1717
steps:
18-
- uses: actions/checkout@v2
19-
- uses: actions/setup-go@v1
18+
- uses: actions/checkout@v3
19+
- uses: actions/setup-go@v3
2020
with:
2121
go-version: ${{ matrix.go_version }}
2222

23-
- name: Install go dependencies
24-
run: go get -t -v ./...
23+
- name: Run library go tests
24+
run: cd lib; go test -v -race -cover -coverprofile=coverage.txt -covermode=atomic ./...
2525

26-
- name: Run go tests
27-
run: go test -v -race -cover -coverprofile=coverage.txt -covermode=atomic ./...
26+
test_stores:
27+
runs-on: ubuntu-latest
28+
strategy:
29+
matrix:
30+
go_version: [ '1.18', '1.19' ]
31+
store:
32+
- bigcache
33+
- freecache
34+
- go_cache
35+
- memcache
36+
- pegasus
37+
- redis
38+
- rediscluster
39+
- ristretto
40+
steps:
41+
- uses: actions/checkout@v3
42+
- uses: actions/setup-go@v3
43+
with:
44+
go-version: ${{ matrix.go_version }}
45+
46+
- name: Run stores go tests
47+
run: cd store/${{ matrix.store }}; go test -v -race ./...

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2021 Vincent Composieux <[email protected]>
1+
Copyright (c) 2022 Vincent Composieux <[email protected]>
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
.PHONY: mocks test benchmark-store
22

33
mocks:
4-
mockgen -source=cache/interface.go -destination=test/mocks/cache/cache_interface.go -package=mocks
5-
mockgen -source=codec/interface.go -destination=test/mocks/codec/codec_interface.go -package=mocks
6-
mockgen -source=metrics/interface.go -destination=test/mocks/metrics/metrics_interface.go -package=mocks
7-
mockgen -source=store/interface.go -destination=test/mocks/store/store_interface.go -package=mocks
8-
mockgen -source=store/bigcache.go -destination=test/mocks/store/clients/bigcache_interface.go -package=mocks
9-
mockgen -source=store/memcache.go -destination=test/mocks/store/clients/memcache_interface.go -package=mocks
10-
mockgen -source=store/redis.go -destination=test/mocks/store/clients/redis_interface.go -package=mocks
11-
mockgen -source=store/rediscluster.go -destination=test/mocks/store/clients/rediscluster_interface.go -package=mocks
12-
mockgen -source=store/ristretto.go -destination=test/mocks/store/clients/ristretto_interface.go -package=mocks
13-
mockgen -source=store/freecache.go -destination=test/mocks/store/clients/freecache_interface.go -package=mocks
14-
mockgen -source=store/go_cache.go -destination=test/mocks/store/clients/go_cache_interface.go -package=mocks
4+
mockgen -source=cache/interface.go -destination=lib/cache/cache_mock.go -package=cache
5+
mockgen -source=codec/interface.go -destination=lib/codec/codec_mock.go -package=codec
6+
mockgen -source=metrics/interface.go -destination=lib/metrics/metrics_mock.go -package=metrics
7+
mockgen -source=store/interface.go -destination=lib/store/store_mock.go -package=store
8+
mockgen -source=store/bigcache/bigcache.go -destination=store/bigcache_mock.go -package=bigcache
9+
mockgen -source=store/memcache/memcache.go -destination=store/memcache_mock.go -package=memcache
10+
mockgen -source=store/redis/redis.go -destination=store/redis_mock.go -package=redis
11+
mockgen -source=store/rediscluster/rediscluster.go -destination=store/rediscluster_mock.go -package=rediscluster
12+
mockgen -source=store/ristretto/ristretto.go -destination=store/ristretto_mock.go -package=ristretto
13+
mockgen -source=store/freecache/freecache.go -destination=store/freecache_mock.go -package=freecache
14+
mockgen -source=store/go_cache/go_cache.go -destination=store/go_cache_mock.go -package=go_cache
15+
1516
test:
16-
GOGC=10 go test -p=4 ./...
17-
benchmark-store:
18-
cd store && go test -bench=. -benchmem -benchtime=1s -count=1 -run=none
17+
cd lib; GOGC=10 go test -v -p=4 ./...
18+
cd store/bigcache; GOGC=10 go test -v -p=4 ./...
19+
cd store/freecache; GOGC=10 go test -v -p=4 ./...
20+
cd store/go_cache; GOGC=10 go test -v -p=4 ./...
21+
cd store/memcache; GOGC=10 go test -v -p=4 ./...
22+
cd store/pegasus; GOGC=10 go test -v -p=4 ./...
23+
cd store/redis; GOGC=10 go test -v -p=4 ./...
24+
cd store/rediscluster; GOGC=10 go test -v -p=4 ./...
25+
cd store/ristretto; GOGC=10 go test -v -p=4 ./...

README.md

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ Here is what it brings in detail:
3737

3838
* [Prometheus](https://github.com/prometheus/client_golang)
3939

40+
## Installation
41+
42+
To begin working with the latest version of gocache, you can import the library in your project:
43+
44+
```go
45+
go get github.com/eko/gocache/v4/lib
46+
```
47+
48+
and then, import the store(s) you want to use between all available ones:
49+
50+
```go
51+
go get github.com/eko/gocache/v4/store/bigcache
52+
go get github.com/eko/gocache/v4/store/freecache
53+
go get github.com/eko/gocache/v4/store/go_cache
54+
go get github.com/eko/gocache/v4/store/memcache
55+
go get github.com/eko/gocache/v4/store/pegasus
56+
go get github.com/eko/gocache/v4/store/redis
57+
go get github.com/eko/gocache/v4/store/rediscluster
58+
go get github.com/eko/gocache/v4/store/ristretto
59+
```
60+
61+
Then, simply use the following import statements:
62+
63+
```go
64+
import (
65+
"github.com/eko/gocache/v4/lib/cache"
66+
"github.com/eko/gocache/v4/store/redis"
67+
)
68+
```
69+
70+
If you run into any errors, please be sure to run `go mod tidy` to clean your go.mod file.
71+
4072
## Available cache features in detail
4173

4274
### A simple cache
@@ -46,7 +78,7 @@ Here is a simple cache instantiation with Redis but you can also look at other a
4678
#### Memcache
4779

4880
```go
49-
memcacheStore := store.NewMemcache(
81+
memcacheStore := memcache_store.NewMemcache(
5082
memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212"),
5183
store.WithExpiration(10*time.Second),
5284
)
@@ -70,7 +102,7 @@ cacheManager.Clear(ctx) // Clears the entire cache, in case you want to flush al
70102

71103
```go
72104
bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Minute))
73-
bigcacheStore := store.NewBigcache(bigcacheClient)
105+
bigcacheStore := bigcache_store.NewBigcache(bigcacheClient)
74106

75107
cacheManager := cache.New[[]byte](bigcacheStore)
76108
err := cacheManager.Set(ctx, "my-key", []byte("my-value"))
@@ -92,7 +124,7 @@ ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
92124
if err != nil {
93125
panic(err)
94126
}
95-
ristrettoStore := store.NewRistretto(ristrettoCache)
127+
ristrettoStore := ristretto_store.NewRistretto(ristrettoCache)
96128

97129
cacheManager := cache.New[string](ristrettoStore)
98130
err := cacheManager.Set(ctx, "my-key", "my-value", store.WithCost(2))
@@ -109,7 +141,7 @@ cacheManager.Delete(ctx, "my-key")
109141

110142
```go
111143
gocacheClient := gocache.New(5*time.Minute, 10*time.Minute)
112-
gocacheStore := store.NewGoCache(gocacheClient)
144+
gocacheStore := gocache_store.NewGoCache(gocacheClient)
113145

114146
cacheManager := cache.New[[]byte](gocacheStore)
115147
err := cacheManager.Set(ctx, "my-key", []byte("my-value"))
@@ -127,7 +159,7 @@ fmt.Printf("%s", value)
127159
#### Redis
128160

129161
```go
130-
redisStore := store.NewRedis(redis.NewClient(&redis.Options{
162+
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{
131163
Addr: "127.0.0.1:6379",
132164
}))
133165

@@ -151,7 +183,7 @@ switch err {
151183
#### Freecache
152184

153185
```go
154-
freecacheStore := store.NewFreecache(freecache.NewCache(1000), store.WithExpiration(10 * time.Second))
186+
freecacheStore := freecache_store.NewFreecache(freecache.NewCache(1000), store.WithExpiration(10 * time.Second))
155187

156188
cacheManager := cache.New[[]byte](freecacheStore)
157189
err := cacheManager.Set(ctx, "by-key", []byte("my-value"), opts)
@@ -165,7 +197,7 @@ value := cacheManager.Get(ctx, "my-key")
165197
#### Pegasus
166198

167199
```go
168-
pegasusStore, err := store.NewPegasus(&store.OptionsPegasus{
200+
pegasusStore, err := pegasus_store.NewPegasus(&store.OptionsPegasus{
169201
MetaServers: []string{"127.0.0.1:34601", "127.0.0.1:34602", "127.0.0.1:34603"},
170202
})
171203

@@ -197,8 +229,8 @@ if err != nil {
197229
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
198230

199231
// Initialize stores
200-
ristrettoStore := store.NewRistretto(ristrettoCache)
201-
redisStore := store.NewRedis(redisClient, store.WithExpiration(5*time.Second))
232+
ristrettoStore := ristretto_store.NewRistretto(ristrettoCache)
233+
redisStore := redis_store.NewRedis(redisClient, store.WithExpiration(5*time.Second))
202234

203235
// Initialize chained cache
204236
cacheManager := cache.NewChain[any](
@@ -223,7 +255,7 @@ type Book struct {
223255

224256
// Initialize Redis client and store
225257
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
226-
redisStore := store.NewRedis(redisClient)
258+
redisStore := redis_store.NewRedis(redisClient)
227259

228260
// Initialize a load function that loads your data from a custom source
229261
loadFunction := func(ctx context.Context, key any) (*Book, error) {
@@ -249,7 +281,7 @@ This cache will record metrics depending on the metric provider you pass to it.
249281
```go
250282
// Initialize Redis client and store
251283
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
252-
redisStore := store.NewRedis(redisClient)
284+
redisStore := redis_store.NewRedis(redisClient)
253285

254286
// Initializes Prometheus metrics service
255287
promMetrics := metrics.NewPrometheus("my-test-app")
@@ -270,7 +302,7 @@ Some caches like Redis stores and returns the value as a string so you have to m
270302
```go
271303
// Initialize Redis client and store
272304
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
273-
redisStore := store.NewRedis(redisClient)
305+
redisStore := redis_store.NewRedis(redisClient)
274306

275307
// Initialize chained cache
276308
cacheManager := cache.NewMetric[any](
@@ -313,7 +345,7 @@ Here is an example on how to use it:
313345
```go
314346
// Initialize Redis client and store
315347
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
316-
redisStore := store.NewRedis(redisClient)
348+
redisStore := redis_store.NewRedis(redisClient)
317349

318350
// Initialize chained cache
319351
cacheManager := cache.NewMetric[*Book](
@@ -353,13 +385,13 @@ package main
353385

354386
import (
355387
"log"
356-
"github.com/eko/gocache/v3/generic"
357-
"github.com/eko/gocache/v3/cache"
358-
"github.com/eko/gocache/v3/store"
388+
"github.com/eko/gocache/v4/lib/generic"
389+
"github.com/eko/gocache/v4/lib/cache"
390+
"github.com/eko/gocache/v4/lib/store"
359391
)
360392

361393
func main() {
362-
redisStore := store.NewRedis(redis.NewClient(&redis.Options{
394+
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{
363395
Addr: "127.0.0.1:6379",
364396
}), nil)
365397

@@ -378,26 +410,6 @@ func main() {
378410
}
379411
```
380412

381-
## Installation
382-
383-
To begin working with the latest version of go-cache, you can use the following command:
384-
385-
```go
386-
go get github.com/eko/gocache/v3
387-
```
388-
389-
To avoid any errors when trying to import your libraries use the following import statement:
390-
391-
```go
392-
import (
393-
"github.com/eko/gocache/v3/cache"
394-
"github.com/eko/gocache/v3/store"
395-
)
396-
```
397-
398-
If you run into any errors, please be sure to run `go mod tidy` to clean your go.mod file.
399-
400-
401413
### Write your own custom cache
402414

403415
Cache respect the following interface so you can write your own (proprietary?) cache logic if needed by implementing the following interface:
@@ -460,17 +472,16 @@ type CacheKeyGenerator interface {
460472

461473
## Run tests
462474

463-
Generate mocks:
475+
To generate mocks usng mockgen library, run:
476+
464477
```bash
465-
$ go get github.com/golang/mock/mockgen
466478
$ make mocks
467479
```
468480

469481
Test suite can be run with:
470482

471483
```bash
472484
$ make test # run unit test
473-
$ make benchmark-store # run benchmark test
474485
```
475486

476487
## Community

go.mod

Lines changed: 0 additions & 51 deletions
This file was deleted.

cache/cache.go renamed to lib/cache/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"reflect"
88
"time"
99

10-
"github.com/eko/gocache/v3/codec"
11-
"github.com/eko/gocache/v3/store"
10+
"github.com/eko/gocache/v4/lib/codec"
11+
"github.com/eko/gocache/v4/lib/store"
1212
)
1313

1414
const (

test/mocks/cache/cache_interface.go renamed to lib/cache/cache_mock.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)