forked from cnu/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.go
More file actions
133 lines (121 loc) · 3.23 KB
/
strings.go
File metadata and controls
133 lines (121 loc) · 3.23 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
125
126
127
128
129
130
131
132
133
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)
}
// 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
}