Package list golang-github-renekroon-ttlcache / 8af4104
New upstream version 2.4.0+ds Sascha Steinbiss 3 months ago
4 changed file(s) with 107 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
00 language: go
11
22 go:
3 - "1.16.x"
34 - "1.15.x"
4 - "1.14.x"
55
66 git:
77 depth: 1
368368 return length
369369 }
370370
371 // GetKeys returns all keys of items in the cache. Returns nil when the cache has been closed.
372 func (cache *Cache) GetKeys() []string {
373 cache.mutex.Lock()
374 defer cache.mutex.Unlock()
375
376 if cache.isShutDown {
377 return nil
378 }
379 keys := make([]string, len(cache.items))
380 i := 0
381 for k := range cache.items {
382 keys[i] = k
383 i++
384 }
385 return keys
386 }
387
371388 // SetTTL sets the global TTL value for items in the cache, which can be overridden at the item level.
372389 func (cache *Cache) SetTTL(ttl time.Duration) error {
373390 cache.mutex.Lock()
464481 return cache.metrics
465482 }
466483
484 // Touch resets the TTL of the key when it exists, returns ErrNotFound if the key is not present.
485 func (cache *Cache) Touch(key string) error {
486 cache.mutex.Lock()
487 defer cache.mutex.Unlock()
488 item, exists := cache.items[key]
489 if !exists {
490 return ErrNotFound
491 }
492 item.touch()
493 return nil
494 }
495
467496 func min(duration time.Duration, second time.Duration) time.Duration {
468497 if duration < second {
469498 return duration
5454 <-sync
5555 assert.Equal(t, Closed, reason)
5656
57 }
58
59 func TestCache_TestTouch(t *testing.T) {
60 t.Parallel()
61 cache := NewCache()
62 defer cache.Close()
63
64 lock := sync.Mutex{}
65
66 lock.Lock()
67 expired := false
68 lock.Unlock()
69
70 cache.SkipTTLExtensionOnHit(true)
71 cache.SetExpirationCallback(func(key string, value interface{}) {
72 lock.Lock()
73 defer lock.Unlock()
74 expired = true
75 })
76
77 cache.SetWithTTL("key", "data", time.Millisecond*900)
78 <-time.After(time.Millisecond * 500)
79
80 // no Touch
81 // cache.Touch("key")
82
83 <-time.After(time.Millisecond * 500)
84 lock.Lock()
85 assert.Equal(t, true, expired)
86 lock.Unlock()
87 cache.Remove("key")
88
89 lock.Lock()
90 expired = false
91 lock.Unlock()
92
93 cache.SetWithTTL("key", "data", time.Millisecond*900)
94 <-time.After(time.Millisecond * 500)
95 cache.Touch("key")
96
97 <-time.After(time.Millisecond * 500)
98 lock.Lock()
99 assert.Equal(t, false, expired)
100 lock.Unlock()
57101 }
58102
59103 // Issue #37: Cache metrics
610654 assert.Equal(t, "world", (data.(string)), "Expected data content to be 'world'")
611655 }
612656
657 func TestCacheGetKeys(t *testing.T) {
658 t.Parallel()
659
660 cache := NewCache()
661 defer cache.Close()
662
663 keys := cache.GetKeys()
664 assert.Empty(t, keys, "Expected keys to be empty")
665
666 cache.Set("hello", "world")
667 keys = cache.GetKeys()
668 assert.NotEmpty(t, keys, "Expected keys to be not empty")
669 assert.Equal(t, []string{"hello"}, keys, "Expected keys contains 'hello'")
670 }
671
613672 func TestCacheExpirationCallbackFunction(t *testing.T) {
614673 t.Parallel()
615674
0 package ttlcache
1
2 import (
3 "testing"
4
5 "github.com/stretchr/testify/assert"
6 )
7
8 func TestError(t *testing.T) {
9 assert.Equal(t, "key not found", ErrNotFound.Error())
10
11 }
12
13 func TestEvictionError(t *testing.T) {
14 assert.Equal(t, "Removed", Removed.String())
15 assert.Equal(t, "Expired", Expired.String())
16
17 }