1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package redis
- import (
- "fmt"
- "github.com/gomodule/redigo/redis"
- "github.com/spf13/viper"
- "math/rand"
- "strconv"
- "testing"
- )
- func TestRedisRepository(t *testing.T) {
- r := NewTestRepository()
- for i := 0; i < 10; i++ {
- err := r.Save2("test", []byte(strconv.Itoa(rand.Intn(100))), 0)
- if nil != err {
- println("Redis error:", err)
- }
- }
- }
- // Redis操作类示例
- type TestRepository struct {
- hub *Hub
- }
- func NewTestRepository() *TestRepository {
- viper.SetDefault("redis.host", "127.0.0.1:6379")
- hub := NewHub()
- return &TestRepository{hub: hub}
- }
- func (r *TestRepository) Save2(key string, value []byte, expire int) error {
- var err error
- var reply string
- if expire > 0 {
- reply, err = redis.String(r.hub.Do("SET", key, value, expire))
- } else {
- reply, err = redis.String(r.hub.Do("SET", key, value))
- }
- if nil != err {
- return err
- } else if reply != "" {
- println(reply)
- }
- return nil
- }
- func (r *TestRepository) Save(key string, value []byte) error {
- fmt.Printf("input: %s\n", string(value))
- var err error
- var i interface{}
- i, err = r.hub.Do("SET", key, value)
- if nil != err {
- fmt.Printf("%T\n", err)
- return err
- }
- fmt.Printf("%T\n", i)
- println(fmt.Sprintf("%s", i))
- return nil
- }
- func (r *TestRepository) Test(key string, value []byte) error {
- i, err := r.hub.Do("HSET", key, []byte("code"), value)
- if nil != err {
- fmt.Printf("%T\n", err)
- return err
- }
- fmt.Printf("%T\n", i)
- println(fmt.Sprintf("%d", i))
- return nil
- }
|