-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrings.go
172 lines (158 loc) · 4.32 KB
/
strings.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package godis
import (
"errors"
"reflect"
"strconv"
"unicode/utf8"
)
// SET is used to assign a value to a key
func (g *Godis) SET(key string, value string) (string, error) {
if got, _ := g.EXISTS(key); got == 1 {
s, exists := g.getSDS(key)
if exists {
s.Lock()
defer s.Unlock()
s.value = value
}
} else {
s := NewSDS(value)
g.db[key] = s
}
return key, nil
}
// GET returns the value stored for a key
func (g *Godis) GET(key string) (string, error) {
s, exists := g.getSDS(key)
if exists {
return s.get(), nil
}
return "", errors.New("keynotexists")
}
// SETEX is used to assign a value to a key and destroy it within its given
//expiry time in seconds, returns the key,<nil> if set or "",<nil>
func (g *Godis) SETEX(key string, exp uint64, value string) (string, error) {
if exp <= 0 {
return "", errors.New("badexpiry")
}
g.SET(key, value)
go g.destroyInSecs(key, exp)
return key, nil
}
// PSETEX is used to assign a value to a key and destroy it within its given
// expiry time in milliseconds, returns the key,<nil> if set or "",<nil>
func (g *Godis) PSETEX(key string, exp uint64, value string) (string, error) {
if exp <= 0 {
return "", errors.New("badexpiry")
}
g.SET(key, value)
go g.destroyInMillis(key, exp)
return key, nil
}
// INCR increments the key by one
func (g *Godis) INCR(key string) (int, error) {
return g.INCRBY(key, 1)
}
// DECR decrements the key by one
func (g *Godis) DECR(key string) (int, error) {
return g.DECRBY(key, 1)
}
// INCRBY increments the key by given value
func (g *Godis) INCRBY(key string, n int) (int, error) {
var val string
var err error
val, err = g.GET(key)
if val == "" || err != nil {
g.SET(key, "0")
val, _ = g.GET(key)
}
valInt, convErr := strconv.Atoi(val)
if convErr != nil {
return 0, errors.New("typemismatch")
}
valInt += n
g.SET(key, strconv.Itoa(valInt))
return valInt, nil
}
// DECRBY decrements the key by given value
func (g *Godis) DECRBY(key string, n int) (int, error) {
return g.INCRBY(key, -n)
}
// Increment the string representing a floating point number stored at key by the
//specified increment. If the key does not exist, it is set to 0 before
//performing the operation.
func (g *Godis) INCRBYFLOAT(key string, n float64) (float64, error) {
var val string
var err error
val, err = g.GET(key)
if val == "" || err != nil {
g.SET(key, "0")
val, _ = g.GET(key)
}
valFlt, convErr := strconv.ParseFloat(val, 64)
if convErr != nil {
return 0, errors.New("typemismatch")
}
valFlt += n
g.SET(key, strconv.FormatFloat(valFlt, 'E', -1, 64))
return valFlt, nil
}
// MGET returns a slice of values for a input slice of keys
func (g *Godis) MGET(keys ...string) ([]interface{}, error) {
var output []interface{} // will be strings or nils
for _, key := range keys {
value, err := g.GET(key)
if err == nil {
output = append(output, value)
} else {
// if the key isn't available, append a nil instead
// TODO: how to do multi-return way if we have a slice to return?
output = append(output, nil)
}
}
return output, nil
}
// MSET sets a slice of key-values
// pass a slice of keys and value alternating
// eg: "key1", "value1", "key2", "value2"
// returns true, <nil> as MSET never fails!
func (g *Godis) MSET(items ...string) (bool, error) {
g.Lock()
defer g.Unlock()
for i, item := range items {
if i%2 == 1 {
continue
}
g.SET(item, items[i+1])
}
return true, nil
}
//STRLEN returns the length of the string value stored at key.
//An error is returned when key holds a non-string value.
func (g *Godis) STRLEN(key string) (int64, error) {
val, err := g.GET(key)
if err != nil && err.Error() == "keynotexists" {
return 0, errors.New("keynotexists")
}
if reflect.ValueOf(val).Kind() != reflect.String {
return 0, errors.New("typemismatch")
}
return int64(utf8.RuneCountInString(val)), nil
}
// If key already exists and is a string, this command appends the value at the end
// of the string. If key does not exist it is created and set as an empty string.
func (g *Godis) APPEND(key string, val string) (int64, error) {
exst, _ := g.EXISTS(key)
if exst == 0 {
g.SET(key, val)
ln, _ := g.STRLEN(key)
return ln, nil
}
got, _ := g.GET(key)
if reflect.ValueOf(got).Kind() != reflect.String {
return 0, errors.New("typemismatch")
}
newVal := got + val
g.SET(key, newVal)
ln, _ := g.STRLEN(key)
return ln, nil
}