src.dualinventive.com/go/lib/kv/kv.go

47 lines
1.0 KiB
Go

package kv
import (
"time"
"src.dualinventive.com/go/lib/kv/ramkv"
"src.dualinventive.com/go/lib/kv/rediskv"
)
// ErrNotFound is returned when the value is not found
var ErrNotFound = rediskv.ErrNotFound
// KV represents a interface with functions to get and set persistent data
type KV interface {
Get(string) (string, error)
Set(string, interface{}) error
SetExp(string, interface{}, time.Duration) error
Del(string) (bool, error)
Exists(string) (bool, error)
Keys(string) ([]string, error)
HKeys(string) ([]string, error)
HGet(string, string) (string, error)
HGetAll(string) (map[string]string, error)
HSet(string, string, interface{}) error
HDel(string, string) error
Close() error
}
const (
// TypeRedis is a Redis KV-store
TypeRedis string = "redis"
// TypeRAM is a RAM-only KV-store
TypeRAM string = "ram"
)
// New initializes a new KV-store
func New(t string, params ...string) (KV, error) {
switch t {
case TypeRedis:
return rediskv.New(params...)
case TypeRAM:
return ramkv.New()
default:
panic("invalid KV-storage")
}
}