From b86ac682c9c85922707b3d4e835092437073ff07 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Sun, 20 Dec 2020 20:38:49 -0800 Subject: [PATCH] Add cache wrapper package --- cointop/cointop.go | 2 +- cointop/config.go | 2 +- pkg/cache/cache.go | 37 +++++++++++++++++++++++++++++++++++++ pkg/toml/toml.go | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 pkg/cache/cache.go create mode 100644 pkg/toml/toml.go diff --git a/cointop/cointop.go b/cointop/cointop.go index c96d7ec..f4421c4 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -11,12 +11,12 @@ import ( "github.com/miguelmota/cointop/pkg/api" "github.com/miguelmota/cointop/pkg/api/types" + "github.com/miguelmota/cointop/pkg/cache" "github.com/miguelmota/cointop/pkg/filecache" "github.com/miguelmota/cointop/pkg/pathutil" "github.com/miguelmota/cointop/pkg/table" "github.com/miguelmota/cointop/pkg/ui" "github.com/miguelmota/gocui" - "github.com/patrickmn/go-cache" ) // TODO: clean up and optimize codebase diff --git a/cointop/config.go b/cointop/config.go index 4933852..6e43676 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -9,8 +9,8 @@ import ( "strings" "time" - "github.com/BurntSushi/toml" "github.com/miguelmota/cointop/pkg/pathutil" + "github.com/miguelmota/cointop/pkg/toml" ) var fileperm = os.FileMode(0644) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go new file mode 100644 index 0000000..aff2443 --- /dev/null +++ b/pkg/cache/cache.go @@ -0,0 +1,37 @@ +package cache + +import ( + "time" + + gocache "github.com/patrickmn/go-cache" +) + +// Cache is cache struct +type Cache struct { + cache *gocache.Cache +} + +// NoExpiration is constant +const NoExpiration = gocache.NoExpiration + +// New returns new cache instance +func New(defaultExpiration, cleanupInterval time.Duration) *Cache { + return &Cache{ + cache: gocache.New(defaultExpiration, cleanupInterval), + } +} + +// Set sets cache item +func (c *Cache) Set(k string, x interface{}, d time.Duration) { + c.cache.Set(k, x, d) +} + +// Get gets cache item +func (c *Cache) Get(k string) (interface{}, bool) { + return c.cache.Get(k) +} + +// Delete deletes cache item +func (c *Cache) Delete(k string) { + c.cache.Delete(k) +} diff --git a/pkg/toml/toml.go b/pkg/toml/toml.go new file mode 100644 index 0000000..5075b72 --- /dev/null +++ b/pkg/toml/toml.go @@ -0,0 +1,37 @@ +package toml + +import ( + "io" + + "github.com/BurntSushi/toml" +) + +// MetaData is meta data struct +type MetaData = toml.MetaData + +// Encoder is encoder struct +type Encoder struct { + encoder *toml.Encoder +} + +// NewEncoder returns a new encoder instance +func NewEncoder(w io.Writer) *Encoder { + return &Encoder{ + encoder: toml.NewEncoder(w), + } +} + +// Encode encodes interface to toml +func (enc *Encoder) Encode(v interface{}) error { + return enc.encoder.Encode(v) +} + +// Decode decodes toml data to interface +func Decode(data string, v interface{}) (MetaData, error) { + return toml.Decode(data, v) +} + +// DecodeFile decodes toml file +func DecodeFile(fpath string, v interface{}) (MetaData, error) { + return toml.DecodeFile(fpath, v) +}