# get / set
127.0.0.1:6379> set name meowrain
OK
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> get name
"meowrain"
127.0.0.1:6379> get age
"20"
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists names
(integer) 0
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> exists name
(integer) 0
127.0.0.1:6379> getset name meowrainyyds #不存在值,就返回nil并且设置新的值
(nil)
127.0.0.1:6379> getset name meowrain #如果原来存在值,那么返回原来的值并更新value为新的值
"meowrainyyds"
# 批量操作
127.0.0.1:6379> mset name1 meow name2 kevien name3 john
OK
127.0.0.1:6379>
# 自增自减
127.0.0.1:6379> get n
(nil)
127.0.0.1:6379> incr n
(integer) 1
127.0.0.1:6379> incr n
(integer) 2
127.0.0.1:6379> get n
"2"
127.0.0.1:6379> decr n
(integer) 1
127.0.0.1:6379> get n
"1"
127.0.0.1:6379>
# 增加,减少
127.0.0.1:6379> incrby n 10
(integer) 10
127.0.0.1:6379> get n
"10"
127.0.0.1:6379> incrby n 20
(integer) 30
127.0.0.1:6379> get n
"30"
127.0.0.1:6379> decrby n 10
(integer) 20
127.0.0.1:6379> get n
"20"
# 倒计时自动删除
127.0.0.1:6379> setex name 20 meowrain
OK
...20 秒后
127.0.0.1:6379> get name
# 查看倒计时还有多久结束
127.0.0.1:6379> setex name 20 meowrain
OK
127.0.0.1:6379> ttl name
(integer) 18
127.0.0.1:6379> ttl name
(integer) 17
# 对设置为永不过期的key设置过期时间
127.0.0.1:6379> set name meowrain
OK
127.0.0.1:6379> get name
"meowrain"
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 8
127.0.0.1:6379> ttl name
(integer) 4
127.0.0.1:6379> get name
"meowrain"
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
(nil)
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> set a 12
QUEUED
127.0.0.1:6379(TX)> incr a
QUEUED
127.0.0.1:6379(TX)> lpush list a b c d
QUEUED
127.0.0.1:6379(TX)> exec
1) OK
packagemainimport("fmt""time""github.com/go-redis/redis")varDB*redis.Clientfuncinit(){RedisConnect("127.0.0.1:6379","123456")}funcRedisConnect(addr,passwdstring){client:=redis.NewClient(&redis.Options{Addr:addr,Password:passwd,DB:0,DialTimeout:time.Second,})err:=client.Ping().Err()iferr!=nil{panic(err)}fmt.Printf("connect redis server:%v successfully!\n",addr)DB=client}funcRedisWriteString(key,strstring){varerrerrorerr=DB.Set(key,str,0).Err()iferr!=nil{panic(err)}fmt.Printf("key %v value write into redis: %v\n",key,str)val,err:=DB.Get("name").Result()iferr!=nil{panic(err)}fmt.Printf("val read from redis: %v\n",val)}funcRedisGetString(keystring)string{val,err:=DB.Get(key).Result()iferr!=nil{panic(err)}fmt.Printf("redis get string %v is %v\n",key,val)returnval}funcCheckKeyExists(keystring)error{val,err:=DB.Exists(key).Result()iferr!=nil{returnerr}isTrue:=func(numint64)bool{returnnum!=0}ifisTrue(val){fmt.Println("存在",key)returnnil}fmt.Println("不存在",key)returnnil}funcIncrByVal(keystring,valint64){res,err:=DB.IncrBy(key,val).Result()iferr!=nil{panic(err)}fmt.Printf("key %v after IncrBy value %v is %v\n",key,val,res)}funcDecrByVal(keystring,valint64){res,err:=DB.DecrBy(key,val).Result()iferr!=nil{panic(err)}fmt.Printf("key %v after DecrByVal %v is %v\n",key,val,res)}funcDelKey(keystring){err:=DB.Del(key).Err()iferr!=nil{panic(err)}fmt.Printf("key %v del by DelVal\n",key)}funcSetExpireKey(keystring,t*time.Duration){err:=DB.Expire(key,*t).Err()iferr!=nil{panic(err)}fmt.Printf("key %v set expire time by SetExpireKey",key)}funcGetTTL(keystring)int64{returnDB.TTL(key).Val().Microseconds()}funcmain(){RedisWriteString("name","meowrainyyds")RedisWriteString("age","1")CheckKeyExists("name")CheckKeyExists("age")IncrByVal("age",12)DelKey("age")}
packagemainimport("fmt""time""github.com/go-redis/redis")varDB*redis.Clientfuncinit(){ConnectDB("127.0.0.1:6379","123456",0)}funcConnectDB(addr,passwdstring,dbint){client:=redis.NewClient(&redis.Options{Addr:addr,Password:passwd,DB:db,DialTimeout:time.Second})err:=client.Ping().Err()iferr!=nil{panic(err)}DB=clientfmt.Println("connect to redis server successfully")}funcRpush(listnamestring,values...any){DB.RPush(listname,values...)}funcLpush(listnamestring,values...any){DB.LPush(listname,values...)}funcLPop(listnamestring)string{v,err:=DB.LPop(listname).Result()iferr!=nil{panic(err)}returnv}funcRPop(listnamestring)string{v,err:=DB.RPop(listname).Result()iferr!=nil{panic(err)}returnv}funcGetListLen(listnamestring)int64{v,err:=DB.LLen(listname).Result()iferr!=nil{panic(err)}returnv}funcPrintList(listnamestring){v,err:=DB.LRange(listname,0,-1).Result()iferr!=nil{panic(err)}fmt.Printf("list range:%v\t",listname)for_,val:=rangev{fmt.Printf("%v ",val)}fmt.Println()}funcmain(){PrintList("namelist")}
packageredishashimport("fmt""time""github.com/go-redis/redis")varDB*redis.Clientfuncinit(){ConnectDB("127.0.0.1:6379","123456",0)}funcConnectDB(addr,passwdstring,dbint){client:=redis.NewClient(&redis.Options{Addr:addr,Password:passwd,DB:db,DialTimeout:time.Second,})err:=client.Ping().Err()iferr!=nil{panic(err)}DB=clientfmt.Println("connect db successfully")}funcSetHash(hashnam,fieldstring,valueany){err:=DB.HSet(hashnam,field,value).Err()iferr!=nil{panic(err)}}funcGetHash(hashname,fieldstring)(valueany){varerrerrorvalue,err=DB.HGet(hashname,field).Result()iferr!=nil{panic(err)}returnvalue}funcGetHashKeys(hashnamestring){res,err:=DB.HKeys(hashname).Result()iferr!=nil{panic(err)}fmt.Printf("keys from hashname:%v\n",hashname)for_,v:=rangeres{fmt.Printf("%v ",v)}fmt.Println()}funcHashExists(hashname,fieldstring)bool{returnDB.HExists(hashname,field).Val()}funcGetHashLen(hashnamestring)int64{returnDB.HLen(hashname).Val()}funcGetHashVals(hashnamestring){res,err:=DB.HVals(hashname).Result()iferr!=nil{panic(err)}fmt.Printf("hash vals from %v: \n",hashname)for_,v:=rangeres{fmt.Printf("%v ",v)}fmt.Println()}
packagemainimport("fmt""time""github.com/go-redis/redis")varDB*redis.Clientfuncinit(){ConnectDB("127.0.0.1:6379","123456",0)}funcConnectDB(addr,passwdstring,dbint){client:=redis.NewClient(&redis.Options{Addr:addr,Password:passwd,DB:db,DialTimeout:time.Second,})err:=client.Ping().Err()iferr!=nil{panic(err)}DB=clientfmt.Println("connect db successfully")}funcCretaeTransaction(){_,err:=DB.TxPipelined(func(piperedis.Pipeliner)error{pipe.Set("key1","1",0)pipe.IncrBy("key1",12)returnnil})iferr!=nil{panic(err)}else{fmt.Println("Transaction succeed")}}funcmain(){CretaeTransaction()}