MCPcopy
hub / github.com/usememos/memos / TestCacheBasicOperations

Function TestCacheBasicOperations

store/cache/cache_test.go:11–59  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

9)
10
11func TestCacheBasicOperations(t *testing.T) {
12 ctx := context.Background()
13 config := DefaultConfig()
14 config.DefaultTTL = 100 * time.Millisecond
15 config.CleanupInterval = 50 * time.Millisecond
16 cache := New(config)
17 defer cache.Close()
18
19 // Test Set and Get
20 cache.Set(ctx, "key1", "value1")
21 if val, ok := cache.Get(ctx, "key1"); !ok || val != "value1" {
22 t.Errorf("Expected 'value1', got %v, exists: %v", val, ok)
23 }
24
25 // Test SetWithTTL
26 cache.SetWithTTL(ctx, "key2", "value2", 200*time.Millisecond)
27 if val, ok := cache.Get(ctx, "key2"); !ok || val != "value2" {
28 t.Errorf("Expected 'value2', got %v, exists: %v", val, ok)
29 }
30
31 // Test Delete
32 cache.Delete(ctx, "key1")
33 if _, ok := cache.Get(ctx, "key1"); ok {
34 t.Error("Key 'key1' should have been deleted")
35 }
36
37 // Test automatic expiration
38 time.Sleep(150 * time.Millisecond)
39 if _, ok := cache.Get(ctx, "key1"); ok {
40 t.Error("Key 'key1' should have expired")
41 }
42 // key2 should still be valid (200ms TTL)
43 if _, ok := cache.Get(ctx, "key2"); !ok {
44 t.Error("Key 'key2' should still be valid")
45 }
46
47 // Wait for key2 to expire
48 time.Sleep(100 * time.Millisecond)
49 if _, ok := cache.Get(ctx, "key2"); ok {
50 t.Error("Key 'key2' should have expired")
51 }
52
53 // Test Clear
54 cache.Set(ctx, "key3", "value3")
55 cache.Clear(ctx)
56 if _, ok := cache.Get(ctx, "key3"); ok {
57 t.Error("Cache should be empty after Clear()")
58 }
59}
60
61func TestCacheEviction(t *testing.T) {
62 ctx := context.Background()

Callers

nothing calls this directly

Calls 9

DefaultConfigFunction · 0.85
NewFunction · 0.70
CloseMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65
SetWithTTLMethod · 0.65
DeleteMethod · 0.65
ErrorMethod · 0.65
ClearMethod · 0.65

Tested by

no test coverage detected