Add redis keyvalue storage implementation
Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
parent
f2d0a25c2f
commit
e9e3445397
|
@ -90,7 +90,7 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
|
|||
cacher := caches.NewLRUCacher(caches.NewMemoryStore(), config.CacheMaxElementSize.GetInt())
|
||||
engine.SetDefaultCacher(cacher)
|
||||
case "redis":
|
||||
cacher := xrc.NewRedisCacher(config.RedisEnabled.GetString(), config.RedisPassword.GetString(), xrc.DEFAULT_EXPIRATION, engine.Logger())
|
||||
cacher := xrc.NewRedisCacher(config.RedisHost.GetString(), config.RedisPassword.GetString(), xrc.DEFAULT_EXPIRATION, engine.Logger())
|
||||
engine.SetDefaultCacher(cacher)
|
||||
default:
|
||||
log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2020 Vikunja and contriubtors. All rights reserved.
|
||||
//
|
||||
// This file is part of Vikunja.
|
||||
//
|
||||
// Vikunja is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Vikunja is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package redis
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/red"
|
||||
"encoding/json"
|
||||
"github.com/go-redis/redis/v7"
|
||||
)
|
||||
|
||||
// Storage is a redis implementation of a keyvalue storage
|
||||
type Storage struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
// NewStorage creates a new redis key value storage
|
||||
func NewStorage() *Storage {
|
||||
red.InitRedis()
|
||||
|
||||
return &Storage{
|
||||
client: red.GetRedis(),
|
||||
}
|
||||
}
|
||||
|
||||
// Put puts a value into redis
|
||||
func (s *Storage) Put(key string, value interface{}) (err error) {
|
||||
v, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.client.Set(key, v, 0).Err()
|
||||
}
|
||||
|
||||
// Get retrieves a saved value from redis
|
||||
func (s *Storage) Get(key string) (value interface{}, err error) {
|
||||
b, err := s.client.Get(key).Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, value)
|
||||
return
|
||||
}
|
||||
|
||||
// Del removed a value from redis
|
||||
func (s *Storage) Del(key string) (err error) {
|
||||
return s.client.Del(key).Err()
|
||||
}
|
|
@ -26,6 +26,10 @@ var r *redis.Client
|
|||
|
||||
// InitRedis initializes a redis connection
|
||||
func InitRedis() {
|
||||
if r != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !config.RedisEnabled.GetBool() {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue