forked from juev/gclone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_example.go
More file actions
71 lines (61 loc) · 2.12 KB
/
cache_example.go
File metadata and controls
71 lines (61 loc) · 2.12 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
package main
import (
"fmt"
"time"
)
// Example of how to use the improved directory cache with performance monitoring
func ExampleCacheUsage() {
// Create custom cache configuration
cacheConfig := &CacheConfig{
TTL: 2 * time.Minute, // Longer TTL for better performance
CleanupInterval: 1 * time.Minute, // More frequent cleanup
MaxEntries: 500, // Smaller cache for this example
EnablePeriodicCleanup: true,
}
// Create cache with custom configuration
fs := &DefaultFileSystem{}
cache := NewDirCache(cacheConfig, fs)
defer cache.Close() // Important: close to stop cleanup goroutine
// Test some directory checks
testDirs := []string{
"/tmp",
"/etc",
"/var",
"/usr/bin",
"/nonexistent/dir",
}
fmt.Println("Testing directory cache performance...")
// First round - cache misses
start := time.Now()
for _, dir := range testDirs {
exists := cache.IsDirectoryNotEmpty(dir)
fmt.Printf("Directory %s exists and not empty: %v\n", dir, exists)
}
firstRoundTime := time.Since(start)
// Second round - cache hits (should be much faster)
start = time.Now()
for _, dir := range testDirs {
exists := cache.IsDirectoryNotEmpty(dir)
fmt.Printf("Directory %s exists and not empty: %v (cached)\n", dir, exists)
}
secondRoundTime := time.Since(start)
// Get and display cache statistics
stats := cache.GetStats()
fmt.Printf("\n=== Cache Performance Statistics ===\n")
fmt.Printf("Cache Hits: %d\n", stats.Hits)
fmt.Printf("Cache Misses: %d\n", stats.Misses)
fmt.Printf("Hit Ratio: %.2f%%\n", stats.HitRatio()*100)
fmt.Printf("Total Entries: %d\n", stats.TotalSize)
fmt.Printf("Evictions: %d\n", stats.Evictions)
fmt.Printf("\n=== Performance Improvement ===\n")
fmt.Printf("First round (cache misses): %v\n", firstRoundTime)
fmt.Printf("Second round (cache hits): %v\n", secondRoundTime)
if secondRoundTime.Nanoseconds() > 0 {
speedup := float64(firstRoundTime.Nanoseconds()) / float64(secondRoundTime.Nanoseconds())
fmt.Printf("Speedup: %.1fx faster with cache\n", speedup)
}
}
// Uncomment to run the example:
// func main() {
// ExampleCacheUsage()
// }