2020-12-29 02:04:20 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-10-10 18:53:59 +02:00
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-10-10 18:53:59 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
2020-10-10 18:53:59 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-10-10 18:53:59 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-12-29 02:04:20 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-10 18:53:59 +02:00
|
|
|
|
|
|
|
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)
|
2021-01-31 12:32:46 +01:00
|
|
|
Get(key string) (value interface{}, exists bool, err error)
|
2021-05-28 10:52:32 +02:00
|
|
|
GetWithValue(key string, value interface{}) (exists bool, err error)
|
2020-10-10 18:53:59 +02:00
|
|
|
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
|
2021-01-31 12:32:46 +01:00
|
|
|
func Get(key string) (value interface{}, exists bool, err error) {
|
2020-10-10 18:53:59 +02:00
|
|
|
return store.Get(key)
|
|
|
|
}
|
|
|
|
|
2021-05-28 10:52:32 +02:00
|
|
|
func GetWithValue(key string, value interface{}) (exists bool, err error) {
|
|
|
|
return store.GetWithValue(key, value)
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:53:59 +02:00
|
|
|
// 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)
|
|
|
|
}
|