Skip to content

Commit

Permalink
Store nil instead of empty slice to delete cache
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Nov 28, 2017
1 parent e26a51e commit 1620508
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ func NewCache(dir string) *Cache {
return &Cache{dir}
}

// Store saves data under the given name. If len(data) is 0, the file is
// deleted.
// Store saves data under the given name. If data is nil, the file is deleted.
func (c *Cache) Store(name string, data []byte) error {
p := c.path(name)
if len(data) == 0 {
if data == nil {
if util.PathExists(p) {
return os.Remove(p)
}
Expand Down Expand Up @@ -130,9 +129,10 @@ func (c *Cache) LoadOrStore(name string, maxAge time.Duration, reload func() ([]

// LoadOrStoreJSON loads JSON-serialised data from cache if they exist and are
// newer than maxAge. If the data do not exist or are older than maxAge, reload
// is called, and the returned interface{} is cached and returned.
// is called, and the returned data are marshalled to JSON and cached, and
// unmarshalled into v.
//
// If maxAge is 0, any cached data are always returned.
// If maxAge is 0, any cached data are loaded regardless of age.
func (c *Cache) LoadOrStoreJSON(name string, maxAge time.Duration, reload func() (interface{}, error), v interface{}) error {
var (
load bool
Expand Down
6 changes: 3 additions & 3 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestStoreAndLoad(t *testing.T) {
}

// Delete non-existant store
if err := c.Store(n, []byte{}); err != nil {
if err := c.Store(n, nil); err != nil {
t.Errorf("unexpected error clearing cache: %v", err)
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func TestStoreAndLoad(t *testing.T) {
}

// Delete data
if err := c.Store(n, []byte{}); err != nil {
if err := c.Store(n, nil); err != nil {
t.Errorf("couldn't delete cache %s: %v", p, err)
}

Expand Down Expand Up @@ -383,7 +383,7 @@ func TestSession(t *testing.T) {
}

// Delete non-existant store
if err := s.Store(n, []byte{}); err != nil {
if err := s.Store(n, nil); err != nil {
t.Errorf("unexpected error clearing cache: %v", err)
}

Expand Down

0 comments on commit 1620508

Please sign in to comment.