-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathttlRunner.go
150 lines (136 loc) · 3.09 KB
/
ttlRunner.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package yiyidb
import (
"errors"
"github.com/syndtr/goleveldb/leveldb"
"os"
"sync"
"time"
)
type TtlRunner struct {
DataDir string
masterdbs *sync.Map
db *Kvdb
batchs map[string]*leveldb.Batch
localBatch *leveldb.Batch
inv int64
}
func OpenTtlRunner(dataDir string) (*TtlRunner, error) {
var err error
ttl := &TtlRunner{
DataDir: dataDir,
masterdbs: &sync.Map{},
localBatch: &leveldb.Batch{},
inv: 1000,
}
//Open TTl
ttl.db, err = OpenKvdb(ttl.DataDir, 32)
if err != nil {
return nil, err
}
go ttl.Run()
return ttl, nil
}
func (t *TtlRunner) AddTtlDb(ldb *Kvdb, dbname string) error {
if dbname == "" {
return errors.New("dbname nil")
}
t.masterdbs.Store(dbname, ldb)
return nil
}
func (t *TtlRunner) Exists(dbname, key string) bool {
if dbname == "" {
return false
}
db, ok := t.masterdbs.Load(dbname)
if !ok {
return false
}
return db.(*Kvdb).ExistsMix(dbname, key, nil)
}
func (t *TtlRunner) SetTTL(expires int, dbname, masterDbKey string) error {
//设置大于0值即设置ttl以秒为单位
if expires > 0 {
expiration := time.Now().Add(time.Duration(expires) * time.Second)
if err := t.db.PutMix(dbname, masterDbKey, IdToKeyPure(expiration.UnixNano()), 0, nil); err != nil {
return err
}
} else if expires < 0 {
//设置少于0值即取消此记当的TTL属性
if err := t.db.DelColMix(dbname, masterDbKey, nil); err != nil {
return err
}
}
return nil
}
func (t *TtlRunner) GetTTL(dbname, masterDbKey string) (float64, error) {
val, err := t.db.GetMix(dbname, masterDbKey, nil)
if err != nil {
return 0, err
}
it := time.Unix(0, KeyToIDPure(val))
return it.Sub(time.Now()).Seconds(), nil
}
func (t *TtlRunner) DelTTL(dbname, masterDbKey string) error {
return t.db.DelColMix(dbname, masterDbKey, nil)
}
func (t *TtlRunner) Run() {
for {
t.localBatch.Reset()
t.batchs = make(map[string]*leveldb.Batch)
iter := t.db.newIter(nil, nil)
now := time.Now()
for iter.Next() {
if now.UnixNano() >= KeyToIDPure(iter.Value()) {
k := iter.Key()
t.localBatch.Delete(k)
dbname, masterkey := keyToIdMix(k)
mdb, ok := t.masterdbs.Load(dbname)
if !ok {
continue
}
mk := []byte(masterkey)
master := mdb.(*Kvdb)
val, err := master.Get(mk, nil)
if err == nil {
master.onExp(dbname, mk, val)
}
if bat, ok := t.batchs[dbname]; ok {
bat.Delete(mk)
} else {
bat := &leveldb.Batch{}
bat.Delete(mk)
t.batchs[dbname] = bat
}
}
}
iter.Release()
for dbname, batch := range t.batchs {
if batch.Len() > 0 {
mdb, _ := t.masterdbs.Load(dbname)
_ = mdb.(*Kvdb).exeBatch(batch, nil)
batch.Reset()
}
}
if t.localBatch.Len() > 0 {
_ = t.db.exeBatch(t.localBatch, nil)
t.inv = 1000
} else {
use := time.Now().Sub(now).Milliseconds()
if use <= 0 {
if t.inv < 2000 {
t.inv += 500
}
} else {
t.inv = 1000 - use
}
}
time.Sleep(time.Duration(t.inv) * time.Millisecond)
}
}
func (t *TtlRunner) Close() {
_ = t.db.Close()
}
func (t *TtlRunner) Drop() {
_ = t.db.Close()
_ = os.RemoveAll(t.DataDir)
}