/
keeper.go
82 lines (66 loc) · 2.19 KB
/
keeper.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
package score
import "github.com/mediocregopher/radix.v2/redis"
// Listing of Redis commands that we need to work with sets.
const (
countries = "ALL_COUNTRIES"
parties = "ALL_PARTIES"
redisGet = "GET"
redisIncr = "INCR"
redisSAdd = "SADD"
redisSRem = "SREM"
redisSMembers = "SMEMBERS"
)
// ConnectionPool used to make requests to Redis. Its size is provided by Configuration during application init.
type ConnectionPool interface {
Cmd(cmd string, args ...interface{}) *redis.Resp
}
// Keeper is an implemetation of ScoreKeeper that uses Redis.
type Keeper struct {
pool ConnectionPool
}
// NewKeeper returns pointer to created Keeper instance initialized with Redis pool.
func NewKeeper(p ConnectionPool) *Keeper {
return &Keeper{pool: p}
}
// Get returns current score for given key.
func (d Keeper) Get(key string) (int, error) {
return d.pool.Cmd(redisGet, key).Int()
}
// AddPoint increments counter by one for a given key.
func (d Keeper) AddPoint(key string) error {
_, err := d.pool.Cmd(redisIncr, key).Int()
return err
}
// AddCountry will create country record in set of all countries.
func (d Keeper) AddCountry(c string) error {
return d.sadd(countries, c)
}
// AddCandidate adds the one to current voting.
func (d Keeper) AddCandidate(p string) error {
return d.sadd(parties, p)
}
// RemoveCandidate deletes single candidate with specified name.
func (d Keeper) RemoveCandidate(p string) error {
return d.srem(parties, p)
}
// GetAllCandidates returns all candidates currently taking part in voting.
func (d Keeper) GetAllCandidates() ([]string, error) {
return d.smembers(parties)
}
// GetAllCountries returnes all countries that were participating during voting.
func (d Keeper) GetAllCountries() ([]string, error) {
return d.smembers(countries)
}
func (d Keeper) sadd(set, name string) error {
_, err := d.pool.Cmd(redisSAdd, set, name).Int()
return err
}
func (d Keeper) srem(set, name string) error {
_, err := d.pool.Cmd(redisSRem, set, name).Int()
return err
}
// smembers WILL SLOWDOWN your SERVER if used with large sets.
func (d Keeper) smembers(set string) ([]string, error) {
response, err := d.pool.Cmd(redisSMembers, set).List()
return response, err
}