Add doc comments

Signed-off-by: kolaente <k@knt.li>
This commit is contained in:
kolaente 2020-10-04 22:17:55 +02:00
parent 3a305631db
commit f2d0a25c2f
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 10 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import (
"code.vikunja.io/api/pkg/modules/keyvalue/memory"
)
// 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)
@ -29,18 +30,22 @@ type Storage interface {
var store Storage
// InitStorage initializes the configured storage backend
func InitStorage() {
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)
}

View File

@ -19,17 +19,20 @@ package memory
import "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()
@ -37,12 +40,14 @@ func (s *Storage) Put(key string, value interface{}) (err error) {
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()
return s.store[key], nil
}
// Del removes a saved value from a memory storage
func (s *Storage) Del(key string) (err error) {
s.mutex.Lock()
defer s.mutex.Unlock()