A set of Cache Adapters for various distributed systems (like Redis) written in Go.
Now with a new MultiCacheAdapter
implementation!
- MultiCache -> Leverages the possibility to use multiple cache adapters at the same time, useful to create fallbacks in case one or more of the cache service you specified gives temporary errors.
- Redis -> using
github.com/gomodule/redigo
- MongoDB -> using
github.com/mongodb/mongo-go-driver
- InMemory -> Uses a map of objects with expiration of keys
Just check the GoDocs
Just use the standard way
go get github.com/tryvium-travels/golang-cache-adapters
or use go modules, it should take care of this automagically.
The CacheAdapter
interface offers 2 main ways to access a cache: Get
and Set
methods.
You can use Delete
and SetTTL
functions as well. For more info, check the docs.
This example creates a new RedisAdapter
and uses it, but you can replace it with any of the other
supported Adapters.
To know how to use the MultiCacheAdapter
check out the docs.
package main
import (
"log"
"time"
"github.com/gomodule/redigo/redis"
cacheadapters "github.com/tryvium-travels/golang-cache-adapters"
rediscacheadapters "github.com/tryvium-travels/golang-cache-adapters/redis"
)
func main() {
redisURI := "rediss://my-redis-instance-uri:port"
myRedisPool := &redis.Pool{
Dial: func() (redis.Conn, error) {
// obtain a redis connection, there
// are plenty of ways to do that.
return redis.DialURL(redisURI)
},
}
exampleTTL := time.Hour
adapter, err := rediscacheadapters.New(myRedisPool, exampleTTL)
if err != nil {
// remember to check for errors
log.Fatalf("Adapter initialization error: %s", err)
}
type exampleStruct struct {
Value string
}
exampleKey := "a:redis:key"
var exampleValue exampleStruct
err = adapter.Get(exampleKey, &exampleValue)
if err != nil {
// remember to check for errors
log.Fatalf("adapter.Get error: %s", err)
}
exampleKey = "another:redis:key"
// nil TTL represents the default value put in the New function
err = adapter.Set(exampleKey, exampleValue, nil)
if err != nil {
// remember to check for errors
log.Fatalf("adapter.Get error: %s", err)
}
}
First of all, thank you!
To contribute to our project:
- Open an Issue signaling bugs or proposals using the provided templates
- If you are going to add a new adapter, please stick to the NAMING CONVENTION for branch, package, folder, file and adapter names.
- We require a total test coverage (100%) if we are going to accept a new adapter, we can help you if you have any difficulties to achieve that.
- Please be polite in issue and PR discussions and do not offend other people.
The following naming convention applies if you want to contribute:
-
Branch name should follow the following form
adapters/{{ADAPTER_NAME}}-#{{GITHUB_ISSUE_ID}}
Example branch name:
adapters/redis-#1
-
If you add a new adapter:
The name of the folder must match the lowercase adapter name (e.g. redis/mongodb/memcached)
We strongly suggest you to create an adapter and a session_adapter go file, although it is not required.
Example file names:
redis_adapter.go
andredis_session_adapter.go
Example test file names:
redis_adapter_test.go
andredis_session_adapter_test.go
.There must also be test files with 100% test coverage.