Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
124 lines (108 loc) · 3.65 KB
/
Copy pathcache.go
File metadata and controls
124 lines (108 loc) · 3.65 KB
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
package cache
import (
"bytes"
"context"
"encoding/json"
"io"
"strings"
"time"
"github.com/rockbears/log"
)
// PubSub represents a subscriber
type PubSub interface {
Unsubscribe(channels ...string) error
GetMessage(c context.Context) (string, error)
}
//Key make a key as expected
func Key(args ...string) string {
return strings.Join(args, ":")
}
//Store is an interface
type Store interface {
Get(key string, value interface{}) (bool, error)
Set(key string, value interface{}) error
SetWithTTL(key string, value interface{}, ttl int) error
SetWithDuration(key string, value interface{}, duration time.Duration) error
UpdateTTL(key string, ttl int) error
Delete(key string) error
DeleteAll(key string) error
Exist(key string) (bool, error)
Eval(expr string, args ...string) (string, error)
HealthStore
LockStore
QueueStore
PubSubStore
ScoredSetStore
SetStore
}
type HealthStore interface {
Ping() error
DBSize() (int64, error)
Size(key string) (int64, error)
Keys(pattern string) ([]string, error)
}
type QueueStore interface {
Enqueue(queueName string, value interface{}) error
DequeueWithContext(c context.Context, queueName string, waitDuration time.Duration, value interface{}) error
DequeueJSONRawMessagesWithContext(c context.Context, queueName string, waitDuration time.Duration, maxElements int) ([]json.RawMessage, error)
QueueLen(queueName string) (int, error)
RemoveFromQueue(queueName string, memberKey string) error
}
type SetStore interface {
SetAdd(rootKey string, memberKey string, member interface{}) error
SetRemove(rootKey string, memberKey string, member interface{}) error
SetCard(key string) (int, error)
SetScan(ctx context.Context, key string, members ...interface{}) error
SetSearch(key, pattern string) ([]string, error)
}
type PubSubStore interface {
Publish(ctx context.Context, queueName string, value interface{}) error
Subscribe(queueName string) (PubSub, error)
}
type LockStore interface {
Lock(key string, expiration time.Duration, retryWaitDurationMillisecond int, retryCount int) (bool, error)
Unlock(key string) error
}
type ScoredSetStore interface {
Delete(key string) error
ScoredSetAdd(ctx context.Context, key string, value interface{}, score float64) error
ScoredSetAppend(ctx context.Context, key string, value interface{}) error
ScoredSetScan(ctx context.Context, key string, from, to float64, dest interface{}) error
ScoredSetScanWithScores(ctx context.Context, key string, from, to float64) ([]SetValueWithScore, error)
ScoredSetScanMaxScore(ctx context.Context, key string) (*SetValueWithScore, error)
ScoredSetRange(ctx context.Context, key string, from, to int64, dest interface{}) error
ScoredSetRevRange(_ context.Context, key string, offset int64, limit int64, dest interface{}) error
ScoredSetRem(ctx context.Context, key string, members ...string) error
ScoredSetGetScore(key string, member interface{}) (float64, error)
SetCard(key string) (int, error)
Eval(expr string, args ...string) (string, error)
HealthStore
}
type SetValueWithScore struct {
Score float64
Value json.RawMessage
}
//New init a cache
func New(redisHost, redisPassword string, dbindex, TTL int) (Store, error) {
return NewRedisStore(redisHost, redisPassword, dbindex, TTL)
}
//NewWriteCloser returns a write closer
func NewWriteCloser(store Store, key string, ttl int) io.WriteCloser {
return &writerCloser{
store: store,
key: key,
ttl: ttl,
}
}
type writerCloser struct {
store Store
key string
ttl int
bytes.Buffer
}
func (w *writerCloser) Close() error {
if err := w.store.SetWithTTL(w.key, w.String(), w.ttl); err != nil {
log.Error(context.TODO(), "cannot SetWithTTL: %s: %v", w.key, err)
}
return nil
}