Add cache wrapper package

pull/94/head
Miguel Mota 3 years ago
parent 6af3378473
commit b86ac682c9

@ -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

@ -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)

37
pkg/cache/cache.go vendored

@ -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)
}

@ -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)
}
Loading…
Cancel
Save