initial import

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
2018-03-22 13:35:38 +03:00
commit 83f9fa19ad
73 changed files with 4302 additions and 0 deletions

1
cache/block/block.go vendored Normal file
View File

@@ -0,0 +1 @@
package block

98
cache/cache.go vendored Normal file
View File

@@ -0,0 +1,98 @@
package cache
import (
"fmt"
"strings"
)
type Cache interface {
Configure(interface{}) error
Set(interface{}, interface{}) error
Exists(interface{}) (bool, error)
Get(interface{}) (interface{}, error)
Keys() ([]interface{}, error)
//Size() int
Purge() error
Del(interface{}) error
OnEvict(func(interface{}, interface{})) error
}
var cacheTypes map[string]Cache
func init() {
cacheTypes = make(map[string]Cache)
RegisterCache("", &Dummy{})
}
func RegisterCache(engine string, cache Cache) {
cacheTypes[engine] = cache
}
func New(ctype string, cfg interface{}) (Cache, error) {
var err error
cache, ok := cacheTypes[ctype]
if !ok {
return nil, fmt.Errorf("unknown cluster type %s. only %s supported", ctype, strings.Join(CacheTypes(), ","))
}
if cfg == nil {
return cache, nil
}
err = cache.Configure(cfg)
if err != nil {
return nil, err
}
return cache, nil
}
func CacheTypes() []string {
var ctypes []string
for ctype, _ := range cacheTypes {
ctypes = append(ctypes, ctype)
}
return ctypes
}
type Dummy struct{}
func (*Dummy) Configure(interface{}) error {
return nil
}
func (*Dummy) Del(interface{}) error {
return nil
}
func (*Dummy) Set(interface{}, interface{}) error {
return nil
}
func (*Dummy) Get(interface{}) (interface{}, error) {
return nil, nil
}
func (*Dummy) Exists(interface{}) (bool, error) {
return false, nil
}
func (*Dummy) Keys() ([]interface{}, error) {
return nil, nil
}
func (*Dummy) OnEvict(func(interface{}, interface{})) error {
return nil
}
func (*Dummy) Peek(interface{}) (interface{}, error) {
return nil, nil
}
func (*Dummy) Purge() error {
return nil
}
func (*Dummy) Size() int {
return 0
}

1
cache/filesystem/filesystem.go vendored Normal file
View File

@@ -0,0 +1 @@
package filesystem

81
cache/memory/memory.go vendored Normal file
View File

@@ -0,0 +1,81 @@
package memory
import (
"errors"
hcache "github.com/hashicorp/golang-lru"
"github.com/sdstack/storage/cache"
)
type config struct {
Size int
}
type CacheLRU struct {
cfg *config
c *hcache.Cache
}
func init() {
cache.RegisterCache("memory-lru", &CacheLRU{})
}
func (c *CacheLRU) Configure(data interface{}) error {
// var err error
/*
err = mapstructure.Decode(data, &c.cfg)
if err != nil {
return err
}
*/
c.cfg = &config{Size: data.(int)}
return nil
}
func (c *CacheLRU) OnEvict(onEvict func(interface{}, interface{})) error {
var err error
c.c, err = hcache.NewWithEvict(c.cfg.Size, onEvict)
return err
}
func (c *CacheLRU) Peek(k interface{}) (interface{}, bool) {
return c.c.Peek(k)
}
func (c *CacheLRU) Purge() error {
c.c.Purge()
return nil
}
func (c *CacheLRU) Size() int {
return c.c.Len()
}
func (c *CacheLRU) Set(k interface{}, v interface{}) error {
if c.c.Add(k, v) {
return nil
}
return errors.New("failed")
}
func (c *CacheLRU) Del(k interface{}) error {
c.c.Remove(k)
return nil
}
func (c *CacheLRU) Get(k interface{}) (interface{}, error) {
v, ok := c.c.Get(k)
if ok {
return v, nil
}
return v, errors.New("failed")
}
func (c *CacheLRU) Keys() ([]interface{}, error) {
v := c.c.Keys()
return v, nil
}
func (c *CacheLRU) Exists(k interface{}) (bool, error) {
return c.c.Contains(k), nil
}