C# Redis之ServiceStack


前面幾篇博客基本把redis基本操作學習了下,但一些高級應用並沒有寫進博客,例如持久化、虛擬內存等,像這些主要是通過配置文件來解決的,運維方向可能更側重一些,對於開發者來說,可能就想知道怎么用C#來和Redis服務器打交道,今天使用的ServiceStack就是用來做這事的。

一、引入ServiceStack

  通過NuGET搜索ServiceStack,安裝之后會有4個dll,如下圖

 

二、啟動Redis服務

這里按照上一篇博客主從復制的結果搭建Redis服務器。6379的是主服務器,6380的是從服務器。圖我就不截了,上篇博客中已經有了。

三、封裝幫助類

關於ServiceStack的幫助類也挺多的,我在這博客貼出來的類也是從網上搜的,只是在它的基礎上進行了下修改,比如配置RedisConfig.cs文件,我這里直接返回一個定值,這主要是測試,如果在開發中,應該寫在配置文件中。這里我新建了一個RedisHelper的文件夾來存放這些幫助類。

1.配置文件 主要配置服務器的一些參數 讀寫服務器地址 最大的讀寫數量等

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace RedisHelper
{
    public sealed class RedisConfig : ConfigurationSection
    {
       
        public static string WriteServerConStr{
            get {
                return string.Format("{0},{1}","127.0.0.1:6379","127.0.0.1:6380");
            }
        }
        public static string ReadServerConStr
        {
            get
            {
                return  string.Format("{0}", "127.0.0.1:6379");
            }
        }
        public static int MaxWritePoolSize
        {
            get
            {
                return 50;
            }
        }
        public static int MaxReadPoolSize
        {
            get
            {
                return 200;
            }
        }
        public static bool AutoStart
        {
            get
            {
                return true;
            }
        }

    }
}
View Code

2.RedisManager管理類 主要管理維護服務端訪問類

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisHelper
{
    public class RedisManager
    {
        private static PooledRedisClientManager prcm;

        /// <summary>
        /// 靜態構造方法,初始化鏈接池管理對象
        /// </summary>
        static RedisManager()
        {
            CreateManager();
        }

        /// <summary>
        /// 創建鏈接池管理對象
        /// </summary>
        private static void CreateManager()
        {
            string[] WriteServerConStr = SplitString(RedisConfig.WriteServerConStr, ",");
            string[] ReadServerConStr = SplitString(RedisConfig.ReadServerConStr, ",");
            prcm = new PooledRedisClientManager(ReadServerConStr, WriteServerConStr,
                             new RedisClientManagerConfig
                             {
                                 MaxWritePoolSize = RedisConfig.MaxWritePoolSize,
                                 MaxReadPoolSize = RedisConfig.MaxReadPoolSize,
                                 AutoStart = RedisConfig.AutoStart,
                             });
        }

        private static string[] SplitString(string strSource, string split)
        {
            return strSource.Split(split.ToArray());
        }
        /// <summary>
        /// 客戶端緩存操作對象
        /// </summary>
        public static IRedisClient GetClient()
        {
            if (prcm == null)
                CreateManager();
            return prcm.GetClient();
        }
    }
}
View Code

3.RedisBase類 字符串、List等操作類的基類

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisHelper
{
    /// <summary>
    /// RedisBase類,是redis操作的基類,繼承自IDisposable接口,主要用於釋放內存
    /// </summary>
    public abstract class RedisBase : IDisposable
    {
        public static IRedisClient Core { get; private set; }
        private bool _disposed = false;
        static RedisBase()
        {
            Core = RedisManager.GetClient();
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    Core.Dispose();
                    Core = null;
                }
            }
            this._disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 保存數據DB文件到硬盤
        /// </summary>
        public void Save()
        {
            Core.Save();
        }
        /// <summary>
        /// 異步保存數據DB文件到硬盤
        /// </summary>
        public void SaveAsync()
        {
            Core.SaveAsync();
        }
    }
}
View Code

4.字符串、List、Hash等操作類

(1)string操作類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisHelper
{
    public class RedisString : RedisBase
    {
        #region 賦值
        /// <summary>
        /// 設置key的value
        /// </summary>
        public  static bool Set(string key, string value)
        {
            return RedisBase.Core.Set<string>(key, value);
        }
        /// <summary>
        /// 設置key的value並設置過期時間
        /// </summary>
        public static bool Set(string key, string value, DateTime dt)
        {
            return RedisBase.Core.Set<string>(key, value, dt);
        }
        /// <summary>
        /// 設置key的value並設置過期時間
        /// </summary>
        public static bool Set(string key, string value, TimeSpan sp)
        {
            return RedisBase.Core.Set<string>(key, value, sp);
        }
        /// <summary>
        /// 設置多個key/value
        /// </summary>
        public static void Set(Dictionary<string, string> dic)
        {
            RedisBase.Core.SetAll(dic);
        }

        #endregion
        #region 追加
        /// <summary>
        /// 在原有key的value值之后追加value
        /// </summary>
        public static long Append(string key, string value)
        {
            return RedisBase.Core.AppendToValue(key, value);
        }
        #endregion
        #region 獲取值
        /// <summary>
        /// 獲取key的value值
        /// </summary>
        public static string Get(string key)
        {
            return  RedisBase.Core.GetValue(key);
        }
        /// <summary>
        /// 獲取多個key的value值
        /// </summary>
        public static List<string> Get(List<string> keys)
        {
            return RedisBase.Core.GetValues(keys);
        }
        /// <summary>
        /// 獲取多個key的value值
        /// </summary>
        public static List<T> Get<T>(List<string> keys)
        {
            return RedisBase.Core.GetValues<T>(keys);
        }
        #endregion
        #region 獲取舊值賦上新值
        /// <summary>
        /// 獲取舊值賦上新值
        /// </summary>
        public string GetAndSetValue(string key, string value)
        {
            return RedisBase.Core.GetAndSetValue(key, value);
        }
        #endregion
        #region 輔助方法
        /// <summary>
        /// 獲取值的長度
        /// </summary>
        public static long GetCount(string key)
        {
            return RedisBase.Core.GetStringCount(key);
        }
        /// <summary>
        /// 自增1,返回自增后的值
        /// </summary>
        public static long Incr(string key)
        {
            return RedisBase.Core.IncrementValue(key);
        }
        /// <summary>
        /// 自增count,返回自增后的值
        /// </summary>
        public static double IncrBy(string key, double count)
        {
            return RedisBase.Core.IncrementValueBy(key, count);
        }
        /// <summary>
        /// 自減1,返回自減后的值
        /// </summary>
        public static long Decr(string key)
        {
            return RedisBase.Core.DecrementValue(key);
        }
        /// <summary>
        /// 自減count ,返回自減后的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static long DecrBy(string key, int count)
        {
            return RedisBase.Core.DecrementValueBy(key, count);
        }
        #endregion
    }
}
View Code

(2)List操作類

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisHelper
{
    public class RedisList : RedisBase
    {
        #region 賦值
        /// <summary>
        /// 從左側向list中添加值
        /// </summary>
        public static  void LPush(string key, string value)
        {
            RedisBase.Core.PushItemToList(key, value);
        }
        /// <summary>
        /// 從左側向list中添加值,並設置過期時間
        /// </summary>
        public static void LPush(string key, string value, DateTime dt)
        {
            RedisBase.Core.PushItemToList(key, value);
            RedisBase.Core.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 從左側向list中添加值,設置過期時間
        /// </summary>
        public static void LPush(string key, string value, TimeSpan sp)
        {
            RedisBase.Core.PushItemToList(key, value);
            RedisBase.Core.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 從左側向list中添加值
        /// </summary>
        public static void RPush(string key, string value)
        {
            RedisBase.Core.PrependItemToList(key, value);
        }
        /// <summary>
        /// 從右側向list中添加值,並設置過期時間
        /// </summary>    
        public static void RPush(string key, string value, DateTime dt)
        {
            RedisBase.Core.PrependItemToList(key, value);
            RedisBase.Core.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 從右側向list中添加值,並設置過期時間
        /// </summary>        
        public static void RPush(string key, string value, TimeSpan sp)
        {
            RedisBase.Core.PrependItemToList(key, value);
            RedisBase.Core.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 添加key/value
        /// </summary>     
        public static void Add(string key, string value)
        {
            RedisBase.Core.AddItemToList(key, value);
        }
        /// <summary>
        /// 添加key/value ,並設置過期時間
        /// </summary>  
        public static void Add(string key, string value, DateTime dt)
        {
            RedisBase.Core.AddItemToList(key, value);
            RedisBase.Core.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 添加key/value。並添加過期時間
        /// </summary>  
        public static void Add(string key, string value, TimeSpan sp)
        {
            RedisBase.Core.AddItemToList(key, value);
            RedisBase.Core.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 為key添加多個值
        /// </summary>  
        public static void Add(string key, List<string> values)
        {
            RedisBase.Core.AddRangeToList(key, values);
        }
        /// <summary>
        /// 為key添加多個值,並設置過期時間
        /// </summary>  
        public static void Add(string key, List<string> values, DateTime dt)
        {
            RedisBase.Core.AddRangeToList(key, values);
            RedisBase.Core.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 為key添加多個值,並設置過期時間
        /// </summary>  
        public static void Add(string key, List<string> values, TimeSpan sp)
        {
            RedisBase.Core.AddRangeToList(key, values);
            RedisBase.Core.ExpireEntryIn(key, sp);
        }
        #endregion
        #region 獲取值
        /// <summary>
        /// 獲取list中key包含的數據數量
        /// </summary>  
        public static long Count(string key)
        {
            return RedisBase.Core.GetListCount(key);
        }
        /// <summary>
        /// 獲取key包含的所有數據集合
        /// </summary>  
        public static List<string> Get(string key)
        {
            return RedisBase.Core.GetAllItemsFromList(key);
        }
        /// <summary>
        /// 獲取key中下標為star到end的值集合
        /// </summary>  
        public static List<string> Get(string key, int star, int end)
        {
            return RedisBase.Core.GetRangeFromList(key, star, end);
        }
        #endregion
        #region 阻塞命令
        /// <summary>
        ///  阻塞命令:從list中keys的尾部移除一個值,並返回移除的值,阻塞時間為sp
        /// </summary>  
        public static string BlockingPopItemFromList(string key, TimeSpan? sp)
        {
            return RedisBase.Core.BlockingDequeueItemFromList(key, sp);
        }
        /// <summary>
        ///  阻塞命令:從list中keys的尾部移除一個值,並返回移除的值,阻塞時間為sp
        /// </summary>  
        public static ItemRef BlockingPopItemFromLists(string[] keys, TimeSpan? sp)
        {
            return RedisBase.Core.BlockingPopItemFromLists(keys, sp);
        }
        /// <summary>
        ///  阻塞命令:從list中keys的尾部移除一個值,並返回移除的值,阻塞時間為sp
        /// </summary>  
        public static string BlockingDequeueItemFromList(string key, TimeSpan? sp)
        {
            return RedisBase.Core.BlockingDequeueItemFromList(key, sp);
        }
        /// <summary>
        /// 阻塞命令:從list中keys的尾部移除一個值,並返回移除的值,阻塞時間為sp
        /// </summary>  
        public static ItemRef BlockingDequeueItemFromLists(string[] keys, TimeSpan? sp)
        {
            return RedisBase.Core.BlockingDequeueItemFromLists(keys, sp);
        }
        /// <summary>
        /// 阻塞命令:從list中key的頭部移除一個值,並返回移除的值,阻塞時間為sp
        /// </summary>  
        public static string BlockingRemoveStartFromList(string keys, TimeSpan? sp)
        {
            return RedisBase.Core.BlockingRemoveStartFromList(keys, sp);
        }
        /// <summary>
        /// 阻塞命令:從list中key的頭部移除一個值,並返回移除的值,阻塞時間為sp
        /// </summary>  
        public static ItemRef BlockingRemoveStartFromLists(string[] keys, TimeSpan? sp)
        {
            return RedisBase.Core.BlockingRemoveStartFromLists(keys, sp);
        }
        /// <summary>
        /// 阻塞命令:從list中一個fromkey的尾部移除一個值,添加到另外一個tokey的頭部,並返回移除的值,阻塞時間為sp
        /// </summary>  
        public static string BlockingPopAndPushItemBetweenLists(string fromkey, string tokey, TimeSpan? sp)
        {
            return RedisBase.Core.BlockingPopAndPushItemBetweenLists(fromkey, tokey, sp);
        }
        #endregion
        #region 刪除
        /// <summary>
        /// 從尾部移除數據,返回移除的數據
        /// </summary>  
        public static string PopItemFromList(string key)
        {
            return RedisBase.Core.PopItemFromList(key);
        }
        /// <summary>
        /// 移除list中,key/value,與參數相同的值,並返回移除的數量
        /// </summary>  
        public static long RemoveItemFromList(string key, string value)
        {
            return RedisBase.Core.RemoveItemFromList(key, value);
        }
        /// <summary>
        /// 從list的尾部移除一個數據,返回移除的數據
        /// </summary>  
        public static string RemoveEndFromList(string key)
        {
            return RedisBase.Core.RemoveEndFromList(key);
        }
        /// <summary>
        /// 從list的頭部移除一個數據,返回移除的值
        /// </summary>  
        public static string RemoveStartFromList(string key)
        {
            return RedisBase.Core.RemoveStartFromList(key);
        }
        #endregion
        #region 其它
        /// <summary>
        /// 從一個list的尾部移除一個數據,添加到另外一個list的頭部,並返回移動的值
        /// </summary>  
        public static string PopAndPushItemBetweenLists(string fromKey, string toKey)
        {
            return RedisBase.Core.PopAndPushItemBetweenLists(fromKey, toKey);
        }
        #endregion
    }
}
View Code

(3)Hash操作類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisHelper
{
    public class RedisHash : RedisBase
    {
        #region 添加
        /// <summary>
        /// 向hashid集合中添加key/value
        /// </summary>       
        public static bool SetEntryInHash(string hashid, string key, string value)
        {
            return RedisBase.Core.SetEntryInHash(hashid, key, value);
        }
        /// <summary>
        /// 如果hashid集合中存在key/value則不添加返回false,如果不存在在添加key/value,返回true
        /// </summary>
        public static bool SetEntryInHashIfNotExists(string hashid, string key, string value)
        {
            return RedisBase.Core.SetEntryInHashIfNotExists(hashid, key, value);
        }
        /// <summary>
        /// 存儲對象T t到hash集合中
        /// </summary>
        public static void StoreAsHash<T>(T t)
        {
            RedisBase.Core.StoreAsHash<T>(t);
        }
        #endregion
        #region 獲取
        /// <summary>
        /// 獲取對象T中ID為id的數據。
        /// </summary>
        public static T GetFromHash<T>(object id)
        {
            return RedisBase.Core.GetFromHash<T>(id);
        }
        /// <summary>
        /// 獲取所有hashid數據集的key/value數據集合
        /// </summary>
        public static Dictionary<string, string> GetAllEntriesFromHash(string hashid)
        {
            return RedisBase.Core.GetAllEntriesFromHash(hashid);
        }
        /// <summary>
        /// 獲取hashid數據集中的數據總數
        /// </summary>
        public static long GetHashCount(string hashid)
        {
            return RedisBase.Core.GetHashCount(hashid);
        }
        /// <summary>
        /// 獲取hashid數據集中所有key的集合
        /// </summary>
        public static List<string> GetHashKeys(string hashid)
        {
            return RedisBase.Core.GetHashKeys(hashid);
        }
        /// <summary>
        /// 獲取hashid數據集中的所有value集合
        /// </summary>
        public static List<string> GetHashValues(string hashid)
        {
            return RedisBase.Core.GetHashValues(hashid);
        }
        /// <summary>
        /// 獲取hashid數據集中,key的value數據
        /// </summary>
        public static string GetValueFromHash(string hashid, string key)
        {
            return RedisBase.Core.GetValueFromHash(hashid, key);
        }
        /// <summary>
        /// 獲取hashid數據集中,多個keys的value集合
        /// </summary>
        public static List<string> GetValuesFromHash(string hashid, string[] keys)
        {
            return RedisBase.Core.GetValuesFromHash(hashid, keys);
        }
        #endregion
        #region 刪除
        #endregion
        /// <summary>
        /// 刪除hashid數據集中的key數據
        /// </summary>
        public static bool RemoveEntryFromHash(string hashid, string key)
        {
            return RedisBase.Core.RemoveEntryFromHash(hashid, key);
        }
        #region 其它
        /// <summary>
        /// 判斷hashid數據集中是否存在key的數據
        /// </summary>
        public static bool HashContainsEntry(string hashid, string key)
        {
            return RedisBase.Core.HashContainsEntry(hashid, key);
        }
        /// <summary>
        /// 給hashid數據集key的value加countby,返回相加后的數據
        /// </summary>
        public static double IncrementValueInHash(string hashid, string key, double countBy)
        {
            return RedisBase.Core.IncrementValueInHash(hashid, key, countBy);
        }
        #endregion
    }
}
View Code

(4)Set操作類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisHelper
{
    public class RedisSet : RedisBase
    {
        #region 添加
        /// <summary>
        /// key集合中添加value值
        /// </summary>
        public static void Add(string key, string value)
        {
            RedisBase.Core.AddItemToSet(key, value);
        }
        /// <summary>
        /// key集合中添加list集合
        /// </summary>
        public static void Add(string key, List<string> list)
        {
            RedisBase.Core.AddRangeToSet(key, list);
        }
        #endregion
        #region 獲取
        /// <summary>
        /// 隨機獲取key集合中的一個值
        /// </summary>
        public static string GetRandomItemFromSet(string key)
        {
            return RedisBase.Core.GetRandomItemFromSet(key);
        }
        /// <summary>
        /// 獲取key集合值的數量
        /// </summary>
        public static long GetCount(string key)
        {
            return RedisBase.Core.GetSetCount(key);
        }
        /// <summary>
        /// 獲取所有key集合的值
        /// </summary>
        public static HashSet<string> GetAllItemsFromSet(string key)
        {
            return RedisBase.Core.GetAllItemsFromSet(key);
        }
        #endregion
        #region 刪除
        /// <summary>
        /// 隨機刪除key集合中的一個值
        /// </summary>
        public static string PopItemFromSet(string key)
        {
            return RedisBase.Core.PopItemFromSet(key);
        }
        /// <summary>
        /// 刪除key集合中的value
        /// </summary>
        public static void RemoveItemFromSet(string key, string value)
        {
            RedisBase.Core.RemoveItemFromSet(key, value);
        }
        #endregion
        #region 其它
        /// <summary>
        /// 從fromkey集合中移除值為value的值,並把value添加到tokey集合中
        /// </summary>
        public static void MoveBetweenSets(string fromkey, string tokey, string value)
        {
            RedisBase.Core.MoveBetweenSets(fromkey, tokey, value);
        }
        /// <summary>
        /// 返回keys多個集合中的並集,返還hashset
        /// </summary>
        public static HashSet<string> GetUnionFromSets(string[] keys)
        {
            return RedisBase.Core.GetUnionFromSets(keys);
        }
        /// <summary>
        /// keys多個集合中的並集,放入newkey集合中
        /// </summary>
        public static void StoreUnionFromSets(string newkey, string[] keys)
        {
            RedisBase.Core.StoreUnionFromSets(newkey, keys);
        }
        /// <summary>
        /// 把fromkey集合中的數據與keys集合中的數據對比,fromkey集合中不存在keys集合中,則把這些不存在的數據放入newkey集合中
        /// </summary>
        public static void StoreDifferencesFromSet(string newkey, string fromkey, string[] keys)
        {
            RedisBase.Core.StoreDifferencesFromSet(newkey, fromkey, keys);
        }
        #endregion
    }
}
View Code

(5)ZSet操作類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisHelper
{
    public class RedisZSet : RedisBase
    {
        #region 添加
        /// <summary>
        /// 添加key/value,默認分數是從1.多*10的9次方以此遞增的,自帶自增效果
        /// </summary>
        public static bool AddItemToSortedSet(string key, string value)
        {
            return RedisBase.Core.AddItemToSortedSet(key, value);
        }
        /// <summary>
        /// 添加key/value,並設置value的分數
        /// </summary>
        public static bool AddItemToSortedSet(string key, string value, double score)
        {
            return RedisBase.Core.AddItemToSortedSet(key, value, score);
        }
        /// <summary>
        /// 為key添加values集合,values集合中每個value的分數設置為score
        /// </summary>
        public static bool AddRangeToSortedSet(string key, List<string> values, double score)
        {
            return RedisBase.Core.AddRangeToSortedSet(key, values, score);
        }
        /// <summary>
        /// 為key添加values集合,values集合中每個value的分數設置為score
        /// </summary>
        public static bool AddRangeToSortedSet(string key, List<string> values, long score)
        {
            return RedisBase.Core.AddRangeToSortedSet(key, values, score);
        }
        #endregion
        #region 獲取
        /// <summary>
        /// 獲取key的所有集合
        /// </summary>
        public static List<string> GetAllItemsFromSortedSet(string key)
        {
            return RedisBase.Core.GetAllItemsFromSortedSet(key);
        }
        /// <summary>
        /// 獲取key的所有集合,倒敘輸出
        /// </summary>
        public static List<string> GetAllItemsFromSortedSetDesc(string key)
        {
            return RedisBase.Core.GetAllItemsFromSortedSetDesc(key);
        }
        /// <summary>
        /// 獲取可以的說有集合,帶分數
        /// </summary>
        public static IDictionary<string, double> GetAllWithScoresFromSortedSet(string key)
        {
            return RedisBase.Core.GetAllWithScoresFromSortedSet(key);
        }
        /// <summary>
        /// 獲取key為value的下標值
        /// </summary>
        public static long GetItemIndexInSortedSet(string key, string value)
        {
            return RedisBase.Core.GetItemIndexInSortedSet(key, value);
        }
        /// <summary>
        /// 倒敘排列獲取key為value的下標值
        /// </summary>
        public static long GetItemIndexInSortedSetDesc(string key, string value)
        {
            return RedisBase.Core.GetItemIndexInSortedSetDesc(key, value);
        }
        /// <summary>
        /// 獲取key為value的分數
        /// </summary>
        public static double GetItemScoreInSortedSet(string key, string value)
        {
            return RedisBase.Core.GetItemScoreInSortedSet(key, value);
        }
        /// <summary>
        /// 獲取key所有集合的數據總數
        /// </summary>
        public static long GetSortedSetCount(string key)
        {
            return RedisBase.Core.GetSortedSetCount(key);
        }
        /// <summary>
        /// key集合數據從分數為fromscore到分數為toscore的數據總數
        /// </summary>
        public static long GetSortedSetCount(string key, double fromScore, double toScore)
        {
            return RedisBase.Core.GetSortedSetCount(key, fromScore, toScore);
        }
        /// <summary>
        /// 獲取key集合從高分到低分排序數據,分數從fromscore到分數為toscore的數據
        /// </summary>
        public static List<string> GetRangeFromSortedSetByHighestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeFromSortedSetByHighestScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 獲取key集合從低分到高分排序數據,分數從fromscore到分數為toscore的數據
        /// </summary>
        public static List<string> GetRangeFromSortedSetByLowestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeFromSortedSetByLowestScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 獲取key集合從高分到低分排序數據,分數從fromscore到分數為toscore的數據,帶分數
        /// </summary>
        public static IDictionary<string, double> GetRangeWithScoresFromSortedSetByHighestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSetByHighestScore(key, fromscore, toscore);
        }
        /// <summary>
        ///  獲取key集合從低分到高分排序數據,分數從fromscore到分數為toscore的數據,帶分數
        /// </summary>
        public static IDictionary<string, double> GetRangeWithScoresFromSortedSetByLowestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSetByLowestScore(key, fromscore, toscore);
        }
        /// <summary>
        ///  獲取key集合數據,下標從fromRank到分數為toRank的數據
        /// </summary>
        public static List<string> GetRangeFromSortedSet(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeFromSortedSet(key, fromRank, toRank);
        }
        /// <summary>
        /// 獲取key集合倒敘排列數據,下標從fromRank到分數為toRank的數據
        /// </summary>
        public static List<string> GetRangeFromSortedSetDesc(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeFromSortedSetDesc(key, fromRank, toRank);
        }
        /// <summary>
        /// 獲取key集合數據,下標從fromRank到分數為toRank的數據,帶分數
        /// </summary>
        public static IDictionary<string, double> GetRangeWithScoresFromSortedSet(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSet(key, fromRank, toRank);
        }
        /// <summary>
        ///  獲取key集合倒敘排列數據,下標從fromRank到分數為toRank的數據,帶分數
        /// </summary>
        public static IDictionary<string, double> GetRangeWithScoresFromSortedSetDesc(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSetDesc(key, fromRank, toRank);
        }
        #endregion
        #region 刪除
        /// <summary>
        /// 刪除key為value的數據
        /// </summary>
        public static bool RemoveItemFromSortedSet(string key, string value)
        {
            return RedisBase.Core.RemoveItemFromSortedSet(key, value);
        }
        /// <summary>
        /// 刪除下標從minRank到maxRank的key集合數據
        /// </summary>
        public static long RemoveRangeFromSortedSet(string key, int minRank, int maxRank)
        {
            return RedisBase.Core.RemoveRangeFromSortedSet(key, minRank, maxRank);
        }
        /// <summary>
        /// 刪除分數從fromscore到toscore的key集合數據
        /// </summary>
        public static long RemoveRangeFromSortedSetByScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.RemoveRangeFromSortedSetByScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 刪除key集合中分數最大的數據
        /// </summary>
        public static string PopItemWithHighestScoreFromSortedSet(string key)
        {
            return RedisBase.Core.PopItemWithHighestScoreFromSortedSet(key);
        }
        /// <summary>
        /// 刪除key集合中分數最小的數據
        /// </summary>
        public static string PopItemWithLowestScoreFromSortedSet(string key)
        {
            return RedisBase.Core.PopItemWithLowestScoreFromSortedSet(key);
        }
        #endregion
        #region 其它
        /// <summary>
        /// 判斷key集合中是否存在value數據
        /// </summary>
        public static bool SortedSetContainsItem(string key, string value)
        {
            return RedisBase.Core.SortedSetContainsItem(key, value);
        }
        /// <summary>
        /// 為key集合值為value的數據,分數加scoreby,返回相加后的分數
        /// </summary>
        public static double IncrementItemInSortedSet(string key, string value, double scoreBy)
        {
            return RedisBase.Core.IncrementItemInSortedSet(key, value, scoreBy);
        }
        /// <summary>
        /// 獲取keys多個集合的交集,並把交集添加的newkey集合中,返回交集數據的總數
        /// </summary>
        public static long StoreIntersectFromSortedSets(string newkey, string[] keys)
        {
            return RedisBase.Core.StoreIntersectFromSortedSets(newkey, keys);
        }
        /// <summary>
        /// 獲取keys多個集合的並集,並把並集數據添加到newkey集合中,返回並集數據的總數
        /// </summary>
        public static long StoreUnionFromSortedSets(string newkey, string[] keys)
        {
            return RedisBase.Core.StoreUnionFromSortedSets(newkey, keys);
        }
        #endregion
    }
}
View Code

四、操作類使用測試

    class Program
    {
        static void Main(string[] args)
        {
            string key = "Users";
            RedisBase.Core.FlushAll();
            RedisBase.Core.AddItemToList(key, "cuiyanwei");
            RedisBase.Core.AddItemToList(key, "xiaoming");
            RedisBase.Core.Add<string>("mykey", "123456");
            RedisString.Set("mykey1","abcdef");
            Console.ReadLine();

        }
    }

上面先清楚所有的數據,然后在key=Users的list中增加兩個Value,又增加了兩個string類型的數據,分別為mykey、mykey1.

分別啟動6379、6380的Redis客戶端,查詢上面添加的幾個數據。

五、番外篇

今天朋友問我最近怎么很少在微信群里冒泡了,說不清楚怎么了,越來越不喜歡在群里吹吹水,侃侃大山,沒勁,平時特別無聊的話聊聊還好,想着自己也老大不小了,應該成熟起來,做點有意義的事情了,上個月買了3本書,也希望在接下來的日子能認真的堅持讀下去。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM