-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis.go
43 lines (36 loc) · 840 Bytes
/
redis.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
package main
import (
"fmt"
"os"
"github.com/go-redis/redis"
)
var rdb *redis.Client
func testConnectAndAutoConnect() {
if err := rdb.Ping().Err(); err != nil {
rdb = redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_URL"),
Password: "", // no password set
DB: 0, // use default DB
})
}
}
func InitRedis() {
rdb = redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_URL"),
Password: "", // no password set
DB: 0, // use default DB
})
testConnectAndAutoConnect()
}
func RedisAdd(LongURL string, shortName string) {
testConnectAndAutoConnect()
err := rdb.Set(shortName, LongURL, 0).Err()
if err != nil {
fmt.Println("Redis Insert Error")
panic(err)
}
}
func RedisGet(shortName string) (string, error) {
testConnectAndAutoConnect()
return rdb.Get(shortName).Result()
}