aboutsummaryrefslogtreecommitdiffstats
path: root/examples/basic_usage/main.go
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/basic_usage/main.go
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 '')
-rw-r--r--examples/basic_usage/main.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/basic_usage/main.go b/examples/basic_usage/main.go
new file mode 100644
index 0000000..0582770
--- /dev/null
+++ b/examples/basic_usage/main.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "fmt"
+ "time"
+
+ "github.com/marcthe12/cache"
+)
+
+func main() {
+ // Create an in-memory cache
+ db, err := cache.OpenMem[string, string]("example")
+ if err != nil {
+ fmt.Println("Error:", err)
+ return
+ }
+ defer db.Close()
+
+ // Set a value with a TTL of 5 seconds
+ err = db.Set("key1", "value1", 5*time.Second)
+ if 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)
+ }
+}