-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcache.go
52 lines (41 loc) · 1.12 KB
/
cache.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
// Copyright 2018-2019 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package sched
import (
"time"
"github.com/go-redis/redis"
)
type cache struct {
client *redis.Client
}
func newCache(url string) (*cache, error) {
opt, err := redis.ParseURL(url)
if err != nil {
return nil, err
}
return &cache{redis.NewClient(opt)}, nil
}
// GET command of redis
func (c *cache) Get(key string) (value string, err error) {
return c.client.Get(key).Result()
}
// SET command of redis
func (c *cache) Set(key, value string) (err error) {
return c.client.Set(key, value, 0).Err()
}
// DEL command of redis
func (c *cache) Del(key string) (err error) {
return c.client.Del(key).Err()
}
// SETNX command of redis
func (c *cache) SetNX(key string, value string, expire time.Duration) (ok bool, err error) {
return c.client.SetNX(key, value, expire).Result()
}
// KEYS command of redis
func (c *cache) Keys(prefix string) (keys []string, err error) {
return c.client.Keys(prefix + "*").Result()
}
func (c *cache) Close() error {
return c.client.Close()
}