aboutsummaryrefslogtreecommitdiffstats
path: root/examples/eviction_policy/main.go
blob: a9c857707b821cdf67c084680d7c74efaeacd6df (plain) (blame)
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
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)
		}
	}
}