This package makes it easy to abstract your Redis connection and use Get and Set methods.
go get github.com/ercantopuz/rediscache
Import it in your code:
import "github.com/ercantopuz/rediscache"
package main
import "github.com/ercantopuz/rediscache"
func main() {
// It has all the properties of the redis.Options
redisCacheOptions := rediscache.Options{
Addr: "",
Username: "",
Password: "",
DB: 0,
}
redisCache := rediscache.NewRedisCache(
expire,
redisCacheOptions)
}
package service
import (
"context"
"github.com/ercantopuz/rediscache"
)
type service struct {
redisCache rediscache.Cache
}
func (s *service) GetData() {
var err error
var expectedData model.Data{}
expectedData, err = s.redisCache.Get(
context.Background(),
key,
expectedData)
/****/
err = s.redisCache.Set(
context.Background(),
key,
dataFromRepository)
}