-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
103 lines (78 loc) · 2.13 KB
/
cache.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
)
type DBConnect struct {
Username string
Database string
Hostname string
Port int
Password string
}
type Record struct {
ID int `json:"id"`
CreatedAt string `json:"created_at"`
Key string `json:"key"`
Value string `json:"value"`
TTL int `json:"ttl"`
}
type Filters struct {
Key string
}
func connectDB(c DBConnect) *sql.DB {
connectionString := fmt.Sprintf("user=%v dbname=%v host=%v port=%v password=%v", c.Username, c.Database, c.Hostname, c.Port, c.Password)
db, err := sql.Open("postgres", connectionString)
if err != nil {
log.Printf("Unable to connect to: %v, %v, %v, %v.", c.Hostname, c.Database, c.Port, c.Username)
}
return db
}
func GetCache(dbh *sql.DB, key string) (Record, error) {
query := fmt.Sprintf(`SELECT id, created_at, key, value, ttl
FROM sql_cache
WHERE key = $1 AND extract(epoch from now()) < (extract(epoch from created_at) + ttl)
ORDER by id desc limit 1`)
var r Record
err := dbh.QueryRow(query, key).Scan(&r.ID, &r.CreatedAt, &r.Key, &r.Value, &r.TTL)
if err != nil {
if err == sql.ErrNoRows {
// there were no rows, but otherwise no error occurred
} else {
log.Printf("Query - %s (%s).", query, key)
return r, err
}
}
return r, nil
}
func SetCache(dbh *sql.DB, r Record) (sql.Result, error) {
query := fmt.Sprintf(`INSERT into sql_cache (key, value, ttl)
VALUES ($1, $2, $3)`)
stmt, err := dbh.Prepare(query)
if err != nil {
log.Printf("Prepare error - %s", query)
return nil, err
}
result, err := stmt.Exec(r.Key, r.Value, r.TTL)
if err != nil {
log.Printf("Query - %s (%s, %s, %d).", query, r.Key, r.Value, r.TTL)
return result, err
}
return result, nil
}
func ClearCache(dbh *sql.DB, key string) (sql.Result, error) {
query := fmt.Sprintf(`UPDATE sql_cache SET ttl = 0 WHERE key = $1`)
stmt, err := dbh.Prepare(query)
if err != nil {
log.Printf("Prepare error - %s", query)
return nil, err
}
result, err := stmt.Exec(key)
if err != nil {
log.Printf("Query - %s (%s).", query, key)
return result, err
}
return result, nil
}