分享一个Redis帮助类

最近在项目中使用了redis来存储已经下载过的URL,项目中用的是ServiceStack来操作Redis,一开始ServiceStack的版本用的是最新的,后来发现ServiceStack已经商业化了,非商业版本每个小时只能操作6000次Redis,后来把ServiceStack换成了V3版本。

在项目中用到了redis的Hash集合,但是ServiceStack封装的使用起来不方便,于是就自己封装了一个dll,利用的ServiceStack的pool来动态创建IRedisClient实例,创建了一个抽象类RedisOperatorBase封装了一些基本方法。比较简单,相信大家一看代码就会。

代码Github下载地址:https://github.com/dazhuangtage/RedisDemo/

下面贴上最简单的用法:

首先配置文件配置节点:

 <configSections>
    <section name="RedisTools" type="RedisTools.RedisConfig, RedisTools"/>
  </configSections>
 <RedisTools WriteServerList="127.0.0.1:6379" ReadServerList="freep456187@127.0.0.1:6379" MaxWritePoolSize="60" MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false"/>
</configuration>

使用方法:

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            var hashOperator = new HashOperator();
            hashOperator.Set("test", "name", "大壮他哥");
            Console.WriteLine(hashOperator.Get<string>("test", "name"));
            Console.ReadKey();
        }    

如果大家要扩展一个帮助类,只需要继承RedisOperatorBase就可以了。

原文地址:https://www.cnblogs.com/dazhuangtage/p/5856174.html