Update upstream source from tag 'upstream/2.4.0+ds'
Update to upstream version '2.4.0+ds'
with Debian dir 4f63a350acc9fdd668490dbf67863203d6f8ba39
Sascha Steinbiss
2 years ago
368 | 368 | return length |
369 | 369 | } |
370 | 370 | |
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 | ||
371 | 388 | // SetTTL sets the global TTL value for items in the cache, which can be overridden at the item level. |
372 | 389 | func (cache *Cache) SetTTL(ttl time.Duration) error { |
373 | 390 | cache.mutex.Lock() |
464 | 481 | return cache.metrics |
465 | 482 | } |
466 | 483 | |
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 | ||
467 | 496 | func min(duration time.Duration, second time.Duration) time.Duration { |
468 | 497 | if duration < second { |
469 | 498 | return duration |
54 | 54 | <-sync |
55 | 55 | assert.Equal(t, Closed, reason) |
56 | 56 | |
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() | |
57 | 101 | } |
58 | 102 | |
59 | 103 | // Issue #37: Cache metrics |
610 | 654 | assert.Equal(t, "world", (data.(string)), "Expected data content to be 'world'") |
611 | 655 | } |
612 | 656 | |
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 | ||
613 | 672 | func TestCacheExpirationCallbackFunction(t *testing.T) { |
614 | 673 | t.Parallel() |
615 | 674 |
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 | } |