Key-Value Storages (#674)
Fix lint Add docs for keyvalue config options Use keyvalue store to cache unsplash photo results Cleanup Use keyvalue store for upload avatar Use keyvalue store for initials avatar Fix initializing metrics Use keyvalue for metrics Add IncryBy and DecrBy methods to increase or decrease a value Fix lint Return custom error if a key does not exist Init keyvalue storage Follow the keyvalue storage setting for things like cache and other Add docs Add configuration of the storage backend Add redis keyvalue storage implementation Add doc comments Add methods to use storage through the package itself Add memory implementation for keyvalue store Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/674 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
This commit is contained in:
52
pkg/modules/keyvalue/error/error.go
Normal file
52
pkg/modules/keyvalue/error/error.go
Normal file
@ -0,0 +1,52 @@
|
||||
// Vikunja is a to-do list application to facilitate your life.
|
||||
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package error
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ErrValueNotFoundForKey represents an error where a key could not be found
|
||||
type ErrValueNotFoundForKey struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
// Error is the error implementation
|
||||
func (e *ErrValueNotFoundForKey) Error() string {
|
||||
return fmt.Sprintf("could not find value for key %s", e.Key)
|
||||
}
|
||||
|
||||
// IsErrValueNotFoundForKey checks if an error is ErrValueNotFoundForKey
|
||||
func IsErrValueNotFoundForKey(err error) bool {
|
||||
_, is := err.(*ErrValueNotFoundForKey)
|
||||
return is
|
||||
}
|
||||
|
||||
// ErrValueHasWrongType represents an error where a value saved at key has the wrong value
|
||||
type ErrValueHasWrongType struct {
|
||||
Key string
|
||||
ExpectedValue string
|
||||
}
|
||||
|
||||
// Error is the error implementation
|
||||
func (e *ErrValueHasWrongType) Error() string {
|
||||
return fmt.Sprintf("value at key %s has the wrong value, expexted was %s", e.Key, e.ExpectedValue)
|
||||
}
|
||||
|
||||
// IsErrValueHasWrongType checks if an error is ErrValueHasWrongType
|
||||
func IsErrValueHasWrongType(err error) bool {
|
||||
_, is := err.(*ErrValueHasWrongType)
|
||||
return is
|
||||
}
|
72
pkg/modules/keyvalue/keyvalue.go
Normal file
72
pkg/modules/keyvalue/keyvalue.go
Normal file
@ -0,0 +1,72 @@
|
||||
// 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 keyvalue
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/modules/keyvalue/memory"
|
||||
"code.vikunja.io/api/pkg/modules/keyvalue/redis"
|
||||
)
|
||||
|
||||
// Storage defines an interface for saving key-value pairs
|
||||
type Storage interface {
|
||||
Put(key string, value interface{}) (err error)
|
||||
Get(key string) (value interface{}, err error)
|
||||
Del(key string) (err error)
|
||||
IncrBy(key string, update int64) (err error)
|
||||
DecrBy(key string, update int64) (err error)
|
||||
}
|
||||
|
||||
var store Storage
|
||||
|
||||
// InitStorage initializes the configured storage backend
|
||||
func InitStorage() {
|
||||
switch config.KeyvalueType.GetString() {
|
||||
case "redis":
|
||||
store = redis.NewStorage()
|
||||
case "memory":
|
||||
fallthrough
|
||||
default:
|
||||
store = memory.NewStorage()
|
||||
}
|
||||
}
|
||||
|
||||
// Put puts a value in the storage backend
|
||||
func Put(key string, value interface{}) error {
|
||||
return store.Put(key, value)
|
||||
}
|
||||
|
||||
// Get returns a value from a storage backend
|
||||
func Get(key string) (value interface{}, err error) {
|
||||
return store.Get(key)
|
||||
}
|
||||
|
||||
// Del removes a save value from a storage backend
|
||||
func Del(key string) (err error) {
|
||||
return store.Del(key)
|
||||
}
|
||||
|
||||
// IncrBy increases a value at key by the amount in update
|
||||
func IncrBy(key string, update int64) (err error) {
|
||||
return store.IncrBy(key, update)
|
||||
}
|
||||
|
||||
// DecrBy increases a value at key by the amount in update
|
||||
func DecrBy(key string, update int64) (err error) {
|
||||
return store.DecrBy(key, update)
|
||||
}
|
102
pkg/modules/keyvalue/memory/memory.go
Normal file
102
pkg/modules/keyvalue/memory/memory.go
Normal file
@ -0,0 +1,102 @@
|
||||
// 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 memory
|
||||
|
||||
import (
|
||||
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Storage is the memory implementation of a storage backend
|
||||
type Storage struct {
|
||||
store map[string]interface{}
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
// NewStorage creates a new memory storage
|
||||
func NewStorage() *Storage {
|
||||
s := &Storage{}
|
||||
s.store = make(map[string]interface{})
|
||||
return s
|
||||
}
|
||||
|
||||
// Put puts a value into the memory storage
|
||||
func (s *Storage) Put(key string, value interface{}) (err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
s.store[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get retrieves a saved value from memory storage
|
||||
func (s *Storage) Get(key string) (value interface{}, err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
var exists bool
|
||||
value, exists = s.store[key]
|
||||
if !exists {
|
||||
return nil, &e.ErrValueNotFoundForKey{Key: key}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Del removes a saved value from a memory storage
|
||||
func (s *Storage) Del(key string) (err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
delete(s.store, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrBy increases the value saved at key by the amount provided through update
|
||||
// It assumes the value saved for the key either does not exist or has a type of int64
|
||||
func (s *Storage) IncrBy(key string, update int64) (err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
v, err := s.Get(key)
|
||||
if err != nil && !e.IsErrValueNotFoundForKey(err) {
|
||||
return err
|
||||
}
|
||||
val, is := v.(int64)
|
||||
if !is {
|
||||
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
||||
}
|
||||
s.store[key] = val + update
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecrBy decreases the value saved at key by the amount provided through update
|
||||
// It assumes the value saved for the key either does not exist or has a type of int64
|
||||
func (s *Storage) DecrBy(key string, update int64) (err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
v, err := s.Get(key)
|
||||
if err != nil && !e.IsErrValueNotFoundForKey(err) {
|
||||
return err
|
||||
}
|
||||
val, is := v.(int64)
|
||||
if !is {
|
||||
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
||||
}
|
||||
s.store[key] = val - update
|
||||
return nil
|
||||
}
|
78
pkg/modules/keyvalue/redis/redis.go
Normal file
78
pkg/modules/keyvalue/redis/redis.go
Normal file
@ -0,0 +1,78 @@
|
||||
// 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 (
|
||||
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
||||
"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 {
|
||||
if err == redis.Nil {
|
||||
return nil, &e.ErrValueNotFoundForKey{Key: key}
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
// IncrBy increases the value saved at key by the amount provided through update
|
||||
func (s *Storage) IncrBy(key string, update int64) (err error) {
|
||||
return s.client.IncrBy(key, update).Err()
|
||||
}
|
||||
|
||||
// DecrBy decreases the value saved at key by the amount provided through update
|
||||
func (s *Storage) DecrBy(key string, update int64) (err error) {
|
||||
return s.client.DecrBy(key, update).Err()
|
||||
}
|
Reference in New Issue
Block a user