ServiceStack.Redis 之 IRedisTypedClient<第四篇>
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<第四篇>的更多相关文章
- ServiceStack.Redis 之 IRedisTypedClient 04_转
IRedisTypedClient IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似. 它支持在Redis中使用Linq查 ...
- ServiceStack.Redis 之 IRedisTypedClient
IRedisTypedClient IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似. 它支持在Redis中使用Linq查 ...
- ServiceStack.Redis泛型存储后getById问题
关于ServiceStack.Redis实体存储常用的有一下几个方法 StoreAsHash<T>(T entity) //将对象按照Hash存储 Redis.As<T>() ...
- servicestack.redis工具类
using System;using System.Collections.Generic;using System.Linq;using ServiceStack.Redis;using Servi ...
- 使用ServiceStack.Redis实现Redis数据读写
原文:使用ServiceStack.Redis实现Redis数据读写 User.cs实体类 public class User { public string Name { get; set; } p ...
- Service-stack.redis 使用PooledRedisClientManager 速度慢的原因之一
现在越来越多的开发者使用service-stack.redis 来进行redis的访问,但是获取redisclient的方式有多种方式,其中有一种从缓冲池获取client的方式很是得到大家的认可. L ...
- .Net使用Redis详解之ServiceStack.Redis(七)
序言 本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现,其中会对list的阻塞功能和事务的运用做二个案例,进行记录学习. Redi ...
- ServiceStack.Redis订阅发布服务的调用(Z)
1.Redis订阅发布介绍Redis订阅发布是一种消息通信模式:发布者(publisher)发送消息,订阅者(Subscriber)接受消息.类似于设计模式中的观察者模式.发布者和订阅者之间使用频 ...
- serviceStack.Redis 在PooledRedisClientManager 中设置密码
ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能.但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置, ...
随机推荐
- LOJ #2135. 「ZJOI2015」幻想乡战略游戏
#2135. 「ZJOI2015」幻想乡战略游戏 链接 分析: 动态点分治,求加权重心,带修改. 考虑如果知道了一个点s,如何求答案,那么首先可以点分治的思想,求每个联通块内所有点到分治中心距离和,然 ...
- cocos2d-x学习记录2——CCAction动作
CCAction能够使CCNode运动起来,能够呈现出多种多样的动作.这些动作能够改变其运动方向.形状.大小.旋转等. 同时,还可利用CCCallFunc.CCCallFuncN.CCCallFunc ...
- 【转】Spring Boot干货系列:(一)优雅的入门篇
转自Spring Boot干货系列:(一)优雅的入门篇 前言 Spring一直是很火的一个开源框架,在过去的一段时间里,Spring Boot在社区中热度一直很高,所以决定花时间来了解和学习,为自己做 ...
- SpringBoot日记——缓存的使用
SpringBoot核心技术的东西基本上都有介绍过了,接下来,进阶点~来说说缓存吧~ 缓存这个词不少同学应该不会很陌生.而我们这里主要使用的就是Redis. 客户端第一次请求的时候是从库里拿出我们需要 ...
- 百度地图API的网页使用
请看图示(以及参考官方文档): 图片尺寸:1710x822
- idou老师教你学Istio 17 : 通过HTTPS进行双向TLS传输
众所周知,HTTPS是用来解决 HTTP 明文协议的缺陷,在 HTTP 的基础上加入 SSL/TLS 协议,依靠 SSL 证书来验证服务器的身份,为客户端和服务器端之间建立“SSL”通道,确保数据运输 ...
- 微软职位内部推荐-Senior Dev Lead - SharePoint
微软近期Open的职位: SharePoint is a multi-billion dollar enterprise business that has grown from an on-prem ...
- App Inspector 功能详解
前言: App Inspector:浏览器端的移动设备 UI 查看器,使用树状态结构查看 UI 布局,自动生成 XPaths 官网:https://macacajs.github.io/app-ins ...
- CodeMirror 小册子
User manual and reference guide version 5.41.1 用户手册和参考指南 CodeMirror is a code-editor component ...
- python 安装多个包/pip用法
列出已安装的包 pip freeze or pip list 导出requirements.txt pip freeze > <目录>/requirements.txt 安装包 在线 ...