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.
go get -u github.com/bartventer/gocache
Name | Author | Docs |
---|---|---|
Redis | go-redis/redis | |
Redis Cluster | go-redis/redis | |
Memcache | bradfitz/gomemcache | |
RAM Cache (in-memory) | bartventer/gocache |
Note: More coming soon!
See the Contributing section if you would like to add a new cache implementation.
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.
The redis package provides a Redis cache driver using the go-redis/redis client.
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
}
You can create a Redis cache with redis.New:
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
}
The rediscluster package provides a Redis Cluster cache driver using the go-redis/redis client.
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
}
You can create a Redis Cluster cache with rediscluster.New:
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
}
The memcache package provides a Memcache cache driver using the bradfitz/gomemcache client.
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
}
You can create a Memcache cache with memcache.New:
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
}
The ramcache package provides an in-memory cache driver using a map.
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
}
You can create a RAM cache with ramcache.New:
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
}
All contributions are welcome! See the Contributing Guide for more details.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.