summaryrefslogtreecommitdiffstats
path: root/examples/eviction_policy
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-24 10:37:50 +0530
committerMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-27 13:37:32 +0530
commita35f42bc2e4c29a1583fbc8900c14ec0ce5533d2 (patch)
tree5af6a2854644ac3dc44acdc9d604fa0eb576f120 /examples/eviction_policy
parentAdded LTR Eviction (diff)
downloadcache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar
cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.gz
cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.bz2
cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.lz
cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.xz
cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.zst
cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.zip
Added examples and documentation
Diffstat (limited to 'examples/eviction_policy')
-rw-r--r--examples/eviction_policy/main.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/eviction_policy/main.go b/examples/eviction_policy/main.go
new file mode 100644
index 0000000..a9c8577
--- /dev/null
+++ b/examples/eviction_policy/main.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "fmt"
+
+ "code.qburst.com/marcpervaz//cache"
+)
+
+func main() {
+ // Create an in-memory cache with LRU eviction policy
+ db, err := cache.OpenMem[string, string]("example", cache.WithPolicy(cache.PolicyLRU))
+ if err != nil {
+ fmt.Println("Error:", err)
+ return
+ }
+ defer db.Close()
+
+ // Set values
+ db.Set("key1", "value1", 0)
+ db.Set("key2", "value2", 0)
+ db.Set("key3", "value3", 0)
+
+ // Access some keys
+ db.GetValue("key1")
+ db.GetValue("key2")
+
+ // Add another key to trigger eviction
+ db.Set("key4", "value4", 0)
+
+ // Check which keys are present
+ for _, key := range []string{"key1", "key2", "key3", "key4"} {
+ value, _, err := db.GetValue(key)
+ if err != nil {
+ fmt.Printf("Key %s not found\n", key)
+ } else {
+ fmt.Printf("Key %s found with value: %s\n", key, value)
+ }
+ }
+}