forked from couchbaselabs/devguide-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
expiration.go
81 lines (67 loc) · 1.84 KB
/
expiration.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"time"
"github.com/couchbase/gocb"
)
// bucket reference - reuse as bucket reference in the application
var bucket *gocb.Bucket
func main() {
// Connect to Cluster
cluster, err := gocb.Connect("couchbase://127.0.0.1")
if err != nil {
fmt.Println("ERROR CONNECTING TO CLUSTER:", err)
}
// Open Bucket
bucket, err = cluster.OpenBucket("travel-sample", "")
if err != nil {
fmt.Println("ERROR OPENING BUCKET:", err)
}
// Create a document and assign an expiration of 2 seconds
key := "goDevguideExampleExpiration"
val := "Expiration Test Value"
_, err = bucket.Upsert(key, &val, 2)
if err != nil {
fmt.Println("ERROR CREATING DOCUMENT:", err)
}
// Retrieve Value Right Away
var retValue interface{}
_, err = bucket.Get(key, &retValue)
if err != nil {
fmt.Println("ERROR RETURNING DOCUMENT:", err)
}
fmt.Println("Document Not Yet Expired:", retValue)
// Sleep for 4 seconds
time.Sleep(4 * time.Second)
// Try to retrieve document when Expired
_, err = bucket.Get(key, &retValue)
if err != nil {
fmt.Println("Document Expired:", err)
}
// Create a document with no expiration, and add an expiry using
// touch after the document is created of 2 seconds
_, err = bucket.Upsert(key, &val, 2)
if err != nil {
fmt.Println("ERROR CREATING DOCUMENT:", err)
}
// Add an expiry
_, err = bucket.Touch(key, 0, 2)
if err != nil {
fmt.Println("ERROR TOUCHING DOCUMENT:", err)
}
// Retrieve Document
_, err = bucket.Get(key, &retValue)
if err != nil {
fmt.Println("ERROR RETURNING DOCUMENT:", err)
}
fmt.Println("Document Not Yet Expired:", retValue)
// Sleep for 4 seconds
time.Sleep(4 * time.Second)
// Try to retrieve document when Expired
_, err = bucket.Get(key, &retValue)
if err != nil {
fmt.Println("Document Expired:", err)
}
// Exiting
fmt.Println("Example Successful - Exiting")
}