Zoe

using System.Configuration;

namespace MyRedisDemo.Init
{
    /// <summary>
    /// redis配置文件信息
    /// </summary>
    public sealed class RedisConfigInfo
    {
        /// <summary>
        /// 可写的Redis链接地址
        /// format:ip1,ip2
        /// </summary>
        public string WriteServerList = "127.0.0.1:6379";
        /// <summary>
        /// 可读的Redis链接地址
        /// format:ip1,ip2
        /// </summary>
        public string ReadServerList = "127.0.0.1:6379";
        /// <summary>
        /// 最大写链接数
        /// </summary>
        public int MaxWritePoolSize = 60;
        /// <summary>
        /// 最大读链接数
        /// </summary>
        public int MaxReadPoolSize = 60;
        /// <summary>
        /// 本地缓存到期时间,单位:秒
        /// </summary>
        public int LocalCacheTime = 180;
        /// <summary>
        /// 自动重启
        /// </summary>
        public bool AutoStart = true;
        /// <summary>
        /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,
        /// 如redis工作正常,请关闭该项
        /// </summary>
        public bool RecordeLog = false;
    }
}
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo.Init
{
    public class RedisManager
    {
        /// <summary>
        /// redis配置文件信息
        /// </summary>
        private static RedisConfigInfo RedisConfigInfo = new RedisConfigInfo();

        private static PooledRedisClientManager prcManager;

        /// <summary>
        /// 静态构造方法,初始化链接池管理对象
        /// </summary>
        static RedisManager()
        {
            CreateManager();
        }

        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        private static void CreateManager()
        {
            string[] WriteServerConStr = RedisConfigInfo.WriteServerList.Split(',');
            string[] ReadServerConStr = RedisConfigInfo.ReadServerList.Split(',');
            prcManager = new PooledRedisClientManager(ReadServerConStr, WriteServerConStr,
                             new RedisClientManagerConfig
                             {
                                 MaxWritePoolSize = RedisConfigInfo.MaxWritePoolSize,
                                 MaxReadPoolSize = RedisConfigInfo.MaxReadPoolSize,
                                 AutoStart = RedisConfigInfo.AutoStart,
                             });
        }

        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public static IRedisClient GetClient()
        {
            return prcManager.GetClient();
        }
    }
}
using MyRedisDemo.Init;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo.Interface
{
    /// <summary>
    /// RedisBase类,是redis操作的基类,继承自IDisposable接口,主要用于释放内存
    /// </summary>
    public abstract class RedisBase : IDisposable
    {
        public static IRedisClient iClient { get; private set; }
        private bool _disposed = false;
        static RedisBase()
        {
            iClient = RedisManager.GetClient();
        }

        public virtual void FlushAll()
        {
            iClient.FlushAll();
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    iClient.Dispose();
                    iClient = null;
                }
            }
            this._disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 保存数据DB文件到硬盘
        /// </summary>
        public void Save()
        {
            iClient.Save();
        }
        /// <summary>
        /// 异步保存数据DB文件到硬盘
        /// </summary>
        public void SaveAsync()
        {
            iClient.SaveAsync();
        }
    }
}
using MyRedisDemo.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo.Service
{
    /// <summary>
    /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直接修改某个字段
    /// string的话要么是 001:序列化整个实体
    ///           要么是 001_name:  001_pwd: 多个key-value
    /// Hash的话,一个hashid-{key:value;key:value;key:value;}
    /// 可以一次性查找实体,也可以单个,还可以单个修改
    /// </summary>
    public class RedisHashService : RedisBase
    {
        #region 添加
        /// <summary>
        /// 向hashid集合中添加key/value
        /// </summary>       
        public bool SetEntryInHash(string hashid, string key, string value)
        {
            return RedisBase.iClient.SetEntryInHash(hashid, key, value);
        }
        /// <summary>
        /// 如果hashid集合中存在key/value则不添加返回false,
        /// 如果不存在在添加key/value,返回true
        /// </summary>
        public bool SetEntryInHashIfNotExists(string hashid, string key, string value)
        {
            return RedisBase.iClient.SetEntryInHashIfNotExists(hashid, key, value);
        }
        /// <summary>
        /// 存储对象T t到hash集合中
        /// </summary>
        public void StoreAsHash<T>(T t)
        {
            RedisBase.iClient.StoreAsHash<T>(t);
        }
        #endregion

        #region 获取
        /// <summary>
        /// 获取对象T中ID为id的数据。
        /// </summary>
        public T GetFromHash<T>(object id)
        {
            return RedisBase.iClient.GetFromHash<T>(id);
        }
        /// <summary>
        /// 获取所有hashid数据集的key/value数据集合
        /// </summary>
        public Dictionary<string, string> GetAllEntriesFromHash(string hashid)
        {
            return RedisBase.iClient.GetAllEntriesFromHash(hashid);
        }
        /// <summary>
        /// 获取hashid数据集中的数据总数
        /// </summary>
        public long GetHashCount(string hashid)
        {
            return RedisBase.iClient.GetHashCount(hashid);
        }
        /// <summary>
        /// 获取hashid数据集中所有key的集合
        /// </summary>
        public List<string> GetHashKeys(string hashid)
        {
            return RedisBase.iClient.GetHashKeys(hashid);
        }
        /// <summary>
        /// 获取hashid数据集中的所有value集合
        /// </summary>
        public List<string> GetHashValues(string hashid)
        {
            return RedisBase.iClient.GetHashValues(hashid);
        }
        /// <summary>
        /// 获取hashid数据集中,key的value数据
        /// </summary>
        public string GetValueFromHash(string hashid, string key)
        {
            return RedisBase.iClient.GetValueFromHash(hashid, key);
        }
        /// <summary>
        /// 获取hashid数据集中,多个keys的value集合
        /// </summary>
        public List<string> GetValuesFromHash(string hashid, string[] keys)
        {
            return RedisBase.iClient.GetValuesFromHash(hashid, keys);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除hashid数据集中的key数据
        /// </summary>
        public bool RemoveEntryFromHash(string hashid, string key)
        {
            return RedisBase.iClient.RemoveEntryFromHash(hashid, key);
        }
        #endregion

        #region 其它
        /// <summary>
        /// 判断hashid数据集中是否存在key的数据
        /// </summary>
        public bool HashContainsEntry(string hashid, string key)
        {
            return RedisBase.iClient.HashContainsEntry(hashid, key);
        }
        /// <summary>
        /// 给hashid数据集key的value加countby,返回相加后的数据
        /// </summary>
        public double IncrementValueInHash(string hashid, string key, double countBy)
        {
            return RedisBase.iClient.IncrementValueInHash(hashid, key, countBy);
        }
        #endregion
    }
}
using MyRedisDemo.Interface;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo.Service
{
    /// <summary>
    ///  Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,
    ///  Redis内部的很多实现,包括发送缓冲队列等也都是用的这个数据结构。  
    /// </summary>
    public class RedisListService : RedisBase
    {
        #region 赋值
        /// <summary>
        /// 从左侧向list中添加值
        /// </summary>
        public void LPush(string key, string value)
        {
            RedisBase.iClient.PushItemToList(key, value);
        }
        /// <summary>
        /// 从左侧向list中添加值,并设置过期时间
        /// </summary>
        public void LPush(string key, string value, DateTime dt)
        {
            RedisBase.iClient.PushItemToList(key, value);
            RedisBase.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 从左侧向list中添加值,设置过期时间
        /// </summary>
        public void LPush(string key, string value, TimeSpan sp)
        {
            RedisBase.iClient.PushItemToList(key, value);
            RedisBase.iClient.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 从左侧向list中添加值
        /// </summary>
        public void RPush(string key, string value)
        {
            RedisBase.iClient.PrependItemToList(key, value);
        }
        /// <summary>
        /// 从右侧向list中添加值,并设置过期时间
        /// </summary>    
        public void RPush(string key, string value, DateTime dt)
        {
            RedisBase.iClient.PrependItemToList(key, value);
            RedisBase.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 从右侧向list中添加值,并设置过期时间
        /// </summary>        
        public void RPush(string key, string value, TimeSpan sp)
        {
            RedisBase.iClient.PrependItemToList(key, value);
            RedisBase.iClient.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 添加key/value
        /// </summary>     
        public void Add(string key, string value)
        {
            RedisBase.iClient.AddItemToList(key, value);
        }
        /// <summary>
        /// 添加key/value ,并设置过期时间
        /// </summary>  
        public void Add(string key, string value, DateTime dt)
        {
            RedisBase.iClient.AddItemToList(key, value);
            RedisBase.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 添加key/value。并添加过期时间
        /// </summary>  
        public void Add(string key, string value, TimeSpan sp)
        {
            RedisBase.iClient.AddItemToList(key, value);
            RedisBase.iClient.ExpireEntryIn(key, sp);
        }
        /// <summary>
        /// 为key添加多个值
        /// </summary>  
        public void Add(string key, List<string> values)
        {
            RedisBase.iClient.AddRangeToList(key, values);
        }
        /// <summary>
        /// 为key添加多个值,并设置过期时间
        /// </summary>  
        public void Add(string key, List<string> values, DateTime dt)
        {
            RedisBase.iClient.AddRangeToList(key, values);
            RedisBase.iClient.ExpireEntryAt(key, dt);
        }
        /// <summary>
        /// 为key添加多个值,并设置过期时间
        /// </summary>  
        public void Add(string key, List<string> values, TimeSpan sp)
        {
            RedisBase.iClient.AddRangeToList(key, values);
            RedisBase.iClient.ExpireEntryIn(key, sp);
        }
        #endregion

        #region 获取值
        /// <summary>
        /// 获取list中key包含的数据数量
        /// </summary>  
        public long Count(string key)
        {
            return RedisBase.iClient.GetListCount(key);
        }
        /// <summary>
        /// 获取key包含的所有数据集合
        /// </summary>  
        public List<string> Get(string key)
        {
            return RedisBase.iClient.GetAllItemsFromList(key);
        }
        /// <summary>
        /// 获取key中下标为star到end的值集合
        /// </summary>  
        public List<string> Get(string key, int star, int end)
        {
            return RedisBase.iClient.GetRangeFromList(key, star, end);
        }
        #endregion

        #region 阻塞命令
        /// <summary>
        ///  阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public string BlockingPopItemFromList(string key, TimeSpan? sp)
        {
            return RedisBase.iClient.BlockingDequeueItemFromList(key, sp);
        }
        /// <summary>
        ///  阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public ItemRef BlockingPopItemFromLists(string[] keys, TimeSpan? sp)
        {
            return RedisBase.iClient.BlockingPopItemFromLists(keys, sp);
        }
        /// <summary>
        ///  阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public string BlockingDequeueItemFromList(string key, TimeSpan? sp)
        {
            return RedisBase.iClient.BlockingDequeueItemFromList(key, sp);
        }
        /// <summary>
        /// 阻塞命令:从list中keys的尾部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public ItemRef BlockingDequeueItemFromLists(string[] keys, TimeSpan? sp)
        {
            return RedisBase.iClient.BlockingDequeueItemFromLists(keys, sp);
        }
        /// <summary>
        /// 阻塞命令:从list中key的头部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public string BlockingRemoveStartFromList(string keys, TimeSpan? sp)
        {
            return RedisBase.iClient.BlockingRemoveStartFromList(keys, sp);
        }
        /// <summary>
        /// 阻塞命令:从list中key的头部移除一个值,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public ItemRef BlockingRemoveStartFromLists(string[] keys, TimeSpan? sp)
        {
            return RedisBase.iClient.BlockingRemoveStartFromLists(keys, sp);
        }
        /// <summary>
        /// 阻塞命令:从list中一个fromkey的尾部移除一个值,添加到另外一个tokey的头部,并返回移除的值,阻塞时间为sp
        /// </summary>  
        public string BlockingPopAndPushItemBetweenLists(string fromkey, string tokey, TimeSpan? sp)
        {
            return RedisBase.iClient.BlockingPopAndPushItemBetweenLists(fromkey, tokey, sp);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 从尾部移除数据,返回移除的数据
        /// </summary>  
        public string PopItemFromList(string key)
        {
            return RedisBase.iClient.PopItemFromList(key);
        }
        /// <summary>
        /// 移除list中,key/value,与参数相同的值,并返回移除的数量
        /// </summary>  
        public long RemoveItemFromList(string key, string value)
        {
            return RedisBase.iClient.RemoveItemFromList(key, value);
        }
        /// <summary>
        /// 从list的尾部移除一个数据,返回移除的数据
        /// </summary>  
        public string RemoveEndFromList(string key)
        {
            return RedisBase.iClient.RemoveEndFromList(key);
        }
        /// <summary>
        /// 从list的头部移除一个数据,返回移除的值
        /// </summary>  
        public string RemoveStartFromList(string key)
        {
            return RedisBase.iClient.RemoveStartFromList(key);
        }
        #endregion

        #region 其它
        /// <summary>
        /// 从一个list的尾部移除一个数据,添加到另外一个list的头部,并返回移动的值
        /// </summary>  
        public string PopAndPushItemBetweenLists(string fromKey, string toKey)
        {
            return RedisBase.iClient.PopAndPushItemBetweenLists(fromKey, toKey);
        }
        #endregion
    }
}
using MyRedisDemo.Interface;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo.Service
{
    /// <summary>
    /// Set:用哈希表来保持字符串的唯一性,没有先后顺序,存储一些集合性的数据
    /// 1.共同好友、二度好友
    /// 2.利用唯一性,可以统计访问网站的所有独立 IP
    /// </summary>
    public class RedisSetService : RedisBase
    {
        #region 添加
        /// <summary>
        /// key集合中添加value值
        /// </summary>
        public void Add(string key, string value)
        {
            RedisBase.iClient.AddItemToSet(key, value);
        }
        /// <summary>
        /// key集合中添加list集合
        /// </summary>
        public void Add(string key, List<string> list)
        {
            RedisBase.iClient.AddRangeToSet(key, list);
        }
        #endregion

        #region 获取
        /// <summary>
        /// 随机获取key集合中的一个值
        /// </summary>
        public string GetRandomItemFromSet(string key)
        {
            return RedisBase.iClient.GetRandomItemFromSet(key);
        }
        /// <summary>
        /// 获取key集合值的数量
        /// </summary>
        public long GetCount(string key)
        {
            return RedisBase.iClient.GetSetCount(key);
        }
        /// <summary>
        /// 获取所有key集合的值
        /// </summary>
        public HashSet<string> GetAllItemsFromSet(string key)
        {
            return RedisBase.iClient.GetAllItemsFromSet(key);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 随机删除key集合中的一个值
        /// </summary>
        public string RandomRemoveItemFromSet(string key)
        {
            return RedisBase.iClient.PopItemFromSet(key);
        }
        /// <summary>
        /// 删除key集合中的value
        /// </summary>
        public void RemoveItemFromSet(string key, string value)
        {
            RedisBase.iClient.RemoveItemFromSet(key, value);
        }
        #endregion

        #region 其它
        /// <summary>
        /// 从fromkey集合中移除值为value的值,并把value添加到tokey集合中
        /// </summary>
        public void MoveBetweenSets(string fromkey, string tokey, string value)
        {
            RedisBase.iClient.MoveBetweenSets(fromkey, tokey, value);
        }
        /// <summary>
        /// 返回keys多个集合中的并集,返还hashset
        /// </summary>
        public HashSet<string> GetUnionFromSets(params string[] keys)
        {
            return RedisBase.iClient.GetUnionFromSets(keys);
        }
        /// <summary>
        /// 返回keys多个集合中的交集,返还hashset
        /// </summary>
        public HashSet<string> GetIntersectFromSets(params string[] keys)
        {
            return RedisBase.iClient.GetIntersectFromSets(keys);
        }
        /// <summary>
        /// 返回keys多个集合中的差集,返还hashset
        /// </summary>
        /// <param name="fromKey">原集合</param>
        /// <param name="keys">其他集合</param>
        /// <returns>出现在原集合,但不包含在其他集合</returns>
        public HashSet<string> GetDifferencesFromSet(string fromKey, params string[] keys)
        {
            return RedisBase.iClient.GetDifferencesFromSet(fromKey,keys);
        }
        /// <summary>
        /// keys多个集合中的并集,放入newkey集合中
        /// </summary>
        public void StoreUnionFromSets(string newkey, string[] keys)
        {
            RedisBase.iClient.StoreUnionFromSets(newkey, keys);
        }
        /// <summary>
        /// 把fromkey集合中的数据与keys集合中的数据对比,fromkey集合中不存在keys集合中,则把这些不存在的数据放入newkey集合中
        /// </summary>
        public void StoreDifferencesFromSet(string newkey, string fromkey, string[] keys)
        {
            RedisBase.iClient.StoreDifferencesFromSet(newkey, fromkey, keys);
        }
        #endregion
    }
}
using MyRedisDemo.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo.Service
{
    /// <summary>
    /// key-value 键值对:value可以是序列化的数据
    /// </summary>
    public class RedisStringService : RedisBase
    {
        #region 赋值
        /// <summary>
        /// 设置key的value
        /// </summary>
        public bool Set(string key, string value)
        {
            return RedisBase.iClient.Set<string>(key, value);
        }
        /// <summary>
        /// 设置key的value并设置过期时间
        /// </summary>
        public bool Set(string key, string value, DateTime dt)
        {
            return RedisBase.iClient.Set<string>(key, value, dt);
        }
        /// <summary>
        /// 设置key的value并设置过期时间
        /// </summary>
        public bool Set(string key, string value, TimeSpan sp)
        {
            return RedisBase.iClient.Set<string>(key, value, sp);
        }
        /// <summary>
        /// 设置多个key/value
        /// </summary>
        public void Set(Dictionary<string, string> dic)
        {
            RedisBase.iClient.SetAll(dic);
        }

        #endregion

        #region 追加
        /// <summary>
        /// 在原有key的value值之后追加value
        /// </summary>
        public long Append(string key, string value)
        {
            return RedisBase.iClient.AppendToValue(key, value);
        }
        #endregion

        #region 获取值
        /// <summary>
        /// 获取key的value值
        /// </summary>
        public string Get(string key)
        {
            return RedisBase.iClient.GetValue(key);
        }
        /// <summary>
        /// 获取多个key的value值
        /// </summary>
        public List<string> Get(List<string> keys)
        {
            return RedisBase.iClient.GetValues(keys);
        }
        /// <summary>
        /// 获取多个key的value值
        /// </summary>
        public List<T> Get<T>(List<string> keys)
        {
            return RedisBase.iClient.GetValues<T>(keys);
        }
        #endregion

        #region 获取旧值赋上新值
        /// <summary>
        /// 获取旧值赋上新值
        /// </summary>
        public string GetAndSetValue(string key, string value)
        {
            return RedisBase.iClient.GetAndSetValue(key, value);
        }
        #endregion

        #region 辅助方法
        /// <summary>
        /// 获取值的长度
        /// </summary>
        public long GetLength(string key)
        {
            return RedisBase.iClient.GetStringCount(key);
        }
        /// <summary>
        /// 自增1,返回自增后的值
        /// </summary>
        public long Incr(string key)
        {
            return RedisBase.iClient.IncrementValue(key);
        }
        /// <summary>
        /// 自增count,返回自增后的值
        /// </summary>
        public double IncrBy(string key, double count)
        {
            return RedisBase.iClient.IncrementValueBy(key, count);
        }
        /// <summary>
        /// 自减1,返回自减后的值
        /// </summary>
        public long Decr(string key)
        {
            return RedisBase.iClient.DecrementValue(key);
        }
        /// <summary>
        /// 自减count ,返回自减后的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public long DecrBy(string key, int count)
        {
            return RedisBase.iClient.DecrementValueBy(key, count);
        }
        #endregion
    }
}
using MyRedisDemo.Interface;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo.Service
{
    /// <summary>
    /// Sorted Sets是将 Set 中的元素增加了一个权重参数 score,使得集合中的元素能够按 score 进行有序排列
    /// 1.带有权重的元素,比如一个游戏的用户得分排行榜
    /// 2.比较复杂的数据结构,一般用到的场景不算太多
    /// </summary>
    public class RedisZSetService : RedisBase
    {
        #region 添加
        /// <summary>
        /// 添加key/value,默认分数是从1.多*10的9次方以此递增的,自带自增效果
        /// </summary>
        public bool Add(string key, string value)
        {
            return RedisBase.iClient.AddItemToSortedSet(key, value);
        }
        /// <summary>
        /// 添加key/value,并设置value的分数
        /// </summary>
        public bool AddItemToSortedSet(string key, string value, double score)
        {
            return RedisBase.iClient.AddItemToSortedSet(key, value, score);
        }
        /// <summary>
        /// 为key添加values集合,values集合中每个value的分数设置为score
        /// </summary>
        public bool AddRangeToSortedSet(string key, List<string> values, double score)
        {
            return RedisBase.iClient.AddRangeToSortedSet(key, values, score);
        }
        /// <summary>
        /// 为key添加values集合,values集合中每个value的分数设置为score
        /// </summary>
        public bool AddRangeToSortedSet(string key, List<string> values, long score)
        {
            return RedisBase.iClient.AddRangeToSortedSet(key, values, score);
        }
        #endregion

        #region 获取
        /// <summary>
        /// 获取key的所有集合
        /// </summary>
        public List<string> GetAll(string key)
        {
            return RedisBase.iClient.GetAllItemsFromSortedSet(key);
        }
        /// <summary>
        /// 获取key的所有集合,倒叙输出
        /// </summary>
        public List<string> GetAllDesc(string key)
        {
            return RedisBase.iClient.GetAllItemsFromSortedSetDesc(key);
        }
        /// <summary>
        /// 获取集合,带分数
        /// </summary>
        public IDictionary<string, double> GetAllWithScoresFromSortedSet(string key)
        {
            return RedisBase.iClient.GetAllWithScoresFromSortedSet(key);
        }
        /// <summary>
        /// 获取key为value的下标值
        /// </summary>
        public long GetItemIndexInSortedSet(string key, string value)
        {
            return RedisBase.iClient.GetItemIndexInSortedSet(key, value);
        }
        /// <summary>
        /// 倒叙排列获取key为value的下标值
        /// </summary>
        public long GetItemIndexInSortedSetDesc(string key, string value)
        {
            return RedisBase.iClient.GetItemIndexInSortedSetDesc(key, value);
        }
        /// <summary>
        /// 获取key为value的分数
        /// </summary>
        public double GetItemScoreInSortedSet(string key, string value)
        {
            return RedisBase.iClient.GetItemScoreInSortedSet(key, value);
        }
        /// <summary>
        /// 获取key所有集合的数据总数
        /// </summary>
        public long GetSortedSetCount(string key)
        {
            return RedisBase.iClient.GetSortedSetCount(key);
        }
        /// <summary>
        /// key集合数据从分数为fromscore到分数为toscore的数据总数
        /// </summary>
        public long GetSortedSetCount(string key, double fromScore, double toScore)
        {
            return RedisBase.iClient.GetSortedSetCount(key, fromScore, toScore);
        }
        /// <summary>
        /// 获取key集合从高分到低分排序数据,分数从fromscore到分数为toscore的数据
        /// </summary>
        public List<string> GetRangeFromSortedSetByHighestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.iClient.GetRangeFromSortedSetByHighestScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 获取key集合从低分到高分排序数据,分数从fromscore到分数为toscore的数据
        /// </summary>
        public List<string> GetRangeFromSortedSetByLowestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.iClient.GetRangeFromSortedSetByLowestScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 获取key集合从高分到低分排序数据,分数从fromscore到分数为toscore的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSetByHighestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.iClient.GetRangeWithScoresFromSortedSetByHighestScore(key, fromscore, toscore);
        }
        /// <summary>
        ///  获取key集合从低分到高分排序数据,分数从fromscore到分数为toscore的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSetByLowestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.iClient.GetRangeWithScoresFromSortedSetByLowestScore(key, fromscore, toscore);
        }
        /// <summary>
        ///  获取key集合数据,下标从fromRank到分数为toRank的数据
        /// </summary>
        public List<string> GetRangeFromSortedSet(string key, int fromRank, int toRank)
        {
            return RedisBase.iClient.GetRangeFromSortedSet(key, fromRank, toRank);
        }
        /// <summary>
        /// 获取key集合倒叙排列数据,下标从fromRank到分数为toRank的数据
        /// </summary>
        public List<string> GetRangeFromSortedSetDesc(string key, int fromRank, int toRank)
        {
            return RedisBase.iClient.GetRangeFromSortedSetDesc(key, fromRank, toRank);
        }
        /// <summary>
        /// 获取key集合数据,下标从fromRank到分数为toRank的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSet(string key, int fromRank, int toRank)
        {
            return RedisBase.iClient.GetRangeWithScoresFromSortedSet(key, fromRank, toRank);
        }
        /// <summary>
        ///  获取key集合倒叙排列数据,下标从fromRank到分数为toRank的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSetDesc(string key, int fromRank, int toRank)
        {
            return RedisBase.iClient.GetRangeWithScoresFromSortedSetDesc(key, fromRank, toRank);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除key为value的数据
        /// </summary>
        public bool RemoveItemFromSortedSet(string key, string value)
        {
            return RedisBase.iClient.RemoveItemFromSortedSet(key, value);
        }
        /// <summary>
        /// 删除下标从minRank到maxRank的key集合数据
        /// </summary>
        public long RemoveRangeFromSortedSet(string key, int minRank, int maxRank)
        {
            return RedisBase.iClient.RemoveRangeFromSortedSet(key, minRank, maxRank);
        }
        /// <summary>
        /// 删除分数从fromscore到toscore的key集合数据
        /// </summary>
        public long RemoveRangeFromSortedSetByScore(string key, double fromscore, double toscore)
        {
            return RedisBase.iClient.RemoveRangeFromSortedSetByScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 删除key集合中分数最大的数据
        /// </summary>
        public string PopItemWithHighestScoreFromSortedSet(string key)
        {
            return RedisBase.iClient.PopItemWithHighestScoreFromSortedSet(key);
        }
        /// <summary>
        /// 删除key集合中分数最小的数据
        /// </summary>
        public string PopItemWithLowestScoreFromSortedSet(string key)
        {
            return RedisBase.iClient.PopItemWithLowestScoreFromSortedSet(key);
        }
        #endregion

        #region 其它
        /// <summary>
        /// 判断key集合中是否存在value数据
        /// </summary>
        public bool SortedSetContainsItem(string key, string value)
        {
            return RedisBase.iClient.SortedSetContainsItem(key, value);
        }
        /// <summary>
        /// 为key集合值为value的数据,分数加scoreby,返回相加后的分数
        /// </summary>
        public double IncrementItemInSortedSet(string key, string value, double scoreBy)
        {
            return RedisBase.iClient.IncrementItemInSortedSet(key, value, scoreBy);
        }
        /// <summary>
        /// 获取keys多个集合的交集,并把交集添加的newkey集合中,返回交集数据的总数
        /// </summary>
        public long StoreIntersectFromSortedSets(string newkey, string[] keys)
        {
            return RedisBase.iClient.StoreIntersectFromSortedSets(newkey, keys);
        }
        /// <summary>
        /// 获取keys多个集合的并集,并把并集数据添加到newkey集合中,返回并集数据的总数
        /// </summary>
        public long StoreUnionFromSortedSets(string newkey, string[] keys)
        {
            return RedisBase.iClient.StoreUnionFromSortedSets(newkey, keys);
        }
        #endregion
    }
}
using MyRedisDemo.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyRedisDemo
{
    ///<summary>
    /// 1 Cache和NoSql
    /// 2 String
    /// 3 Hashtable
    /// 4 Set
    /// 5 ZSet
    /// 6 List
    /// 7 分布式异步队列
    /// 
    /// REmote DIctionary Server
    /// ServiceStack.Redis
    /// 6000 Request per hour
    ///</summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Student student_1 = new Student()
                {
                    Id = 11,
                    Name = "Eleven"
                };
                Student student_2 = new Student()
                {
                    Id = 12,
                    Name = "Twelve",
                    Remark = "123423245"
                };

                //Student_2_id  12  Student_2_Name  Twelve
                //需要修改对象
                //查询--反序列化--修改--序列化保存
                Console.WriteLine("*****************************************");
                {
                    //RedisStringService service = new RedisStringService();
                    //service.FlushAll();
                    //service.Set("RedisStringService_key1", "RedisStringService_value1");
                    //Console.WriteLine(service.Get("RedisStringService_key1"));
                    //Console.WriteLine(service.GetAndSetValue("RedisStringService_key1", "RedisStringService_value2"));
                    //Console.WriteLine(service.Get("RedisStringService_key1"));

                    //service.Append("RedisStringService_key1", "Append");
                    //Console.WriteLine(service.Get("RedisStringService_key1"));
                    //service.Set("RedisStringService_key1", "RedisStringService_value", DateTime.Now.AddSeconds(5));
                    //Console.WriteLine(service.Get("RedisStringService_key1"));
                    //Thread.Sleep(5000);
                    //Console.WriteLine(service.Get("RedisStringService_key1"));

                }

                Console.WriteLine("*****************************************");
                {
                    //RedisHashService service = new RedisHashService();
                    //service.FlushAll();
                    //service.SetEntryInHash("Student", "id", "13");
                    //service.SetEntryInHash("Student", "Name", "Thirteen");
                    //service.SetEntryInHashIfNotExists("Student", "Remark", "1234567");
                    //var listResult = service.GetHashValues("Student");
                    //listResult = service.GetHashKeys("Student");

                    //var dicList = service.GetAllEntriesFromHash("Student");

                    //service.SetEntryInHash("Student", "id", "14");//同一条数据,覆盖
                    //service.SetEntryInHash("Student", "Name", "Fourteen");
                    //service.SetEntryInHashIfNotExists("Student", "Remark", "2345678");//同一条数据,不覆盖

                    //listResult = service.GetHashValues("Student");
                    //service.RemoveEntryFromHash("Student", "Remark");
                    //service.SetEntryInHashIfNotExists("Student", "Remark", "2345678");
                    //listResult = service.GetHashValues("Student");

                    //service.StoreAsHash<Student>(student_1);
                    //Student student1 = service.GetFromHash<Student>(11);
                    //service.StoreAsHash<Student>(student_2);
                    //Student student2 = service.GetFromHash<Student>(12);
                }
                Console.WriteLine("*****************************************");
                {
                    ////key--values
                    //RedisSetService service = new RedisSetService();
                    //service.FlushAll();
                    //service.Add("Advanced", "111");
                    //service.Add("Advanced", "112");
                    //service.Add("Advanced", "113");
                    //service.Add("Advanced", "114");
                    //service.Add("Advanced", "115");
                    //service.Add("Advanced", "111");

                    //service.Add("Begin", "111");
                    //service.Add("Begin", "112");
                    //service.Add("Begin", "113");
                    //service.Add("Begin", "116");
                    //service.Add("Begin", "117");
                    //service.Add("Begin", "111");

                    //service.Add("Internal", "111");
                    //service.Add("Internal", "112");
                    //service.Add("Internal", "117");
                    //service.Add("Internal", "118");
                    //service.Add("Internal", "119");
                    //service.Add("Internal", "111");

                    //var result = service.GetAllItemsFromSet("Advanced");
                    //var result2 = service.GetRandomItemFromSet("Advanced");
                    //result = service.GetAllItemsFromSet("Begin");
                    //result2 = service.GetRandomItemFromSet("Begin");

                    //var result3 = service.GetIntersectFromSets("Advanced", "Begin", "Internal");////result3 = service.GetDifferencesFromSet("Advanced", "Begin", "Internal");////result3 = service.GetUnionFromSets("Advanced", "Begin", "Internal");////service.RemoveItemFromSet("Advanced", "111");
                    //result = service.GetAllItemsFromSet("Advanced");
                    //service.RandomRemoveItemFromSet("Advanced");
                    //result = service.GetAllItemsFromSet("Advanced");
                }
                Console.WriteLine("*****************************************");
                {
                    //RedisZSetService service = new RedisZSetService();
                    //service.FlushAll();
                    //service.Add("score", "111");
                    //service.Add("score", "112");
                    //service.Add("score", "113");
                    //service.Add("score", "114");
                    //service.Add("score", "115");
                    //service.Add("score", "111");

                    //service.AddItemToSortedSet("user", "Eleven1", 1);

                    //service.AddItemToSortedSet("user", "Eleven2", 2);

                    //var list = service.GetAll("score");
                    //list = service.GetAllDesc("score");
                }
                Console.WriteLine("*****************************************");
                {
                    RedisListService service = new RedisListService();
                    service.FlushAll();

                    List<string> stringList = new List<string>();
                    for (int i = 0; i < 10; i++)
                    {
                        stringList.Add(string.Format("放入任务{0}", i));
                    }

                    service.LPush("test", "这是一个学生1");
                    service.LPush("test", "这是一个学生2");
                    service.LPush("test", "这是一个学生3");
                    service.LPush("test", "这是一个学生4");
                    service.Add("task", stringList);

                    //Console.WriteLine(service.Count("test"));
                    //Console.WriteLine(service.Count("task"));
                    //var list = service.Get("test");
                    //list = service.Get("task", 2, 4);


                    //new Action(() =>
                    //{
                    //    while (true)
                    //    {
                    //        var result = service.BlockingPopItemFromLists(new string[] { "test", "task" }, TimeSpan.FromHours(3));
                    //        Console.WriteLine("这里是队列获取的消息 {0} {1}", result.Id, result.Item);
                    //    }
                    //}).BeginInvoke(null, null);

                    Action act = new Action(() =>
                     {
                         while (true)
                         {
                             Console.WriteLine("************请输入数据**************");
                             string testTask = Console.ReadLine();
                             service.LPush("test", testTask);
                         }
                     });
                    act.EndInvoke(act.BeginInvoke(null, null));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRedisDemo
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Remark { get; set; }
        public string Description { get; set; }
    }
}

 

posted on 2018-03-27 16:02  口袋里的SKY  阅读(334)  评论(0编辑  收藏  举报