A CacheAdapter
implementation that allows to connect and use multiple adapters at the same time.
- Allows the creation of fallback cache logics, allowing to reach different cache types if one of them is failing for any reason
- Allows, optionally, to track partial failures as warnings, so you can log them using your preferred method
Since MultiCacheAdapter
and MultiCacheSessionAdapter
implement the respective interfaces (CacheAdapter
and CacheSessionAdapter
) you have all the methods at your disposal.
Please refer to the following example for the correct usage:
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"
multicacheadapters "github.com/tryvium-travels/golang-cache-adapters/multicache"
)
func main() {
// Ideally you would want to use 2 DIFFERENT cache types
// for the sake of the simplicity, we will just create 2
// adapters of the same type.
redisURI1 := "rediss://my-redis-instance-uri1:port"
redisURI2 := "rediss://my-redis-instance-uri2:port"
myRedisPool1 := &redis.Pool{
Dial: func() (redis.Conn, error) {
// obtain a redis connection, there
// are plenty of ways to do that.
return redis.DialURL(redisURI1)
},
}
myRedisPool2 := &redis.Pool{
Dial: func() (redis.Conn, error) {
// obtain a redis connection, there
// are plenty of ways to do that.
return redis.DialURL(redisURI2)
},
}
exampleTTL := time.Hour
// first we create the adapters.
redisAdapter1, err := rediscacheadapters.New(myRedisPool1, exampleTTL)
if err != nil {
// remember to check for errors
log.Fatalf("Redis Adapter 1 initialization error: %s", err)
}
redisAdapter2, err := rediscacheadapters.New(myRedisPool1, exampleTTL)
if err != nil {
// remember to check for errors
log.Fatalf("Redis Adapter 2 initialization error: %s", err)
}
// then we create a MultiCacheAdapter to use them at the same time.
adapter, err := multicacheadapters.New(redisAdapter1, redisAdapter2)
if err != nil {
// remember to check for errors
log.Fatalf("Multi Cache Adapter 2 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)
}
}