package main
import (
"fmt"
"time"
"log"
"github.com/garyburd/redigo/redis"
)
func main(){
fmt.Println("Redis:")
conn,err := redisConn("","","6");
if err != nil {
log.Fatal("Error: ", err)
}
test(conn)
}
func test(conn *RedisConn) {
conn.Do("SET","xxx",1)
if xxx,err :=redis.Int(conn.Do("GET","xxx")); err == nil {
fmt.Println("xxx:",xxx)
}
conn.FlushClose()
}
////////////////////////////////////////////////////////////////
type RedisConn struct {
dbid string
redis.Conn
}
func (r *RedisConn)FlushClose() error {
if r.dbid != "" {
if _, err := r.Conn.Do("SELECT", r.dbid);err != nil {
return nil
}
}
if _, err := r.Conn.Do("FLUSHDB");err != nil {
return err
}
return r.Conn.Close()
}
func (r *RedisConn)Close() error {
return r.Conn.Close()
}
func redisConn(host,password,db string) (*RedisConn, error) {
if host == "" {
host = ":6379"
}
//conn, err := redis.Dial( "tcp", host)
conn , err := redis.DialTimeout("tcp", host, 0, 1*time.Second, 1*time.Second)
if err != nil {
return nil, err
}
if password != "" {
if _, err := conn.Do("AUTH", password); err != nil {
conn.Close()
return nil, err
}
}
if db != "" {
if _, err := conn.Do("SELECT", db);err != nil {
conn.Close()
return nil, err
}
}
return &RedisConn{dbid:db,Conn: conn}, nil
}
import (
"fmt"
"time"
"log"
"github.com/garyburd/redigo/redis"
)
func main(){
fmt.Println("Redis:")
conn,err := redisConn("","","6");
if err != nil {
log.Fatal("Error: ", err)
}
test(conn)
}
func test(conn *RedisConn) {
conn.Do("SET","xxx",1)
if xxx,err :=redis.Int(conn.Do("GET","xxx")); err == nil {
fmt.Println("xxx:",xxx)
}
conn.FlushClose()
}
////////////////////////////////////////////////////////////////
type RedisConn struct {
dbid string
redis.Conn
}
func (r *RedisConn)FlushClose() error {
if r.dbid != "" {
if _, err := r.Conn.Do("SELECT", r.dbid);err != nil {
return nil
}
}
if _, err := r.Conn.Do("FLUSHDB");err != nil {
return err
}
return r.Conn.Close()
}
func (r *RedisConn)Close() error {
return r.Conn.Close()
}
func redisConn(host,password,db string) (*RedisConn, error) {
if host == "" {
host = ":6379"
}
//conn, err := redis.Dial( "tcp", host)
conn , err := redis.DialTimeout("tcp", host, 0, 1*time.Second, 1*time.Second)
if err != nil {
return nil, err
}
if password != "" {
if _, err := conn.Do("AUTH", password); err != nil {
conn.Close()
return nil, err
}
}
if db != "" {
if _, err := conn.Do("SELECT", db);err != nil {
conn.Close()
return nil, err
}
}
return &RedisConn{dbid:db,Conn: conn}, nil
}
有疑问加站长微信联系(非本文作者)