redis_test.go 4.3 KB
Newer Older
M
Ming Deng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package redis

import (
M
Ming Deng 已提交
18
	"context"
M
Ming Deng 已提交
19
	"fmt"
20
	"os"
M
Ming Deng 已提交
21 22 23 24 25
	"testing"
	"time"

	"github.com/gomodule/redigo/redis"
	"github.com/stretchr/testify/assert"
M
Ming Deng 已提交
26

M
Ming Deng 已提交
27
	"github.com/astaxie/beego/client/cache"
M
Ming Deng 已提交
28 29 30
)

func TestRedisCache(t *testing.T) {
31 32 33 34 35 36 37

	redisAddr := os.Getenv("REDIS_ADDR")
	if redisAddr == "" {
		redisAddr = "127.0.0.1:6379"
	}

	bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, redisAddr))
M
Ming Deng 已提交
38 39 40 41
	if err != nil {
		t.Error("init err")
	}
	timeoutDuration := 10 * time.Second
M
Ming Deng 已提交
42
	if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
M
Ming Deng 已提交
43 44
		t.Error("set Error", err)
	}
M
Ming Deng 已提交
45
	if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
M
Ming Deng 已提交
46 47 48 49 50
		t.Error("check err")
	}

	time.Sleep(11 * time.Second)

M
Ming Deng 已提交
51
	if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
M
Ming Deng 已提交
52 53
		t.Error("check err")
	}
M
Ming Deng 已提交
54
	if err = bm.Put(context.Background(), "astaxie", 1, timeoutDuration); err != nil {
M
Ming Deng 已提交
55 56 57
		t.Error("set Error", err)
	}

M
Ming Deng 已提交
58 59
	val, _ := bm.Get(context.Background(), "astaxie")
	if v, _ := redis.Int(val, err); v != 1 {
M
Ming Deng 已提交
60 61 62
		t.Error("get err")
	}

M
Ming Deng 已提交
63
	if err = bm.Incr(context.Background(), "astaxie"); err != nil {
M
Ming Deng 已提交
64 65
		t.Error("Incr Error", err)
	}
M
Ming Deng 已提交
66 67
	val, _ = bm.Get(context.Background(), "astaxie")
	if v, _ := redis.Int(val, err); v != 2 {
M
Ming Deng 已提交
68 69 70
		t.Error("get err")
	}

M
Ming Deng 已提交
71
	if err = bm.Decr(context.Background(), "astaxie"); err != nil {
M
Ming Deng 已提交
72 73 74
		t.Error("Decr Error", err)
	}

M
Ming Deng 已提交
75 76
	val, _ = bm.Get(context.Background(), "astaxie")
	if v, _ := redis.Int(val, err); v != 1 {
M
Ming Deng 已提交
77 78
		t.Error("get err")
	}
M
Ming Deng 已提交
79 80
	bm.Delete(context.Background(), "astaxie")
	if res, _ := bm.IsExist(context.Background(), "astaxie"); res {
M
Ming Deng 已提交
81 82 83
		t.Error("delete err")
	}

M
Ming Deng 已提交
84
	// test string
M
Ming Deng 已提交
85
	if err = bm.Put(context.Background(), "astaxie", "author", timeoutDuration); err != nil {
M
Ming Deng 已提交
86 87
		t.Error("set Error", err)
	}
M
Ming Deng 已提交
88
	if res, _ := bm.IsExist(context.Background(), "astaxie"); !res {
M
Ming Deng 已提交
89 90 91
		t.Error("check err")
	}

M
Ming Deng 已提交
92 93
	val, _ = bm.Get(context.Background(), "astaxie")
	if v, _ := redis.String(val, err); v != "author" {
M
Ming Deng 已提交
94 95 96
		t.Error("get err")
	}

M
Ming Deng 已提交
97
	// test GetMulti
M
Ming Deng 已提交
98
	if err = bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration); err != nil {
M
Ming Deng 已提交
99 100
		t.Error("set Error", err)
	}
M
Ming Deng 已提交
101
	if res, _ := bm.IsExist(context.Background(), "astaxie1"); !res {
M
Ming Deng 已提交
102 103 104
		t.Error("check err")
	}

M
Ming Deng 已提交
105
	vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
M
Ming Deng 已提交
106 107 108 109 110 111 112 113 114 115
	if len(vv) != 2 {
		t.Error("GetMulti ERROR")
	}
	if v, _ := redis.String(vv[0], nil); v != "author" {
		t.Error("GetMulti ERROR")
	}
	if v, _ := redis.String(vv[1], nil); v != "author1" {
		t.Error("GetMulti ERROR")
	}

A
AllenX2018 已提交
116
	vv, _ = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
A
AllenX2018 已提交
117 118 119 120 121 122 123
	if vv[0] != nil {
		t.Error("GetMulti ERROR")
	}
	if v, _ := redis.String(vv[1], nil); v != "author1" {
		t.Error("GetMulti ERROR")
	}

M
Ming Deng 已提交
124
	// test clear all
M
Ming Deng 已提交
125
	if err = bm.ClearAll(context.Background()); err != nil {
M
Ming Deng 已提交
126 127 128 129 130 131
		t.Error("clear all err")
	}
}

func TestCache_Scan(t *testing.T) {
	timeoutDuration := 10 * time.Second
132 133 134 135 136 137

	addr := os.Getenv("REDIS_ADDR")
	if addr == "" {
		addr = "127.0.0.1:6379"
	}

M
Ming Deng 已提交
138
	// init
139
	bm, err := cache.NewCache("redis", fmt.Sprintf(`{"conn": "%s"}`, addr))
M
Ming Deng 已提交
140 141 142 143
	if err != nil {
		t.Error("init err")
	}
	// insert all
M
Ming Deng 已提交
144
	for i := 0; i < 100; i++ {
M
Ming Deng 已提交
145
		if err = bm.Put(context.Background(), fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil {
M
Ming Deng 已提交
146 147 148
			t.Error("set Error", err)
		}
	}
149
	time.Sleep(time.Second)
M
Ming Deng 已提交
150 151 152 153 154 155
	// scan all for the first time
	keys, err := bm.(*Cache).Scan(DefaultKey + ":*")
	if err != nil {
		t.Error("scan Error", err)
	}

M
Ming Deng 已提交
156
	assert.Equal(t, 100, len(keys), "scan all error")
M
Ming Deng 已提交
157 158

	// clear all
M
Ming Deng 已提交
159
	if err = bm.ClearAll(context.Background()); err != nil {
M
Ming Deng 已提交
160 161 162 163 164 165 166 167 168 169 170 171
		t.Error("clear all err")
	}

	// scan all for the second time
	keys, err = bm.(*Cache).Scan(DefaultKey + ":*")
	if err != nil {
		t.Error("scan Error", err)
	}
	if len(keys) != 0 {
		t.Error("scan all err")
	}
}