標籤:height void 持久 輸入 表徵圖 ++ 多個 code 分享圖片
Redis 是一個非關係型高效能的key-value資料庫。在部分場合可以對關聯式資料庫起到很好的補充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等用戶端,使用很方便。
redis提供五種資料類型:string,hash,list,set及zset(sorted set)。
好了,話不多說,先安裝redis吧。我這裡提供的版本是64位的3.2.1.00 https://files.cnblogs.com/files/wangjifeng23/Redis-x64-3.2.100.zip ,其餘版本可前往官網進行下載 http://download.redis.io/releases/ 。
下載好之後,建立檔案夾,將檔案解壓。
解壓完之後,開始進行redis安裝。
1.鍵入cmd
2.指向redis安裝路徑 f: --> cd redis
3.redis安裝指令 redis-server redis.windows.conf,出現以標即安裝成功
開啟redis用戶端工具(redis-cli.exe)
使用set get設定擷取值,如下所示即使用成功
好了,為了使用方便,我們可以把redis部署到服務上面自啟動,然後使用第三方用戶端軟體RedisDesktopManager(下載連結: https://pan.baidu.com/s/1DAWFwlZQK0AJphOQEHQaXA 密碼: jr5r)進行管理,讓開發更加便捷。
如上所示使用cmd鍵入命令: redis-server --service-install redis.windows.conf
開啟用戶端,建立串連,輸入localhost(本機服務),串連前確保redis服務已開啟,連接埠為6379(主伺服器)
如所示證明我們已經串連成功啦,左邊就是我儲存的4個索引值對資料。
好了,接下來我們要在代碼裡實現對他的儲存以及擷取。
使用NuGet安裝ServiceStack.Redis,這是微軟提供已經封裝好的對redis操作類。包含4個dll
串連redis伺服器,讀取以及儲存
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace redis{ public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient("127.0.0.1", 6379); public void login(object sender, EventArgs e) { //讀取 string name = client.Get<string>("name"); string pwd = client.Get<string>("password"); //儲存 client.Set<string>("name1", username.Value); client.Set<string>("password1", userpwd.Value); } }}
總結:
1 、Redis不僅僅支援簡單的k/v類型的資料,同時還提供list,set,zset,hash等資料結構的儲存。
2 、Redis支援資料的備份,即master-slave模式的資料備份。
3 、Redis支援資料的持久化,可以將記憶體中的資料保持在磁碟中,重啟的時候可以再次載入進行使用。
4、Redis可以實現主從複製,實現故障恢複。
5、Redis的Sharding技術: 很容易將資料分布到多個Redis執行個體中
轉載請註明出處
C#操作redis