https://github.com/bartventer/gocache
💾 GoCache is a versatile API for cache management in Go. It supports Redis, Redis Cluster, Memcache, and RAM Cache (in-memory), allowing seamless transitions between them. It simplifies both local testing and different environment deployments.
https://github.com/bartventer/gocache
cache go golang in-memory memcache portable redis
Last synced: 2 months ago
JSON representation
💾 GoCache is a versatile API for cache management in Go. It supports Redis, Redis Cluster, Memcache, and RAM Cache (in-memory), allowing seamless transitions between them. It simplifies both local testing and different environment deployments.
- Host: GitHub
- URL: https://github.com/bartventer/gocache
- Owner: bartventer
- License: apache-2.0
- Created: 2024-06-12T11:15:37.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-07T06:20:40.000Z (8 months ago)
- Last Synced: 2025-04-12T15:07:58.093Z (2 months ago)
- Topics: cache, go, golang, in-memory, memcache, portable, redis
- Language: Go
- Homepage:
- Size: 499 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Cache
[](https://pkg.go.dev/github.com/bartventer/gocache)
[](https://github.com/bartventer/gocache/releases/latest)
[](https://goreportcard.com/report/github.com/bartventer/gocache)
[](https://codecov.io/gh/bartventer/gocache)
[](https://github.com/bartventer/gocache/actions/workflows/default.yml)

[](LICENSE)The `Cache` package in Go provides a unified, portable API for managing caches, enabling developers to write cache-related code once and transition seamlessly between different cache drivers with minimal reconfiguration. This approach simplifies both local testing and deployment to different environments.
## Installation
```bash
go get -u github.com/bartventer/gocache
```## Supported Cache Implementations
| Name | Author | Docs |
|------|--------|------|
| Redis | [go-redis/redis](https://github.com/go-redis/redis) | [](https://pkg.go.dev/github.com/bartventer/gocache/redis) |
| Redis Cluster | [go-redis/redis](https://github.com/go-redis/redis) | [](https://pkg.go.dev/github.com/bartventer/gocache/rediscluster) |
| Memcache | [bradfitz/gomemcache](https://github.com/bradfitz/gomemcache) | [](https://pkg.go.dev/github.com/bartventer/gocache/memcache) |
| RAM Cache (in-memory) | [bartventer/gocache](https://github.com/bartventer/gocache) | [](https://pkg.go.dev/github.com/bartventer/gocache/ramcache) |_**Note**: More coming soon!_
_See the [Contributing](#contributing) section if you would like to add a new cache implementation._
## Usage
To use a cache implementation, import the relevant driver package and use the `OpenCache` function to create a new cache. The cache package will automatically use the correct cache driver based on the URL scheme. Each driver also provides a constructor function for manual initialization.
### Redis
The [redis](https://pkg.go.dev/github.com/bartventer/gocache/redis) package provides a [Redis](https://redis.io) cache driver using the [go-redis/redis](https://github.com/go-redis/redis) client.
```go
import (
"context"
"log"cache "github.com/bartventer/gocache"
_ "github.com/bartventer/gocache/redis"
)func main() {
ctx := context.Background()
urlStr := "redis://localhost:7000?maxretries=5&minretrybackoff=1s"
c, err := cache.OpenCache(ctx, urlStr)
if err != nil {
log.Fatalf("Failed to initialize cache: %v", err)
}
// ... use c with the cache.Cache interface
}
```#### Redis Constructor
You can create a Redis cache with [redis.New](https://pkg.go.dev/github.com/bartventer/gocache/redis#New):
```go
import (
"context""github.com/bartventer/gocache/redis"
)func main() {
ctx := context.Background()
c := redis.New[string](ctx, &redis.Options{
RedisOptions: &redis.RedisOptions{
Addr: "localhost:7000",
MaxRetries: 5,
MinRetryBackoff: 1 * time.Second,
},
})
// ... use c with the cache.Cache interface
}
```### Redis Cluster
The [rediscluster](https://pkg.go.dev/github.com/bartventer/gocache/rediscluster) package provides a [Redis Cluster](https://redis.io/topics/cluster-spec) cache driver using the [go-redis/redis](https://github.com/go-redis/redis) client.
```go
import (
"context"
"log"cache "github.com/bartventer/gocache"
_ "github.com/bartventer/gocache/rediscluster"
)func main() {
ctx := context.Background()
urlStr := "rediscluster://localhost:7000,localhost:7001,localhost:7002?maxretries=5&minretrybackoff=1s"
c, err := cache.OpenCache(ctx, urlStr)
if err != nil {
log.Fatalf("Failed to initialize cache: %v", err)
}
// ... use c with the cache.Cache interface
}
```#### Redis Cluster Constructor
You can create a Redis Cluster cache with [rediscluster.New](https://pkg.go.dev/github.com/bartventer/gocache/rediscluster#New):
```go
import (
"context""github.com/bartventer/gocache/rediscluster"
)func main() {
ctx := context.Background()
c := rediscluster.New[string](ctx, &rediscluster.Options{
ClusterOptions: &rediscluster.ClusterOptions{
Addrs: []string{"localhost:7000", "localhost:7001", "localhost:7002"},
MaxRetries: 5,
MinRetryBackoff: 1 * time.Second,
},
})
// ... use c with the cache.Cache interface
}
```### Memcache
The [memcache](https://pkg.go.dev/github.com/bartventer/gocache/memcache) package provides a [Memcache](https://memcached.org) cache driver using the [bradfitz/gomemcache](https://github.com/bradfitz/gomemcache) client.
```go
import (
"context"
"log"cache "github.com/bartventer/gocache"
_ "github.com/bartventer/gocache/memcache"
)func main() {
ctx := context.Background()
urlStr := "memcache://localhost:11211"
c, err := cache.OpenCache(ctx, urlStr)
if err != nil {
log.Fatalf("Failed to initialize cache: %v", err)
}
// ... use c with the cache.Cache interface
}
```#### Memcache Constructor
You can create a Memcache cache with [memcache.New](https://pkg.go.dev/github.com/bartventer/gocache/memcache#New):
```go
import (
"context""github.com/bartventer/gocache/memcache"
)func main() {
ctx := context.Background()
c := memcache.New[string](ctx, &memcache.Options{
Addrs: []string{"localhost:11211"},
})
// ... use c with the cache.Cache interface
}
```### RAM Cache (in-memory)
The [ramcache](https://pkg.go.dev/github.com/bartventer/gocache/ramcache) package provides an in-memory cache driver using a map.
```go
import (
"context"
"log"cache "github.com/bartventer/gocache"
_ "github.com/bartventer/gocache/ramcache"
)func main() {
ctx := context.Background()
urlStr := "ramcache://?cleanupinterval=1m"
c, err := cache.OpenCache(ctx, urlStr)
if err != nil {
log.Fatalf("Failed to initialize cache: %v", err)
}
// ... use c with the cache.Cache interface
}
```#### RAM Cache Constructor
You can create a RAM cache with [ramcache.New](https://pkg.go.dev/github.com/bartventer/gocache/ramcache#New):
```go
import (
"context""github.com/bartventer/gocache/ramcache"
)func main() {
ctx := context.Background()
c := ramcache.New[string](ctx, &ramcache.Options{
CleanupInterval: 1 * time.Minute,
})
// ... use c with the cache.Cache interface
}
```## Contributing
All contributions are welcome! See the [Contributing Guide](CONTRIBUTING.md) for more details.
## License
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.