-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
54 lines (47 loc) · 1.14 KB
/
main.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"time"
)
const (
sqliteFile = "test.db"
gcSeconds = 5
)
func main() {
fmt.Printf("connect to the database.\n")
db, err := sql.Open("sqlite3", sqliteFile)
if err != nil {
log.Fatalf("failed to open database: %v\n", err)
}
fmt.Printf("instantiate a store that garbage collects every %d seconds.\n", gcSeconds)
config := Config{
GCInterval: time.Duration(gcSeconds) * time.Second,
}
ts, err := NewStore(db, config)
if err != nil {
log.Fatalf("failed to create a store: %v\n", err)
os.Exit(1)
}
time.Sleep(500 * time.Millisecond)
expiresIn := 4
fmt.Printf("create an ephemeral entity that expires in %d seconds.\n", expiresIn)
id, err := ts.Create(time.Now().Add(time.Duration(expiresIn) * time.Second))
if err != nil {
log.Fatalf("failed to create a entity: %v\n", err)
os.Exit(1)
}
fmt.Printf("sleeps past the expiration.\n")
time.Sleep(8 * time.Second)
entity, err := ts.Get(id)
if err != nil {
log.Fatalf("failed to retrieve the entity: %v\n", err)
os.Exit(1)
}
if entity == nil {
fmt.Printf("no entity was retrieved.\n")
}
}