Refactor & fix storing struct-values in redis keyvalue
This commit is contained in:
@ -26,6 +26,7 @@ import (
|
||||
type Storage interface {
|
||||
Put(key string, value interface{}) (err error)
|
||||
Get(key string) (value interface{}, exists bool, err error)
|
||||
GetWithValue(key string, value interface{}) (exists bool, err error)
|
||||
Del(key string) (err error)
|
||||
IncrBy(key string, update int64) (err error)
|
||||
DecrBy(key string, update int64) (err error)
|
||||
@ -55,6 +56,10 @@ func Get(key string) (value interface{}, exists bool, err error) {
|
||||
return store.Get(key)
|
||||
}
|
||||
|
||||
func GetWithValue(key string, value interface{}) (exists bool, err error) {
|
||||
return store.GetWithValue(key, value)
|
||||
}
|
||||
|
||||
// Del removes a save value from a storage backend
|
||||
func Del(key string) (err error) {
|
||||
return store.Del(key)
|
||||
|
@ -17,6 +17,7 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
||||
@ -52,6 +53,21 @@ func (s *Storage) Get(key string) (value interface{}, exists bool, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Storage) GetWithValue(key string, value interface{}) (exists bool, err error) {
|
||||
v, exists, err := s.Get(key)
|
||||
if !exists {
|
||||
return exists, err
|
||||
}
|
||||
|
||||
val := reflect.ValueOf(value)
|
||||
if val.Kind() != reflect.Ptr {
|
||||
panic("some: check must be a pointer")
|
||||
}
|
||||
|
||||
val.Elem().Set(reflect.ValueOf(v))
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// Del removes a saved value from a memory storage
|
||||
func (s *Storage) Del(key string) (err error) {
|
||||
s.mutex.Lock()
|
||||
|
@ -17,8 +17,10 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
|
||||
"code.vikunja.io/api/pkg/red"
|
||||
"github.com/go-redis/redis/v8"
|
||||
@ -40,9 +42,28 @@ func NewStorage() *Storage {
|
||||
|
||||
// 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
|
||||
|
||||
var v interface{}
|
||||
|
||||
switch value.(type) {
|
||||
case int:
|
||||
v = value
|
||||
case int8:
|
||||
v = value
|
||||
case int16:
|
||||
v = value
|
||||
case int32:
|
||||
v = value
|
||||
case int64:
|
||||
v = value
|
||||
default:
|
||||
var buf bytes.Buffer
|
||||
enc := gob.NewEncoder(&buf)
|
||||
err = enc.Encode(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.client.Set(context.Background(), key, buf.Bytes(), 0).Err()
|
||||
}
|
||||
|
||||
return s.client.Set(context.Background(), key, v, 0).Err()
|
||||
@ -50,13 +71,32 @@ func (s *Storage) Put(key string, value interface{}) (err error) {
|
||||
|
||||
// Get retrieves a saved value from redis
|
||||
func (s *Storage) Get(key string) (value interface{}, exists bool, err error) {
|
||||
value, err = s.client.Get(context.Background(), key).Result()
|
||||
if err != nil && errors.Is(err, redis.Nil) {
|
||||
return nil, false, nil
|
||||
}
|
||||
return value, true, err
|
||||
}
|
||||
|
||||
func (s *Storage) GetWithValue(key string, value interface{}) (exists bool, err error) {
|
||||
b, err := s.client.Get(context.Background(), key).Bytes()
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, value)
|
||||
return
|
||||
var buf bytes.Buffer
|
||||
_, err = buf.Write(b)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
dec := gob.NewDecoder(&buf)
|
||||
err = dec.Decode(value)
|
||||
return true, err
|
||||
}
|
||||
|
||||
// Del removed a value from redis
|
||||
|
Reference in New Issue
Block a user