@@ -3,6 +3,7 @@ package cache
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ cacheNew "github.com/patrickmn/go-cache"
6
7
"time"
7
8
8
9
log "unknwon.dev/clog/v2"
@@ -15,6 +16,7 @@ import (
15
16
var ctx = context .Background ()
16
17
17
18
var GlobalCache * Cache
19
+ var DumpCache * cacheNew.Cache
18
20
19
21
func (db * Config ) Init () {
20
22
c := & Cache {
@@ -32,6 +34,7 @@ func (db *Config) Init() {
32
34
log .Fatal ("unsupported database type: %s" , db .Type )
33
35
}
34
36
GlobalCache = c
37
+ DumpCache = cacheNew .New (15 * time .Second , 60 * time .Second )
35
38
}
36
39
37
40
func (cache * Cache ) Set (key string , v interface {}) {
@@ -72,3 +75,21 @@ func (cache *Cache) Get(key string) (interface{}, error) {
72
75
}
73
76
return nil , fmt .Errorf ("no cache type" )
74
77
}
78
+
79
+ func (cache * Cache ) GetOnce (key string ) (interface {}, error ) {
80
+ switch cache .Type {
81
+ case "self" :
82
+ if val , hasKey := cache .Self .Get (key ); hasKey {
83
+ cache .Self .Delete (key )
84
+ return val , nil
85
+ }
86
+ return nil , fmt .Errorf ("no cache" )
87
+ case "redis" :
88
+ result , err := cache .Redis .Get (ctx , key ).Result ()
89
+ cache .Redis .Del (ctx , key ).Result ()
90
+ return result , err
91
+ case "none" :
92
+ return nil , fmt .Errorf ("no cache" )
93
+ }
94
+ return nil , fmt .Errorf ("no cache type" )
95
+ }
0 commit comments