# README
Multi Cache Adapter implementation
A CacheAdapter
implementation that allows to connect and use multiple adapters at the same time.
Features
- 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
Usage
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)
}
}
# Functions
New creates a new multi cache adapter from an index-based priority array of cache adapters (called sub-adapters) and a flag instructing to show warning (non-fatal) errors.
NewSession creates a new multi cache session adapter from an index-based priority array of cache adapters (called sub-adapters) and a flag instructing to show warning (non-fatal) errors.
# Variables
ErrInvalidSubAdapters will come out if you try to pass one or more nil sub-adapters when creating a new MultiCacheAdapter or a session.
ErrMultiCacheWarning will come out paired with other errors in case an non-fatal error occurs during a multicache operation.
# Structs
MultiCacheAdapter is a cache adapter which uses multiple sub-adapters, following a priority given by the index of the adapter in the inner array of adapters.
MultiCacheSessionAdapter is a cache adapter which uses multiple sub-adapters, following a priority given by the index of the adapter in the inner array of adapters.