Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move each store into its own go module #183

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
34 changes: 27 additions & 7 deletions .github/workflows/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,39 @@ on:
- '*'

jobs:
test:
test_lib:
runs-on: ubuntu-latest
strategy:
matrix:
go_version: [ '1.18', '1.19' ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}

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

- name: Run go tests
run: go test -v -race -cover -coverprofile=coverage.txt -covermode=atomic ./...
test_stores:
runs-on: ubuntu-latest
strategy:
matrix:
go_version: [ '1.18', '1.19' ]
store:
- bigcache
- freecache
- go_cache
- memcache
- pegasus
- redis
- rediscluster
- ristretto
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go_version }}

- name: Run stores go tests
run: cd store/${{ matrix.store }}; go test -v -race ./...
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2021 Vincent Composieux <gocache@composieux.fr>
Copyright (c) 2022 Vincent Composieux <gocache@composieux.fr>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 21 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
.PHONY: mocks test benchmark-store

mocks:
mockgen -source=cache/interface.go -destination=test/mocks/cache/cache_interface.go -package=mocks
mockgen -source=codec/interface.go -destination=test/mocks/codec/codec_interface.go -package=mocks
mockgen -source=metrics/interface.go -destination=test/mocks/metrics/metrics_interface.go -package=mocks
mockgen -source=store/interface.go -destination=test/mocks/store/store_interface.go -package=mocks
mockgen -source=store/bigcache.go -destination=test/mocks/store/clients/bigcache_interface.go -package=mocks
mockgen -source=store/memcache.go -destination=test/mocks/store/clients/memcache_interface.go -package=mocks
mockgen -source=store/redis.go -destination=test/mocks/store/clients/redis_interface.go -package=mocks
mockgen -source=store/rediscluster.go -destination=test/mocks/store/clients/rediscluster_interface.go -package=mocks
mockgen -source=store/ristretto.go -destination=test/mocks/store/clients/ristretto_interface.go -package=mocks
mockgen -source=store/freecache.go -destination=test/mocks/store/clients/freecache_interface.go -package=mocks
mockgen -source=store/go_cache.go -destination=test/mocks/store/clients/go_cache_interface.go -package=mocks
mockgen -source=cache/interface.go -destination=lib/cache/cache_mock.go -package=cache
mockgen -source=codec/interface.go -destination=lib/codec/codec_mock.go -package=codec
mockgen -source=metrics/interface.go -destination=lib/metrics/metrics_mock.go -package=metrics
mockgen -source=store/interface.go -destination=lib/store/store_mock.go -package=store
mockgen -source=store/bigcache/bigcache.go -destination=store/bigcache_mock.go -package=bigcache
mockgen -source=store/memcache/memcache.go -destination=store/memcache_mock.go -package=memcache
mockgen -source=store/redis/redis.go -destination=store/redis_mock.go -package=redis
mockgen -source=store/rediscluster/rediscluster.go -destination=store/rediscluster_mock.go -package=rediscluster
mockgen -source=store/ristretto/ristretto.go -destination=store/ristretto_mock.go -package=ristretto
mockgen -source=store/freecache/freecache.go -destination=store/freecache_mock.go -package=freecache
mockgen -source=store/go_cache/go_cache.go -destination=store/go_cache_mock.go -package=go_cache

test:
GOGC=10 go test -p=4 ./...
benchmark-store:
cd store && go test -bench=. -benchmem -benchtime=1s -count=1 -run=none
cd lib; GOGC=10 go test -v -p=4 ./...
cd store/bigcache; GOGC=10 go test -v -p=4 ./...
cd store/freecache; GOGC=10 go test -v -p=4 ./...
cd store/go_cache; GOGC=10 go test -v -p=4 ./...
cd store/memcache; GOGC=10 go test -v -p=4 ./...
cd store/pegasus; GOGC=10 go test -v -p=4 ./...
cd store/redis; GOGC=10 go test -v -p=4 ./...
cd store/rediscluster; GOGC=10 go test -v -p=4 ./...
cd store/ristretto; GOGC=10 go test -v -p=4 ./...
91 changes: 51 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ Here is what it brings in detail:

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

## Installation

To begin working with the latest version of gocache, you can import the library in your project:

```go
go get github.com/eko/gocache/v4/lib
```

and then, import the store(s) you want to use between all available ones:

```go
go get github.com/eko/gocache/v4/store/bigcache
go get github.com/eko/gocache/v4/store/freecache
go get github.com/eko/gocache/v4/store/go_cache
go get github.com/eko/gocache/v4/store/memcache
go get github.com/eko/gocache/v4/store/pegasus
go get github.com/eko/gocache/v4/store/redis
go get github.com/eko/gocache/v4/store/rediscluster
go get github.com/eko/gocache/v4/store/ristretto
```

Then, simply use the following import statements:

```go
import (
"github.com/eko/gocache/v4/lib/cache"
"github.com/eko/gocache/v4/store/redis"
)
```

If you run into any errors, please be sure to run `go mod tidy` to clean your go.mod file.

## Available cache features in detail

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

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

```go
bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Minute))
bigcacheStore := store.NewBigcache(bigcacheClient)
bigcacheStore := bigcache_store.NewBigcache(bigcacheClient)

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

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

```go
gocacheClient := gocache.New(5*time.Minute, 10*time.Minute)
gocacheStore := store.NewGoCache(gocacheClient)
gocacheStore := gocache_store.NewGoCache(gocacheClient)

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

```go
redisStore := store.NewRedis(redis.NewClient(&redis.Options{
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
}))

Expand All @@ -151,7 +183,7 @@ switch err {
#### Freecache

```go
freecacheStore := store.NewFreecache(freecache.NewCache(1000), store.WithExpiration(10 * time.Second))
freecacheStore := freecache_store.NewFreecache(freecache.NewCache(1000), store.WithExpiration(10 * time.Second))

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

```go
pegasusStore, err := store.NewPegasus(&store.OptionsPegasus{
pegasusStore, err := pegasus_store.NewPegasus(&store.OptionsPegasus{
MetaServers: []string{"127.0.0.1:34601", "127.0.0.1:34602", "127.0.0.1:34603"},
})

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

// Initialize stores
ristrettoStore := store.NewRistretto(ristrettoCache)
redisStore := store.NewRedis(redisClient, store.WithExpiration(5*time.Second))
ristrettoStore := ristretto_store.NewRistretto(ristrettoCache)
redisStore := redis_store.NewRedis(redisClient, store.WithExpiration(5*time.Second))

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

// Initialize Redis client and store
redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379"})
redisStore := store.NewRedis(redisClient)
redisStore := redis_store.NewRedis(redisClient)

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

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

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

// Initialize chained cache
cacheManager := cache.NewMetric[*Book](
Expand Down Expand Up @@ -353,13 +385,13 @@ package main

import (
"log"
"github.com/eko/gocache/v3/generic"
"github.com/eko/gocache/v3/cache"
"github.com/eko/gocache/v3/store"
"github.com/eko/gocache/v4/lib/generic"
"github.com/eko/gocache/v4/lib/cache"
"github.com/eko/gocache/v4/lib/store"
)

func main() {
redisStore := store.NewRedis(redis.NewClient(&redis.Options{
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
}), nil)

Expand All @@ -378,26 +410,6 @@ func main() {
}
```

## Installation

To begin working with the latest version of go-cache, you can use the following command:

```go
go get github.com/eko/gocache/v3
```

To avoid any errors when trying to import your libraries use the following import statement:

```go
import (
"github.com/eko/gocache/v3/cache"
"github.com/eko/gocache/v3/store"
)
```

If you run into any errors, please be sure to run `go mod tidy` to clean your go.mod file.


### Write your own custom cache

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

## Run tests

Generate mocks:
To generate mocks usng mockgen library, run:

```bash
$ go get github.com/golang/mock/mockgen
$ make mocks
```

Test suite can be run with:

```bash
$ make test # run unit test
$ make benchmark-store # run benchmark test
```

## Community
Expand Down
51 changes: 0 additions & 51 deletions go.mod

This file was deleted.

4 changes: 2 additions & 2 deletions cache/cache.go → lib/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"reflect"
"time"

"github.com/eko/gocache/v3/codec"
"github.com/eko/gocache/v3/store"
"github.com/eko/gocache/v4/lib/codec"
"github.com/eko/gocache/v4/lib/store"
)

const (
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading