-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
135 lines (108 loc) · 3.57 KB
/
main.go
File metadata and controls
135 lines (108 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"time"
"github.com/1mb-dev/obcache-go/v2/pkg/obcache"
)
func main() {
// Create a cache with default configuration
cache, err := obcache.New(obcache.NewDefaultConfig())
if err != nil {
panic(err)
}
// Example 1: Basic cache usage
fmt.Println("=== Basic Cache Usage ===")
if err := cache.Set("key1", "value1", 5*time.Minute); err != nil {
fmt.Printf("Error setting cache: %v\n", err)
return
}
if value, found := cache.Get("key1"); found {
fmt.Printf("Found: %v\n", value)
}
// Example 2: Function wrapping - simple function
fmt.Println("\n=== Function Wrapping ===")
// Original expensive function
expensiveComputation := func(n int) string {
fmt.Printf("Computing for %d (this is expensive)...\n", n)
time.Sleep(100 * time.Millisecond) // Simulate expensive operation
return fmt.Sprintf("result-%d", n*2)
}
// Wrap the function with caching
cachedComputation := obcache.Wrap(cache, expensiveComputation)
// First call - will execute the function
result1 := cachedComputation(5)
fmt.Printf("Result 1: %s\n", result1)
// Second call - will use cache
result2 := cachedComputation(5)
fmt.Printf("Result 2: %s\n", result2)
// Different input - will execute the function again
result3 := cachedComputation(10)
fmt.Printf("Result 3: %s\n", result3)
// Example 3: Function with error handling
fmt.Println("\n=== Function with Error ===")
riskyFunction := func(n int) (string, error) {
if n < 0 {
return "", fmt.Errorf("negative numbers not allowed")
}
fmt.Printf("Processing %d...\n", n)
time.Sleep(50 * time.Millisecond)
return fmt.Sprintf("processed-%d", n), nil
}
cachedRiskyFunction := obcache.Wrap(cache, riskyFunction)
// Success case - will be cached
if result, err := cachedRiskyFunction(42); err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Success: %s\n", result)
}
// Same input - from cache
if result, err := cachedRiskyFunction(42); err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Cached: %s\n", result)
}
// Error case - not cached
if result, err := cachedRiskyFunction(-1); err != nil {
fmt.Printf("Error (not cached): %v\n", err)
} else {
fmt.Printf("Result: %s\n", result)
}
// Example 4: Custom configuration
fmt.Println("\n=== Custom Configuration ===")
customConfig := obcache.NewDefaultConfig().
WithMaxEntries(100).
WithDefaultTTL(1 * time.Second)
customCache, err := obcache.New(customConfig)
if err != nil {
panic(err)
}
slowFunction := func(s string) string {
fmt.Printf("Slow processing of '%s'...\n", s)
time.Sleep(100 * time.Millisecond)
return "processed-" + s
}
cachedSlowFunction := obcache.Wrap(customCache, slowFunction,
obcache.WithTTL(500*time.Millisecond))
// First call
result := cachedSlowFunction("test")
fmt.Printf("Result: %s\n", result)
// Immediate second call - from cache
result = cachedSlowFunction("test")
fmt.Printf("Cached: %s\n", result)
// Wait for TTL expiration
fmt.Println("Waiting for TTL expiration...")
time.Sleep(600 * time.Millisecond)
// Third call - will recompute due to TTL expiration
result = cachedSlowFunction("test")
fmt.Printf("Recomputed: %s\n", result)
// Example 5: Cache statistics
fmt.Println("\n=== Cache Statistics ===")
stats := cache.Stats()
fmt.Printf("Cache Stats:\n")
fmt.Printf(" Hits: %d\n", stats.Hits())
fmt.Printf(" Misses: %d\n", stats.Misses())
fmt.Printf(" Hit Rate: %.2f%%\n", stats.HitRate())
fmt.Printf(" Keys: %d\n", stats.KeyCount())
fmt.Printf(" Evictions: %d\n", stats.Evictions())
fmt.Printf(" Invalidations: %d\n", stats.Invalidations())
}