IRedisTypedClient

IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似。

它支持在Redis中使用Linq查询的强大的类,它本身是一个泛型,IRedisClient的泛型方法As获得对象。

其方法原型如下:

IRedisTypedClient<T> As<T>();

1、IEntityStore<T>接口内容

其中IRedisTypedClient这个类实现了这个接口IEntityStore<T>,该接口要求提供的功能如下:

方法 说明
Delete 根据实体删除一条记录
DeleteAll 全部删除
DeleteById 根据Id删除一条记录
DeleteByIds 根据输入的多个Id删除多条记录
GetAll 获取所有该类型的记录
GetById 根据Id获取一条记录
GetByIds 根据输入的多个Id获取多条记录
Store 根据传入的实体,添加一条记录
StoreAll 根据传入的实体集合,添加多条记录

Linq查询(针对于GetAll方法返回的IList<T>)示例:

        public ActionResult Index()
{
Person p1 = new Person() { Id = 1, Name = "刘备" };
Person p2 = new Person() { Id = 2, Name = "关羽" };
Person p3 = new Person() { Id = 3, Name = "张飞" };
Person p4 = new Person() { Id = 4, Name = "曹操" };
Person p5 = new Person() { Id = 5, Name = "典韦" };
Person p6 = new Person() { Id = 6, Name = "郭嘉" };
List<Person> ListPerson = new List<Person>() { p2,p3,p4,p5,p6 }; using (IRedisClient RClient = prcm.GetClient())
{
IRedisTypedClient<Person> IRPerson = RClient.As<Person>();
IRPerson.DeleteAll(); //------------------------------------------添加-------------------------------------------- //添加单条数据
IRPerson.Store(p1);
//添加多条数据
IRPerson.StoreAll(ListPerson); //------------------------------------------查询-------------------------------------------- //Linq支持
Response.Write(IRPerson.GetAll().Where(m => m.Id == 1).First().Name); //刘备
//注意,用IRedisTypedClient的对象IRPerson的Srore()添加的才能用IRPerson()方法读取
Response.Write(IRPerson.GetAll().First(m => m.Id == 2).Name); //关羽          //------------------------------------------删除-------------------------------------------- IRPerson.Delete(p1); //删除 刘备
Response.Write(IRPerson.GetAll().Count()); //5
IRPerson.DeleteById(2); //删除 关羽
Response.Write(IRPerson.GetAll().Count()); //4
IRPerson.DeleteByIds(new List<int> { 3,4 }); //删除张飞 曹操
Response.Write(IRPerson.GetAll().Count()); //2
IRPerson.DeleteAll(); //全部删除
Response.Write(IRPerson.GetAll().Count()); //0
} return Content("");
}

另外,由于该接口并没有实现修改的方法,所以修改还得通过IRedisClient的实例:

        public ActionResult Index()
{
PooledRedisClientManager prcm = new PooledRedisClientManager(new List<string>() { "127.0.0.1" }, new List<string>() { "127.0.0.1" }, RedisConfig); Person p1 = new Person() { Id = 1, Name = "刘备" };
Person p2 = new Person() { Id = 2, Name = "关羽" };
Person p3 = new Person() { Id = 3, Name = "张飞" };
Person p4 = new Person() { Id = 4, Name = "曹操" };
Person p5 = new Person() { Id = 5, Name = "典韦" };
Person p6 = new Person() { Id = 6, Name = "郭嘉" };
List<Person> ListPerson = new List<Person>() { p2,p3,p4,p5,p6 }; using (IRedisClient RClient = prcm.GetClient())
{
IRedisTypedClient<Person> IRPerson = RClient.As<Person>();
IRPerson.StoreAll(ListPerson); //读取所有的Key
List<string> ListKeys = IRPerson.GetAllKeys();
foreach (string key in ListKeys)
{
Response.Write(key + "<br/>");
} //修改的话只能通过Key修改
//urn:person:3
//urn:person:4
//urn:person:5
//ids:Person
//urn:person:1
//urn:person:6
//urn:person:2
Person p7 = new Person() { Id = 8, Name = "撼地神牛" };
RClient.Set("urn:person:1", p7);
Response.Write(IRPerson.GetAll().First(m => m.Id == 8).Name); //输出 撼地神牛
} return Content("");
}

ServiceStack.Redis 之 IRedisTypedClient的更多相关文章

  1. ServiceStack.Redis 之 IRedisTypedClient 04_转

    IRedisTypedClient IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似. 它支持在Redis中使用Linq查 ...

  2. ServiceStack.Redis 之 IRedisTypedClient<第四篇>

    IRedisTypedClient IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似. 它支持在Redis中使用Linq查 ...

  3. ServiceStack.Redis泛型存储后getById问题

    关于ServiceStack.Redis实体存储常用的有一下几个方法 StoreAsHash<T>(T entity)  //将对象按照Hash存储 Redis.As<T>() ...

  4. servicestack.redis工具类

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

  5. 使用ServiceStack.Redis实现Redis数据读写

    原文:使用ServiceStack.Redis实现Redis数据读写 User.cs实体类 public class User { public string Name { get; set; } p ...

  6. Service-stack.redis 使用PooledRedisClientManager 速度慢的原因之一

    现在越来越多的开发者使用service-stack.redis 来进行redis的访问,但是获取redisclient的方式有多种方式,其中有一种从缓冲池获取client的方式很是得到大家的认可. L ...

  7. .Net使用Redis详解之ServiceStack.Redis(七)

    序言 本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现,其中会对list的阻塞功能和事务的运用做二个案例,进行记录学习. Redi ...

  8. ServiceStack.Redis订阅发布服务的调用(Z)

      1.Redis订阅发布介绍Redis订阅发布是一种消息通信模式:发布者(publisher)发送消息,订阅者(Subscriber)接受消息.类似于设计模式中的观察者模式.发布者和订阅者之间使用频 ...

  9. serviceStack.Redis 在PooledRedisClientManager 中设置密码

    ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能.但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置, ...

随机推荐

  1. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\d29b5393\123c3a1c\App_Code.odl3w4o6.dll”--“拒绝访问。 ”

    IIS部署网站或者Webservice时,出现以下问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...

  2. asp.net命名规范

    以下命名规范是在编程中,可以辅助快速编程的良好方式之一,我一点点的整理出来,以便形成自己的编程规范.还有待完善... 0.产品命名规范: 结构 层次 产品 模块 功能 命名规则 UI(界面层) Web ...

  3. 利用SQL进行推理

    数据库环境:SQL SERVER 2008R2 有如下需求: Baker, Cooper, Fletcher, Miller and Smith住在一座房子的不同楼层.Baker 不住顶层.Coope ...

  4. XhProf安装教程–详细教程

    wget去这里找最新的包下载,我现在最新的是xhprof-0.9.4.tgz cd /tmp mkdir xhprof && cd xhprof wget http://pecl.ph ...

  5. 前端开发构建工具gulp的安装使用

    曾几何时还在使用grunt作为前端的构建工具,直到有一天同事向我推荐了gulp,在这里博主将不讨论gulp与grunt各自优势的比较,只为大家介绍gulp如何安装和使用. Gulp 是用 nodejs ...

  6. [转]深入理解JavaScript系列

    文章转自:汤姆大叔-深入理解JavaScript系列文章 深入理解JavaScript系列文章,包括了原创,翻译,转载,整理等各类型文章,如果对你有用,请推荐支持一把,给大叔写作的动力. 深入理解Ja ...

  7. 祭奠一年前写 Direct2D demo

    一年前, 用Direct2D实现了不怎么样的UI库. 用不怎么样的UI库实现了这个Demo. 当时放进某群共享, 以此装逼. 今天无意中翻出来, 解压, 什么都没变, 还是压缩前的模样. 不经意看见被 ...

  8. php日期处理

    $datetime=strtotime(date("Y-m-d",time()));//获取当前日期并转换成时间戳 $datetime=$datetime+86400;//在时间戳 ...

  9. SlidesJS 3.0.4 在手机上遇到的一些问题及解决办法

    SlidesJS 3.0.4 http://slidesjs.com 在手机上遇到的一些问题及解决办法 1.手机上打开有sliderjs的页面后, 切换到别的页面再回来时, sliderjs部分不能滑 ...

  10. Struts面试笔记

    Struts2面试题1.struts2工作流程Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器 ...