Linux 系统中 Redis 的安装及其使用
安裝及使用 redis
安裝
sudo apt-get install redis-server# 安裝后, Redis 會自動啟動,通過下列命令檢查ps -elf|grep redis netstat -nltp}grep 6379官網(wǎng)
- Redis 官網(wǎng):https://redis.io/
- Redis 在線測試:http://try.redis.io/
設(shè)置
- 默認,訪問 Redis 服務(wù)器不需要密碼,如需要可通過如下命令設(shè)置。
- 打開 Redis 服務(wù)器可被遠程訪問
- 修改后,重啟 Redis 服務(wù)器
- 若打開密碼后,再次無密登錄 redis-cli ,會發(fā)現(xiàn)可以登錄,但是不能執(zhí)行命令。
簡單使用
- 關(guān)于命令的 API 或者使用方法,可以參見官網(wǎng),見文前。
-
Redis 可以存儲的常用數(shù)據(jù)結(jié)構(gòu)有:
- String: 字符串
- Hash: 散列
- List: 列表
- Set: 集合
- Sorted Set: 有序集合 (zset)
-
Redis支持多個數(shù)據(jù)庫,并且每個數(shù)據(jù)庫的數(shù)據(jù)是隔離的不能共享,并且基于單機才有,如果是集群就沒有數(shù)據(jù)庫的概念。Redis是一個字典結(jié)構(gòu)的存儲服務(wù)器,而實際上一個Redis實例提供了多個用來存儲數(shù)據(jù)的字典,客戶端可以指定將數(shù)據(jù)存儲在哪個字典中。這與我們熟知的在一個關(guān)系數(shù)據(jù)庫實例中可以創(chuàng)建多個數(shù)據(jù)庫類似,所以可以將其中的每個字典都理解成一個獨立的數(shù)據(jù)庫。每個數(shù)據(jù)庫對外都是一個從0開始的遞增數(shù)字命名,Redis默認支持16個數(shù)據(jù)庫(可以通過配置文件支持更多,無上限),可以通過配置databases來修改這一數(shù)字。客戶端與Redis建立連接后會自動選擇0號數(shù)據(jù)庫,不過可以隨時使用SELECT命令更換數(shù)據(jù)庫,如要選擇1號數(shù)據(jù)庫.select 1
Go 語言中連接 Redis 的包
-
golang中比較好用的第三方開源redisclient有:
-
go-redis
-
源碼地址:https://github.com/go-redis/redis
-
文檔地址:http://godoc.org/github.com/go-redis/redis
-
-
redigo
-
源碼地址:https://github.com/gomodule/redigo
-
文檔地址:http://godoc.org/github.com/gomodule/redigo/redis
-
兩個都是非常優(yōu)秀的 redisclient 庫,也是redis官網(wǎng)上推薦,筆者選擇是的是go-redis,因為go-redis封裝了redis的大部分命令,不用關(guān)心 redis 的命令的細節(jié),直接調(diào)用相應(yīng)接口就行;redigo 是基于命令的,發(fā)送一個命令,然后在解析 reply。
go-redis 連接 Redis 的 Demo
// redis_test project main.go package mainimport ("fmt""time""github.com/go-redis/redis" )func err_handler(err error) {fmt.Printf("err_handler, error:%s\n", err.Error())panic(err.Error()) }func standalon_redis_test() {fmt.Printf("standalon_redis_test")client := redis.NewClient(&redis.Options{Addr: "localhost:6379",Password: "",DB: 0,})defer client.Close()pong, err := client.Ping().Result()if err != nil {fmt.Printf("ping error[%s]\n", err.Error())err_handler(err)}fmt.Printf("ping result: %s\n", pong)fmt.Printf("----------------------------------------\n")// set / get testfmt.Printf("set/get test\n")err = client.Set("foo", "bar", 0).Err()if err != nil {fmt.Printf("try set key[foo] to value[bar] error[%s]\n",err.Error())err_handler(err)}err = client.Set("foo1", "bar1", time.Hour*2).Err()if err != nil {fmt.Printf("try set key[foo1] to value[bar1] error[%s]\n",err.Error())err_handler(err)}// get valuevalue, err := client.Get("foo").Result()if err != nil {fmt.Printf("try get key[foo] error[%s]\n", err.Error())// err_handler(err)}fmt.Printf("key[foo]'s value is %s\n", value)value, err = client.Get("foo1").Result()if err != nil {fmt.Printf("try get key[foo1] error[%s]\n", err.Error())// err_handler(err)}fmt.Printf("key[foo1]'s value is %s\n", value)value, err = client.Get("foo2").Result()if err != nil {fmt.Printf("try get key[foo2] error[%s]\n", err.Error())// err_handler(err)}fmt.Printf("key[foo2]'s value is %s\n", value)// get ttlduration, err := client.TTL("foo").Result()if err != nil {fmt.Printf("try get ttl of key[foo] error[%s]\n", err.Error())err_handler(err)}fmt.Printf("key[foo]' ttl is [%s] %fs\n",duration.String(), duration.Seconds())duration, err = client.TTL("foo1").Result()if err != nil {fmt.Printf("try get ttl of key[foo1] error[%s]\n", err.Error())err_handler(err)}fmt.Printf("key[foo1]' ttl is [%s] %ds\n",duration.String(), int64(duration.Seconds()))fmt.Printf("----------------------------------------\n")// list testfmt.Printf("list test\n")err = client.RPush("tqueue", "tmsg1").Err()if err != nil {fmt.Printf("rpush list[tqueue] error[%s]\n", err.Error())err_handler(err)}list_len, err := client.LLen("tqueue").Result()if err != nil {fmt.Printf("try get len of list[tqueue] error[%s]\n",err.Error())err_handler(err)}fmt.Printf("len of list[tqueue] is %d\n", list_len)result, err := client.BLPop(time.Second*1, "tqueue").Result()if err != nil {fmt.Printf("blpop list[tqueue] error[%s]\n", err.Error())err_handler(err)}fmt.Printf("blpop list[tqueue], value[%s]\n", result[1])fmt.Printf("----------------------------------------\n")fmt.Printf("hmap test\n")err = client.HSet("tmap", "1", "f1").Err()if err != nil {fmt.Printf("try hset map[tmap] field[1] error[%s]\n",err.Error())err_handler(err)}err = client.HSet("tmap", "2", "f2").Err()if err != nil {fmt.Printf("try hset map[tmap] field[2] error[%s]\n",err.Error())err_handler(err)}kv_map := make(map[string]interface{})kv_map["3"] = "f3"kv_map["4"] = "f4"err = client.HMSet("tmap", kv_map).Err()if err != nil {fmt.Printf("try mset map[tmap] field[3] field[4] error[%s]\n",err.Error())err_handler(err)}map_len, err := client.HLen("tmap").Result()if err != nil {fmt.Printf("try get len of map[tmap] error[%s]\n", err.Error())err_handler(err)}fmt.Printf("len of map[tmap] is %d\n", map_len)// get map valuevalue, err = client.HGet("tmap", "1").Result()if err != nil {fmt.Printf("try get field[1] value of map[tmap] error[%s]\n",err.Error())err_handler(err)}fmt.Printf("field[1] value of map[tmap] is %s\n", value)// hgetallresult_kv, err := client.HGetAll("tmap").Result()if err != nil {fmt.Printf("try hgetall map[tmap] error[%s]\n", err.Error())err_handler(err)}for k, v := range result_kv {fmt.Printf("map[tmap] %s = %s\n", k, v)}fmt.Printf("----------------------------------------\n")fmt.Printf("pubsub test\n")pubsub := client.Subscribe("test_channel")_, err = pubsub.Receive()if err != nil {fmt.Printf("try subscribe channel[test_channel] error[%s]\n",err.Error())err_handler(err)}// go channel to used to receive messagech := pubsub.Channel()// publish a messageerr = client.Publish("test_channel", "hello").Err()if err != nil {fmt.Printf("try publish message to channel[test_channel] error[%s]\n",err.Error())err_handler(err)}time.AfterFunc(time.Second*2, func() {_ = pubsub.Close()})// consume messagefor {msg, ok := <-chif !ok {break}fmt.Printf("recv message[%s] from channel[%s]\n",msg.Payload, msg.Channel)} }func main() {fmt.Println("redis_test!")standalon_redis_test()}總結(jié)
以上是生活随笔為你收集整理的Linux 系统中 Redis 的安装及其使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于特征的文法分析以及概述自然语言处理
- 下一篇: 为什么那么多人转型做大数据