github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/examples/ttl/main.go (about) 1 package main 2 3 import ( 4 "log" 5 "time" 6 7 "github.com/rosedblabs/rosedb/v2" 8 ) 9 10 // this file shows how to use the Expiry/TTL feature of rosedb. 11 func main() { 12 // specify the options 13 options := rosedb.DefaultOptions 14 options.DirPath = "/tmp/rosedb_ttl" 15 16 // open a database 17 db, err := rosedb.Open(options) 18 if err != nil { 19 panic(err) 20 } 21 defer func() { 22 _ = db.Close() 23 }() 24 25 // when you put a key-value pair, you can specify the ttl. 26 err = db.PutWithTTL([]byte("name"), []byte("rosedb"), time.Second*5) 27 if err != nil { 28 panic(err) 29 } 30 // now you can get the ttl of the key. 31 ttl, err := db.TTL([]byte("name")) 32 if err != nil { 33 panic(err) 34 } 35 println(ttl.String()) 36 37 _ = db.Put([]byte("name2"), []byte("rosedb2")) 38 //and you can also set the ttl of the key after you put it. 39 err = db.Expire([]byte("name2"), time.Second*2) 40 if err != nil { 41 panic(err) 42 } 43 ttl, err = db.TTL([]byte("name2")) 44 if err != nil { 45 log.Println(err) 46 } 47 println(ttl.String()) 48 }