C#操作redis

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.