-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
105 lines (85 loc) · 2.86 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
package redis
import (
"strconv"
"sync"
)
var (
allStrings map[string]string = make(map[string]string)
stringsMu sync.RWMutex
)
// Set key to hold the string value. If key already holds a value, it
// is overwritten, regardless of its type. Any previous time to live
// associated with the key is discarded on successful SET operation.
func Set(key, value string) string {
stringsMu.Lock()
defer stringsMu.Unlock()
allStrings[key] = value
publish <- notice{"string", key, "", allStrings[key]}
return "OK"
}
// Get the value of key. If the key does not exist the special value nil
// is returned. An error is returned if the value stored at key is not a
// string, because GET only handles string values.
//
// Return value
// Bulk string reply: the value of key, or nil when key does not exist.
func Get(key string) string {
stringsMu.RLock()
defer stringsMu.RUnlock()
return allStrings[key]
}
// Set key to hold string value if key does not exist. In that case,
// it is equal to SET. When key already holds a value, no operation
// is performed. SETNX is short for "SET if N ot e X ists".
//
// Return value
// Integer reply, specifically:
// 1 if the key was set
// 0 if the key was not set
func Setnx(key, value string) int {
stringsMu.Lock()
defer stringsMu.Unlock()
_, exists := allStrings[key]
if exists {
return 0
}
allStrings[key] = value
publish <- notice{"string", key, "", allStrings[key]}
return 1
}
// Increments the number stored at key by one. If the key does not exist,
// it is set to 0 before performing the operation. An error is returned
// if the key contains a value of the wrong type or contains a string
// that can not be represented as integer.
//
// Return value
// String reply: the value of key after the increment
func Incr(key string) string {
stringsMu.Lock()
defer stringsMu.Unlock()
val, exists := allStrings[key]
if !exists {
val = "0"
}
i, _ := strconv.Atoi(val)
allStrings[key] = strconv.Itoa(i + 1)
publish <- notice{"string", key, "", allStrings[key]}
return allStrings[key]
}
// Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
// See INCR for extra information on increment/decrement operations.
//
// Return value
// String reply: the value of key after the decrement
func Decr(key string) string {
stringsMu.Lock()
defer stringsMu.Unlock()
val, exists := allStrings[key]
if !exists {
val = "0"
}
i, _ := strconv.Atoi(val)
allStrings[key] = strconv.Itoa(i - 1)
publish <- notice{"string", key, "", allStrings[key]}
return allStrings[key]
}