-
Notifications
You must be signed in to change notification settings - Fork 24
/
mongo.go
115 lines (95 loc) · 2.61 KB
/
mongo.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
// Package mongo providers a cache driver that stores the cache in MongoDB.
package mongo
import (
"context"
"time"
"github.com/faabiosr/cachego"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type (
mongoCache struct {
collection *mongo.Collection
}
mongoContent struct {
Duration int64
Key string `bson:"_id"`
Value string
}
)
// New creates an instance of Mongo cache driver
func New(collection *mongo.Collection) cachego.Cache {
return &mongoCache{collection}
}
// NewMongoDriver alias for New.
func NewMongoDriver(collection *mongo.Collection) cachego.Cache {
return New(collection)
}
func (m *mongoCache) Contains(key string) bool {
_, err := m.Fetch(key)
return err == nil
}
// Delete the cached key from Mongo storage
func (m *mongoCache) Delete(key string) error {
_, err := m.collection.DeleteOne(context.TODO(), bson.M{"_id": bson.M{"$eq": key}})
return err
}
// Fetch retrieves the cached value from key of the Mongo storage
func (m *mongoCache) Fetch(key string) (string, error) {
content := &mongoContent{}
result := m.collection.FindOne(context.TODO(), bson.M{"_id": bson.M{"$eq": key}})
if result == nil {
return "", cachego.ErrCacheExpired
}
if result.Err() != nil {
return "", result.Err()
}
err := result.Decode(&content)
if err != nil {
return "", err
}
if content.Duration == 0 {
return content.Value, nil
}
if content.Duration <= time.Now().Unix() {
_ = m.Delete(key)
return "", cachego.ErrCacheExpired
}
return content.Value, nil
}
func (m *mongoCache) FetchMulti(keys []string) map[string]string {
result := make(map[string]string)
cur, err := m.collection.Find(context.TODO(), bson.M{"_id": bson.M{"$in": keys}})
if err != nil {
return result
}
defer func() {
_ = cur.Close(context.Background())
}()
content := &mongoContent{}
for cur.Next(context.Background()) {
err := cur.Decode(content)
if err != nil {
continue
}
result[content.Key] = content.Value
}
return result
}
// Flush removes all cached keys of the Mongo storage
func (m *mongoCache) Flush() error {
_, err := m.collection.DeleteMany(context.TODO(), bson.M{})
return err
}
// Save a value in Mongo storage by key
func (m *mongoCache) Save(key string, value string, lifeTime time.Duration) error {
duration := int64(0)
if lifeTime > 0 {
duration = time.Now().Unix() + int64(lifeTime.Seconds())
}
content := &mongoContent{duration, key, value}
opts := options.Replace().SetUpsert(true)
_, err := m.collection.ReplaceOne(context.TODO(), bson.M{"_id": bson.M{"$eq": key}}, content, opts)
return err
}