1. 程式人生 > >C#操作redis程式碼彙總

C#操作redis程式碼彙總

一 Redis是一種支援多種資料結構的鍵值對資料庫

1.1Redis下載地址 :https://github.com/MicrosoftArchive/Redis

建議下載 .msi結尾的應用程式進行安裝,會自動安裝Redis服務

Redis預設是不能外網訪問的

把Redis部署到本地請忽視下面

設定防火牆埠例外

更改redis.conf 檔案

bind 127.0.0.1

protected-mode yes

更改為

# bind 127.0.0.1

protected-mode no
然後重啟Redis服務,

1.2 Redis支援的資料型別:string、list、set、sortedset、geo(Redis 3.2以上版本),注意不同方法寫入的值不能用混了,比如有寫list的方法寫入的值用獲取字串的方法去獲取就有問題了。

1.3 Redis的優點:

支援多種複雜型別的資料結構

高命中的資料是執行在記憶體中的,資料最終還是可以儲存到硬碟中,伺服器重啟後資料不會丟失

伺服器是單執行緒的,來自所有客戶端的所有命令都是序列執行的,不用擔心併發修改的問題

支援訊息訂閱/通知機制,可以用作訊息佇列

key/value 最大長度允許512M

1.4 Redis的缺點:

Redis是單執行緒的,因此單個Redis的例項只能使用伺服器的一個CPU核,不能充分發揮伺服器的效能

 

二 在 .Net中操作Redis

2.1 在 .net中主要使用兩個開源的元件來操作Redis

1. StackExChange.Redis:依賴的元件少,操作接近原生的Redis操作

2. ServiceStack.Redis:依賴的元件較多,封裝的程度較高

NuGet命令安裝元件 Install-Package StackExChange.Redis

 

using System;
using System.Collections.Generic;
using ServiceStack.Redis;

namespace SysBuild
{
    class Program
    {
        //linux伺服器地址
        static private string host = "182.180.50.168";
        //static private string host = "127.0.0.1";
        static private int port = 6379;
        static RedisClient redisClient = new RedisClient(host, port);//redis服務IP和埠
        static void Main(string[] args)
        {
            //建立一個鍵a,指定值
            redisClient.Set<string>("a", "1");
            //獲取鍵a對應的值
            var a1 = redisClient.Get<string>("a");
            //刪除鍵a
            redisClient.Remove("a");
            //建立一個鍵a,指定值,並指定10s有效
            redisClient.Set<string>("b", "1", new TimeSpan(0, 0, 60));

            //佇列操作之入隊
            redisClient.EnqueueItemOnList("list", "haha");
            redisClient.EnqueueItemOnList("list", "haha111111111");
            redisClient.EnqueueItemOnList("list", "haha22");
            redisClient.EnqueueItemOnList("list", "hahset33333333333333333");
            //讀取佇列深度
            var a = redisClient.GetListCount("list");
            //佇列操作之出隊
            for (int i = 0; i < a; i++)
            {
                Console.WriteLine(redisClient.DequeueItemFromList("list"));
            }


            redisClient.SetEntryInHash("Hash", "Name", "wujf");
            redisClient.SetEntryInHash("Hash", "Age", "99");
            redisClient.SetEntryInHash("Hash", "Sex", "男");
            redisClient.SetEntryInHash("Hash", "Address", "上海市XX號XX室");
            //集合類指定超時的方法
            redisClient.ExpireEntryIn("Hash", new TimeSpan(0, 0, 100));
            var dic = redisClient.GetAllEntriesFromHash("Hash");
            Console.WriteLine("Key-----Value");
            foreach (var keyVal in dic)
            {
                Console.WriteLine(string.Format("{0}-----{1}", keyVal.Key, keyVal.Value));
            }
            List<string> haskKey = redisClient.GetHashKeys("Hash");
            List<string> haskVal = redisClient.GetHashValues("Hash");
            foreach (string key in haskKey)
            {
                Console.WriteLine("HashID--Key:{0}", key);
            }
            foreach (string val in haskVal)
            {
                Console.WriteLine("HashID--val:{0}", val);
            }

            //棧使用,先進後出
            redisClient.PushItemToList("stack", "1");
            redisClient.PushItemToList("stack", "2");
            redisClient.PushItemToList("stack", "3");
            redisClient.PushItemToList("stack", "4");
            int count = redisClient.GetListCount("stack");
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(redisClient.PopItemFromList("stack"));
            }

            //對Set型別進行操作  
            redisClient.AddItemToSet("set", "ddd");
            redisClient.AddItemToSet("set", "ccc");
            redisClient.AddItemToSet("set", "tttt");
            redisClient.AddItemToSet("set", "sssh");
            redisClient.AddItemToSet("set", "hhhh");
            HashSet<string> hashset = redisClient.GetAllItemsFromSet("set");
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }

            //求並集              
            redisClient.AddItemToSet("set1", "aa");
            redisClient.AddItemToSet("set1", "bb");
            redisClient.AddItemToSet("set2", "bb");
            redisClient.AddItemToSet("set2", "cc");
            hashset = redisClient.GetUnionFromSets(new string[] { "set1", "set2" });
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }
            //求交集  
            hashset = redisClient.GetIntersectFromSets(new string[] { "set1", "set2" });
            //求差集.  
            hashset = redisClient.GetDifferencesFromSet("set1", new string[] { "set2" });

            //Sorted Set型別排序
            redisClient.AddItemToSortedSet("sortList", "1");
            redisClient.AddItemToSortedSet("sortList", "9");
            redisClient.AddItemToSortedSet("sortList", "3");
            redisClient.AddItemToSortedSet("sortList", "8");
            List<string> sortList = redisClient.GetAllItemsFromSortedSet("sortList");
            foreach (string str in sortList)
            {
                Console.WriteLine(str);
            }


            Console.ReadKey();
        }
    }
}
複製程式碼

另:執行過程推薦使用Redis客戶端看看資料變化(ps:我用的redisclient)

推薦api說明

http://www.cnblogs.com/kissdodog/p/3572084.html