【redis學習一】基本概念和基本操做

redis 基礎

官網地址 http://redis.io/ html

基本介紹:redis 是一個ansi c編寫,支持網絡的,基於內存的可持久化的日執行,kv數據庫;10年期redis有vmare主持開發。前端

支持數據類型:redis支持strings,hashes list set sorted等結構。redis

持久化:存儲於內存或虛擬內存中,有兩種持久化的方式:數據庫

  • :截圖,內存數據不斷寫入硬盤【性能較高】api

  • :log:記錄每次更新的日誌。【穩定性好】緩存

支持主從同步,性能很是優秀。
提供多種語言的api 基本上知道的都有。安全

使用場景:(我的覺的能夠有的使用場景)
1:權限【權限每次都要入庫校驗,放在前端不靠譜,放在cache最合適】
2:緩存【例如批量操做數據,能夠先緩存】
3:預取【例如topN數據,另外可能用到的數據,提早取出來,加快頁面加載】
4:構建消息隊列【能夠根據redis的數據結構list,構造;數據批量入庫,加快頁面相應等方面不錯】。
5:計數器【相似於批量入庫的原理,能夠計數,redis原子性的,能夠精確支持】
6:其餘場景還在不斷探索中。網絡

安裝和基本操做

安裝參考: http://www.runoob.com/redis/redis-install.html 數據結構

基本操做: 官方文檔地址 性能

啓動:./redis-server
關閉:redis-cli shutdown

[cuihuan bin]$ ./redis-cli shutdown
[cuihuan bin]$ ./redis-server    
[325] 24 Sep 18:49:06.632 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                _._                                                 
           _.-``__ ''-._                                            
      _.-``    `.  `_.  ''-._           Redis 2.6.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 325
  `-._    `-._  `-./  _.-'    _.-'                                  
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |           http://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                  
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                  
      `-._    `-.__.-'    _.-'                                      
          `-._        _.-'                                          
              `-.__.-'                                              

[325] 24 Sep 18:49:06.633 # Server started, Redis version 2.6.10
[325] 24 Sep 18:49:06.633 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[325] 24 Sep 18:49:06.673 * DB loaded from disk: 0.039 seconds
[325] 24 Sep 18:49:06.673 * The server is now ready to accept connections on port 6379

安裝成功的標誌 redis-cli 能夠成功進入

[cuihuan bin]$ ./redis-cli
redis 127.0.0.1:6379>

注意:使用過程當中若是是保密性高的數據,能夠設置登陸密碼,增長安全想;但若是是簡單數據,則能夠不設置,優勢就是速度稍快。

redis 基本配置:
daemonize: yes 標示在後臺運行
bind:綁定請求的地址
port:端口號,默認6379
timeout:默認客戶端鏈接超時 多長時間不操做關閉(默認永久,此處改成3600)

loglevel: log等級
databases:默認鏈接數據庫的個數 【此處爲8】
slaveof 主從庫

masterauth :密碼驗證
requirepass:是否須要密碼

maxclients :最大客戶機個數 設置爲10000
maxmemory:最大內存個數 6625156 [機器內存32G,分配大約6g]

最基本的操做
set name xxx
get name xxx
del name xxx
exists name xxx

舉個栗子

# get val
redis 127.0.0.1:6379> get name
"cuixiaohuan"

# set val 
redis 127.0.0.1:6379> set name cuixiaohuan_2 
OK
redis 127.0.0.1:6379> get name
"cuixiaohuan_2"

# check exists; if exists return 1
redis 127.0.0.1:6379> exists name
(integer) 1

# del val
redis 127.0.0.1:6379> del name
(integer) 1
redis 127.0.0.1:6379> get name
(nil)

其餘經常使用操做:

# ping :check connect
redis 127.0.0.1:6379> ping
PONG

# dbsize :check size
redis 127.0.0.1:6379> dbsize
(integer) 1

#flush :clear db
redis 127.0.0.1:6379> dbsize
(integer) 1
redis 127.0.0.1:6379> flushdb
OK
redis 127.0.0.1:6379> dbsize
(integer) 0

我的小站原文連接

相關文章
相關標籤/搜索