diff options
author | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-05-15 17:56:07 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-05-15 17:56:07 +0530 |
commit | 7d532553ae18bfff1bec9e0df3fa66313073e3dd (patch) | |
tree | 796a7fe86da764f51648b1337011baeaaf53421b /examples/eviction_policy_persistant | |
parent | Lint and bug fixes (diff) | |
download | cache-7d532553ae18bfff1bec9e0df3fa66313073e3dd.tar cache-7d532553ae18bfff1bec9e0df3fa66313073e3dd.tar.gz cache-7d532553ae18bfff1bec9e0df3fa66313073e3dd.tar.bz2 cache-7d532553ae18bfff1bec9e0df3fa66313073e3dd.tar.lz cache-7d532553ae18bfff1bec9e0df3fa66313073e3dd.tar.xz cache-7d532553ae18bfff1bec9e0df3fa66313073e3dd.tar.zst cache-7d532553ae18bfff1bec9e0df3fa66313073e3dd.zip |
Add raw API
Diffstat (limited to 'examples/eviction_policy_persistant')
-rw-r--r-- | examples/eviction_policy_persistant/main.go | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/examples/eviction_policy_persistant/main.go b/examples/eviction_policy_persistant/main.go new file mode 100644 index 0000000..fc7cc32 --- /dev/null +++ b/examples/eviction_policy_persistant/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "fmt" + "os" + "time" + + "github.com/marcthe12/cache" +) + +func main() { + // Create an in-memory cache with LRU eviction policy + db, err := cache.Open[int, int]( + "cache.db", + cache.WithPolicy(cache.PolicyLRU), + cache.WithMaxCost(20), + cache.SetCleanupTime(1*time.Second), + ) + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + defer func() { + err := db.Close() + if err != nil { + fmt.Println("Error:", err) + } + }() + + fmt.Println("Loaded Cache") + // Check which keys are present + for n := range 4 { + key := n + 1 + value, _, err := db.GetValue(key) + + if err != nil { + fmt.Printf("Key %d not found\n", key) + } else { + fmt.Printf("Key %d found with value: %d\n", key, value) + } + } + + fmt.Println("Set Values") + // Set values + if err := db.Set(1, -1, 10*time.Second); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + if err := db.Set(2, -2, 10*time.Second); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + if err := db.Set(3, -3, 10*time.Second); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + // Access some keys + if _, _, err := db.GetValue(1); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + if _, _, err := db.GetValue(2); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + fmt.Println("Add Key 4") + // Add another key to trigger eviction + if err := db.Set(4, -4, 0); err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + fmt.Println("Sleep") + time.Sleep(2 * time.Second) + + fmt.Println("Resume") + // Check which keys are present + for n := range 4 { + key := n + 1 + value, _, err := db.GetValue(key) + + if err != nil { + fmt.Printf("Key %d not found\n", key) + } else { + fmt.Printf("Key %d found with value: %d\n", key, value) + } + } +} |