Skip to content

Commit

Permalink
remove Delete() / Clear() method from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Oct 27, 2024
1 parent 9b72b00 commit 97a8837
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 63 deletions.
43 changes: 0 additions & 43 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,49 +181,6 @@ func (cache *Cache) set(key string, value CacheValue, willFlushAsync bool) {
}
}

func (cache *Cache) Delete(key string) {
cache.cacheMapMutex.Lock()
delete(cache.cacheMap, key)
cache.cacheMapMutex.Unlock()

go cache.flush()
}

func (cache *Cache) Clear() {
cache.cacheMapMutex.Lock()
cache.cacheMap = make(map[string]cacheValue)
cache.lastCompactTime = time.Now()
cache.cacheMapMutex.Unlock()

cache.clearPersistentFile()
}

func (cache *Cache) clearPersistentFile() {
if pf := cache.persistentFile; pf == nil {
return
}

var (
cacheFilePath = cache.persistentFile.cacheFilePath
handleError = cache.persistentFile.handleError
)

unlockFunc, err := lockCachePersistentFile(cacheFilePath, true, handleError)
if err != nil {
return
}
defer unlockFunc()

if err := os.Truncate(cacheFilePath, 0); err != nil {
if handleError != nil {
handleError(err)
}
return
}

cache.persistentFile.lastPersistentTime = time.Now()
}

func (cache *Cache) checkType(cacheValue CacheValue) {
if pf := cache.persistentFile; pf != nil {
if cacheValueType := reflect.TypeOf(cacheValue); !cacheValueType.AssignableTo(pf.valueType) {
Expand Down
20 changes: 0 additions & 20 deletions internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,6 @@ func TestCache(t *testing.T) {
} else if v := value.(integerCacheValue).Value; v != 4 {
t.Fatalf("unexpected cache value: %v", v)
}
cache.Delete("key_3")
if _, result := cache.Get("key_3", func() (CacheValue, error) {
return nil, errors.New("test error")
}); result != NoResultGot {
t.Fatalf("key_3 should be deleted")
}

if value, result := cache.Get("key_4", func() (CacheValue, error) {
return integerCacheValue{Value: 4, ExpiredAt: time.Now().Add(100 * time.Millisecond)}, nil
}); result != GetResultFromFallback {
t.Fatal("unexpected ok")
} else if v := value.(integerCacheValue).Value; v != 4 {
t.Fatalf("unexpected cache value: %v", v)
}
cache.Clear()
if _, result := cache.Get("key_4", func() (CacheValue, error) {
return nil, errors.New("test error")
}); result != NoResultGot {
t.Fatalf("key_4 should be deleted")
}
}

func TestCachePersist(t *testing.T) {
Expand Down

0 comments on commit 97a8837

Please sign in to comment.