Redis基础(转)
? 1
2
3
4
5
6
7
8
9
10
11 public class User
{
public User()
{
this.BlogIds = new List<long>();
}
public long Id { get; set; }
public string Name { get; set; }
public List<long> BlogIds { get; set; }
}</long></long>
? 1
2
3
4
5
6
7 using (var redisUsers = redisClient.GetTypedClient<user>())
{
var ayende = new User { Id = redisUsers.GetNextSequence(), Name = "Oren Eini" };
var mythz = new User { Id = redisUsers.GetNextSequence(), Name = "Demis Bellot" };
redisUsers.Store(ayende);
redisUsers.Store(mythz);
}</user>
? 1
2
3
4
5 redis 127.0.0.1:6379[1]> keys *
1) "seq:User"
2) "ids:User"
3) "urn:user:1"
4) "urn:user:2"
|
seq:User
|
string
|
维护当前类型User的ID自增序列,用做对象唯一ID
|
|
ids:User
|
set
|
同一类型User所有对象ID的列表
|
|
urn:user:1
|
string
|
user对象
|
? 1
2
3
4
5
6
7
8 public long GetNextSequence(int incrBy)
{
return IncrementValueBy(SequenceKey, incrBy);
}
public long IncrementValue(string key)
{
return client.Incr(key);
}
? 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public T Store(T entity)
{
var urnKey = entity.CreateUrn();
this.SetEntry(urnKey, entity);
return entity;
}
//entity.CreateUrn();的结果是"urn:user:1"
public void SetEntry(string key, T value)
{
if (key == null)
throw new ArgumentNullException("key");
client.Set(key, SerializeValue(value));
client.RegisterTypeId(value);
}
internal void RegisterTypeId<t>(T value)
{
var typeIdsSetKey = GetTypeIdsSetKey<t>();
var id = value.GetId().ToString();
if (this.Pipeline != null)
{
var registeredTypeIdsWithinPipeline = GetRegisteredTypeIdsWithinPipeline(typeIdsSetKey);
registeredTypeIdsWithinPipeline.Add(id);
}
else
{
this.AddItemToSet(typeIdsSetKey, id);
}
}</t></t>
? 1
2
3
4
5
6
7
8
9 %%第一种写法
logs.ForEach(n =>
{
pipeline.QueueCommand(r =>
{
((RedisClient)r).Store<oplog>(n, n.GetObjectID(), n.GetUrnKey());
((RedisClient)r).Expire(n.GetUrnKey(), dataLifeTime);
});
});</oplog>
? 1
2
3
4
5
6
7
8 %%第二种写法
logs.ForEach(n =>
{
pipeline.QueueCommand(r => ((RedisClient)r).Store<log>(n, n.ID, n.GetUrnKey()));
pipeline.QueueCommand(r => ((RedisClient)r).Expire(n.GetUrnKey(), dataLifeTime));
});</log>
执行CurrentQueuedOperation =
null;如果按照第一种写法会丢失回调函数,这就造成有返回值在没有及时提取,后续的操作获取返回值时首先取到的是积压的结果信息,就出现了异常,而第
二种写法就避免了这个问题.
|
1
2
3
4
5
|
protected virtual void AddCurrentQueuedOperation() { this.QueuedCommands.Add(CurrentQueuedOperation); CurrentQueuedOperation = null; } |
Redis基础(转)的更多相关文章
- windows下使用redis,Redis入门使用,Redis基础命令
windows下使用redis,Redis入门使用,Redis基础命令 >>>>>>>>>>>>>>>> ...
- [.net 面向对象程序设计深入](14)Redis——基础
[.net 面向对象程序设计深入](14)Redis——基础 很长一段时间没更新博客了,坚持做一件事,真不是件容易的事,后面我会继续尽可能的花时间更新完这个系列文章. 因这个系列的文章涉及的范围太大了 ...
- linux redis基础应用 主从服务器配置
Redis基础应用 redis是一个开源的可基于内存可持久化的日志型,key-value数据库redis的存储分为内存存储,磁盘存储和log文件三部分配置文件中有三个参数对其进行配置 优势:和memc ...
- [.net 面向对象程序设计深入](36)Redis——基础
[.net 面向对象程序设计深入](36)Redis——基础 很长一段时间没更新博客了,坚持做一件事,真不是件容易的事,后面我会继续尽可能的花时间更新完这个系列文章. 因这个系列的文章涉及的范围太大了 ...
- mysql主从复制、redis基础、持久化和主从复制
一.mysql(mariadb)基础 1.基础命令(centos7操作系统下) 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroo ...
- Redis基础用法、高级特性与性能调优以及缓存穿透等分析
一.Redis介绍 Redis是一个开源的,基于内存的结构化数据存储媒介,可以作为数据库.缓存服务或消息服务使用.Redis支持多种数据结构,包括字符串.哈希表.链表.集合.有序集合.位图.Hype ...
- Redis基础知识补充及持久化、备份介绍(二)--技术流ken
Redis知识补充 在上一篇博客<Redis基础认识及常用命令使用(一)--技术流ken>中已经介绍了redis的一些基础知识,以及常用命令的使用,本篇博客将补充一些基础知识以及redis ...
- Spring-Boot之Redis基础
Spring-Boot之Redis基础 准备 Redis下载地址:github.com/MSOpenTech/redis/releases Redis数据库的默认端口号是 6379 开启Redis服务 ...
- mongodb,Mysql,redis基础教程
数据库基础 1:mongodb基础教程 1:pymongo基础教程 2:Mysql基础教程 3:redis基础教程
- Redis基础知识点面试手册
Redis基础知识点面试手册 基础 概述 数据类型 STRING LIST SET HASH ZSET(SORTEDSET) 数据结构 字典 跳跃表 使用场景 会话缓存 缓存 计数器 查找表 消息队列 ...
随机推荐
- Unity3D图片的下载及保存
Unity3D图片的下载及保存 分类: Unity3D 2013-06-24 15:03 3609人阅读 评论(2) 收藏 举报 Unity3D图片URL 代码如下: [csharp] view pl ...
- 【原创】Linux常用管理命令总结
一.文件夹操作:1.查看文件夹ls [-al]/dir Diredtory_Name2.建立文件夹mkdir [-p] Diredtory_Name3.删除文件夹rm -r[f] Diredtory_ ...
- 尚学堂Spring视频教程(二):Spring控制反转
用Spring来实现IOC 在上节中我们自定义了一个接口BeanFactory和类ClassPathXmlApplicationContext来模拟Spring,其实它们在Spring中确实是存在的, ...
- Gesture Recognizers与触摸事件分发[转]
一.Gesture Recognizers Gesture Recognizers是在iOS3.2引入的,可以用来识别手势.简化定制视图事件处理的对象.Gesture Recognizers的基类为U ...
- 细说IIS异常日志 — 你必须知道的功能
最近在跟QAD用Webservice搞接口做数据维护,搞的哥那个叫头大,遇到很多问题,系统的log4net根本就无法记录.话说QAD调我某一个接口,可能包含几百个字段,而且QAD是个产品,所以我这边提 ...
- Oracle 常见函数
1.把数字转换为字符串:To_char(数字) 2.Oracle 拼接字符串: || ':' || select 字段1 || ':' || 字段2 from table :字段1:字段2 3. ...
- range for query
static void range_test(Args _args) { Query Query; QueryRun QueryRun ...
- reverse engineering in ax
install Visio2010 Premium(UML model template). not work in Visio 2013 and other version.
- ajax通讯之格式详解
前言: ajax的出现,一定程度上改变了js的命运,同时也被广泛使用,而jq的兴起也大大降低了ajax的使用难度.虽然,jq的ajax方法使用起来十分便利,但是大部分开发人员也仅仅只是对其中的几个属性 ...
- Dev ChartControl鼠标移动显示坐标点
his.chartIRC_RATES.MouseMove += new MouseEventHandler(chartIRC_RATES_MouseMove); ToolTipController t ...