aboutsummaryrefslogtreecommitdiffstats
path: root/examples/basic_usage/main.go
blob: 5b44cd92aae8287a49690606e1427b8bba9922ad (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
40
41
42
43
44
45
46
47
48
package main

import (
	"fmt"
	"time"

	"github.com/marcthe12/cache"
)

func main() {
	// Create an in-memory cache
	db, err := cache.OpenMem[string, string]()
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	defer func() {
		err := db.Close()
		if err != nil {
			fmt.Println("Error:", err)
		}
	}()

	// Set a value with a TTL of 5 seconds
	if err = db.Set("key1", "value1", 5*time.Second); err != nil {
		fmt.Println("Set Error:", err)
		return
	}

	// Get the value
	value, ttl, err := db.GetValue("key1")
	if err != nil {
		fmt.Println("Get Error:", err)
	} else {
		fmt.Printf("Got value: %s, TTL: %s\n", value, ttl)
	}

	// Wait for 6 seconds and try to get the value again
	time.Sleep(6 * time.Second)

	value, ttl, err = db.GetValue("key1")
	if err != nil {
		fmt.Println("Get Error after TTL:", err)
	} else {
		fmt.Printf("Got value after TTL: %s, TTL: %s\n", value, ttl)
	}
}