-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqcache.go
131 lines (115 loc) · 3.74 KB
/
qcache.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
package qcache
import (
"fmt"
"time"
)
// A small utility for an in-memory cache
// TODO: use generics instead of interface{}
// constants for the type of value stored in the cache
const (
TYPE_STRING int = 0 // value is a string
TYPE_BYTES int = 1 // value is a byte array
)
// structure that describes a value in the cache
type QCacheVal struct {
Type int // type of value stored in the cache (string or byte array)
Value interface{} // value stored in the cache
Timestamp int64 // timestamp when the value was added
}
// structure that describes the method required of a cache store
type IQCacheStore interface {
ListKeys() ([]string, error) // list all keys in the cache
ListStrKeys() ([]string, error) // list all keys in the cache that are strings
ListBytesKeys() ([]string, error) // list all keys in the cache that are byte arrays
GetStr(key string) (string, error) // get a string value from the cache
SetStr(key string, value string) error // set a string value in the cache
GetBytes(key string) ([]byte, error) // get a byte array value from the cache
SetBytes(key string, value []byte) error // set a byte array value in the cache
HasKey(key string) (bool, int64, error) // check if a key exists in the cache
DeleteKey(key string) error // delete a key from the cache
}
// The quickcache structure store
type QCacheStore struct {
data map[string]QCacheVal // map of key to value
// future struct fields go here
}
// create a new cache store
func NewQCStore() (*QCacheStore, error) {
data := make(map[string]QCacheVal)
qcs := QCacheStore{data: data}
return &qcs, nil
}
// list all keys in the cache (keys are strings)
func (qcs *QCacheStore) ListAllKeys() ([]string, error) {
keys := make([]string, 0)
for k := range qcs.data {
keys = append(keys, k)
}
return keys, nil
}
// list all keys in the cache that are strings
func (qcs *QCacheStore) ListStrKeys() ([]string, error) {
keys := make([]string, 0)
for k := range qcs.data {
if qcs.data[k].Type == TYPE_STRING {
keys = append(keys, k)
}
}
return keys, nil
}
// list all keys in the cache that are byte arrays
func (qcs *QCacheStore) ListBytesKeys() ([]string, error) {
keys := make([]string, 0)
for k := range qcs.data {
if qcs.data[k].Type == TYPE_BYTES {
keys = append(keys, k)
}
}
return keys, nil
}
// check if a key exists in the cache
func (qcs *QCacheStore) HasKey(key string) (bool, int64, error) {
val, ok := qcs.data[key]
if !ok {
return false, 0, nil
}
return true, val.Timestamp, nil
}
// get a key that represents a string value from the cache
func (qcs *QCacheStore) GetStr(key string) (*QCacheVal, error) {
val, ok := qcs.data[key]
if !ok {
return nil, fmt.Errorf("key not found")
}
if val.Type != TYPE_STRING {
return nil, fmt.Errorf("value is not a string")
}
return &val, nil
}
// set a key that represents a string value in the cache
func (qcs *QCacheStore) SetStr(key string, value string) error {
currentUnixEpoch := int64(time.Now().Unix())
qcs.data[key] = QCacheVal{TYPE_STRING, value, currentUnixEpoch}
return nil
}
// get a key that represents a byte array value from the cache
func (qcs *QCacheStore) GetBytes(key string) (*QCacheVal, error) {
val, ok := qcs.data[key]
if !ok {
return nil, fmt.Errorf("key not found")
}
if val.Type != TYPE_BYTES {
return nil, fmt.Errorf("value is not a byte array")
}
return &val, nil
}
// set a key that represents a byte array value in the cache
func (qcs *QCacheStore) SetBytes(key string, value []byte) error {
currentUnixEpoch := int64(time.Now().Unix())
qcs.data[key] = QCacheVal{TYPE_BYTES, value, currentUnixEpoch}
return nil
}
func (qcs *QCacheStore) DeleteKey(key string) error {
delete(qcs.data, key)
return nil
}