Skip to content

Commit

Permalink
switch Value to interface{} to simplify external use
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Sep 15, 2020
1 parent f53907b commit 59436fc
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 121 deletions.
24 changes: 10 additions & 14 deletions cache.go
Expand Up @@ -12,9 +12,6 @@ import (
"fmt"
)

// Value type wraps interface{}
type Value interface{}

// Sizer allows to perform size-based restrictions, optional.
// If not defined both maxValueSize and maxCacheSize checks will be ignored
type Sizer interface {
Expand All @@ -23,14 +20,14 @@ type Sizer interface {

// LoadingCache defines guava-like cache with Get method returning cached value ao retrieving it if not in cache
type LoadingCache interface {
Get(key string, fn func() (Value, error)) (val Value, err error) // load or get from cache
Peek(key string) (Value, bool) // get from cache by key
Invalidate(fn func(key string) bool) // invalidate items for func(key) == true
Delete(key string) // delete by key
Purge() // clear cache
Stat() CacheStat // cache stats
Keys() []string // list of all keys
Close() error // close open connections
Get(key string, fn func() (interface{}, error)) (val interface{}, err error) // load or get from cache
Peek(key string) (interface{}, bool) // get from cache by key
Invalidate(fn func(key string) bool) // invalidate items for func(key) == true
Delete(key string) // delete by key
Purge() // clear cache
Stat() CacheStat // cache stats
Keys() []string // list of all keys
Close() error // close open connections
}

// CacheStat represent stats values
Expand All @@ -57,10 +54,10 @@ func NewNopCache() *Nop {
}

// Get calls fn without any caching
func (n *Nop) Get(key string, fn func() (Value, error)) (Value, error) { return fn() }
func (n *Nop) Get(key string, fn func() (interface{}, error)) (interface{}, error) { return fn() }

// Peek does nothing and always returns false
func (n *Nop) Peek(key string) (Value, bool) { return nil, false }
func (n *Nop) Peek(key string) (interface{}, bool) { return nil, false }

// Invalidate does nothing for nop cache
func (n *Nop) Invalidate(fn func(key string) bool) {}
Expand All @@ -83,4 +80,3 @@ func (n *Nop) Stat() CacheStat {
func (n *Nop) Close() error {
return nil
}

0 comments on commit 59436fc

Please sign in to comment.