1、Redis概述
Redis是什么:
Redis(Remote Dictionary Server ),即遠程字典服務,是一個開源的使用ANSI C語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,并提供多種語言的API。
免費開源!最熱門的NoSQL技術之一!
redis會周期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎上實現了master-slave(主從)同步。讀的速度是110000次/s,寫的速度是81000次/s 。
Redis的作用:
- 內容存儲、持久化(rdb、aof);
- 效率高,可用于高速緩存;
- 發布訂閱系統;
- 地圖信息分析;
- 計時、計數器;
- …
Redis特性:
- Redis支持數據的持久化,可以將內存中的數據保存在磁盤中,重啟的時候可以再次加載進行使用。
- Redis不僅僅支持簡單的key-value類型的數據,同時還提供list,set,zset,hash等數據結構的存儲。
- Redis支持數據的備份,即master-slave模式的數據備份。
2、Redis安裝
2.1、windows下安裝
- 官網下載安裝包http://www.redis.cn/;
- 下載后解壓得到的壓縮包;
- 開啟Redis:雙擊exe文件,運行服務即可(默認端口號6379);
- 使用redis客戶端連接Redis,可以使用ping命令測試一下!
windows下使用很簡單,但是Redis推薦使用Redis來開發!
2.2、Linux下安裝
- 官網下載安裝包http://www.redis.cn/;
- 解壓Redis安裝包;
- 進入目錄,查看:
- 基本的環境安裝:
yum install gcc-c++
make
默認安裝路徑是/usr/local/下
6. redis不是默認后臺啟動的,需要求改一下配置文件:
daemonize后面的no改為yes。
7. 啟動redis服務
redis-server redis.conf
redis-cli -p 6379
- 基本命令測試連同
9.關閉redis服務
3、redis-benchmark 性能測試
Redis 自帶了一個叫 redis-benchmark 的工具來模擬 N 個客戶端同時發出 M 個請求。 (類似于 Apache ab 程序)。你可以使用 redis-benchmark -h 來查看基準參數。
以下參數被支持:Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]-h <hostname> Server hostname (default 127.0.0.1)-p <port> Server port (default 6379)-s <socket> Server socket (overrides host and port)-a <password> Password for Redis Auth-c <clients> Number of parallel connections (default 50)-n <requests> Total number of requests (default 100000)-d <size> Data size of SET/GET value in bytes (default 2)-dbnum <db> SELECT the specified db number (default 0)-k <boolean> 1=keep alive 0=reconnect (default 1)-r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADDUsing this option the benchmark will expand the string __rand_int__inside an argument with a 12 digits number in the specified rangefrom 0 to keyspacelen-1. The substitution changes every time a commandis executed. Default tests use this to hit random keys in thespecified range.-P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).-q Quiet. Just show query/sec values--csv Output in CSV format-l Loop. Run the tests forever-t <tests> Only run the comma separated list of tests. The testnames are the same as the ones produced as output.-I Idle mode. Just open N idle connections and wait.
我們可以簡單測試一下:
redis-benchmark -h localhost -p 6379 -c 100 -n 100000
可以看出:
100000并發、100個并發客戶端,每次寫如3字節,一臺服務器
所有請求在8毫秒內處理完成!!
每秒處理125156.45次請求!!!