forked from edwardwc/better-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
79 lines (64 loc) · 1.66 KB
/
cache.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
package badgers3
import (
"fmt"
"github.com/dgraph-io/badger"
"time"
)
var (
db = getCacheDb()
)
// handleError will attempt to handle and show any errors thrown by BadgerDB
func handleCacheError(err error) {
if err != nil {
return
}
}
// getCacheDb will open a new BadgerDB for the current S3 instance
func getCacheDb() *badger.DB {
db, err := badger.Open(badger.DefaultOptions("/tmp/badger-s3"))
if err != nil {
_ = fmt.Errorf("unable to open badgerdb, check that there isn't already an instance running")
}
return db
}
// setCacheEntry will set an object into the Badger DB
func setCacheEntry(key []byte, data []byte, ttl time.Duration) {
err := db.Update(func(txn *badger.Txn) error {
e := badger.NewEntry(key, data).WithTTL(ttl).WithDiscard()
err := txn.SetEntry(e)
handleCacheError(err)
return err
})
handleCacheError(err)
}
// getCacheEntry will return a cache entry as a string
func getCacheEntry(key []byte) (model *string) {
var valCopy []byte
err := db.View(func(txn *badger.Txn) error {
item, err := txn.Get(key)
handleCacheError(err)
if err == nil {
err = item.Value(func(val []byte) error {
valCopy = append([]byte{}, val...)
return nil
})
}
return err
})
handleCacheError(err)
if err == nil {
strVal := string(valCopy)
return &strVal
}
return nil
}
// isCacheEntryExistent will return true when the given key exists in the cache storage, false otherwise
func isCacheEntryExistent(key []byte) bool {
err := db.View(func(txn *badger.Txn) error {
_, err := txn.Get(key)
return err
})
// If we have no error using txn.Get for a key then the key exists
// Otherwise the key does not exist
return err == nil
}