-
Notifications
You must be signed in to change notification settings - Fork 29
/
tx_str.go
107 lines (85 loc) · 2.2 KB
/
tx_str.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
104
105
106
107
package flashdb
import (
"time"
)
// Set saves a key-value pair.
func (tx *Tx) Set(key string, value string) error {
e := newRecord([]byte(key), []byte(value), StringRecord, StringSet)
tx.addRecord(e)
return nil
}
// SetEx sets key-value pair with given duration time for expiration.
func (tx *Tx) SetEx(key string, value string, duration int64) (err error) {
if duration <= 0 {
return ErrInvalidTTL
}
ttl := time.Now().Unix() + duration
e := newRecordWithExpire([]byte(key), nil, ttl, StringRecord, StringExpire)
tx.addRecord(e)
return
}
// Get returns value of the given key. It may return error if something goes wrong.
func (tx *Tx) Get(key string) (val string, err error) {
val, err = tx.get(key)
if err != nil {
return
}
return
}
// Delete deletes the given key.
func (tx *Tx) Delete(key string) error {
e := newRecord([]byte(key), nil, StringRecord, StringRem)
tx.addRecord(e)
return nil
}
// Expire adds a expiration time period to the given key.
func (tx *Tx) Expire(key string, duration int64) (err error) {
if duration <= 0 {
return ErrInvalidTTL
}
if _, err = tx.get(key); err != nil {
return
}
ttl := time.Now().Unix() + duration
e := newRecordWithExpire([]byte(key), nil, ttl, StringRecord, StringExpire)
tx.addRecord(e)
return
}
// TTL returns remaining time of the expiration.
func (tx *Tx) TTL(key string) (ttl int64) {
deadline := tx.db.getTTL(String, key)
if deadline == nil {
return
}
if tx.db.hasExpired(key, String) {
tx.db.evict(key, String)
return
}
return deadline.(int64) - time.Now().Unix()
}
// Exists checks the given key whether exists. Also, if the key is expired,
// the key is evicted and return false.
func (tx *Tx) Exists(key string) bool {
_, err := tx.db.strStore.get(key)
if err != nil {
if err == ErrExpiredKey {
tx.db.evict(key, String)
}
return false
}
return true
}
// get is a helper method for retrieving value of the given key from the database.
func (tx *Tx) get(key string) (val string, err error) {
v, err := tx.db.strStore.get(key)
if err != nil {
return "", err
}
// Check if the key is expired.
if tx.db.hasExpired(key, String) {
tx.db.evict(key, String)
return "", ErrExpiredKey
}
val = v.(string)
return
}