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) 数据结构 字典 跳跃表 使用场景 会话缓存 缓存 计数器 查找表 消息队列 ...
随机推荐
- Spring框架的XML扩展特性
Spring框架从2.0版本开始,提供了基于Schema风格的XML扩展机制,允许开发者扩展spring配置文件.现在我们来看下怎么实现这个功能,可以参考spring帮助文档中的<Extensi ...
- vim学习
vim编辑器的工作模式分为3种 1.Command Mode 命令模式 2.Insert Mode 插入模式 3.Lastline Mode 底行模式 vim 打开文件时处于命令模式,i 可以切换到插 ...
- Android RelativeLayout用到的一些重要的属性
转载自 http://mobile.51cto.com/android-265842.htm 第一类:属性值为true或false android:layout_centerHrizontal 水平 ...
- Unix 用gdb分析core dump文件
产生core文件条件 用ulimit -c 指定core文件大小来开启core文件的生成,如:ulimit -c unlimited 用gdb分析core文件的条件 可执行程序在编译时,需加入-g参数 ...
- sftp自动授权登录
客户的账号下执行 ssh-keygen -t rsa 生成秘钥文件 ~/.ssh/id_rsa --秘钥文件 ~/.ssh/id_rsa.pub --公钥文件 将公钥文件id_rsa.pub放到sft ...
- 第三章 Git的入门 - 读书笔记
Android驱动月考3 第三章 Git的入门 - 读书笔记 对于Github,这是全世界最大的开源平台,你可以把你做的项目在这里开源,把你发现的一些新技术在这里开源,向全世界的开发者们分享,大家都彼 ...
- Sprintf新解 (ZT)
Sprintf新解 2012-08-06 11:26:45 分类: 原文地址:Sprintf新解 作者:harserm 由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已 ...
- javaweb 拦截器报错
拦截器报错 The content of element type "interceptor-ref" must match "(param)*".内容元素 ...
- jq封装的tab切换
function tab(a,b,c){ $(a).on(c,function(){ $(this).addClass('active').siblings().removeClass('active ...
- Unity3D 学习——入门资料整理
感觉可以写3D游戏,还是用C#,很好玩的样子.整理最近学习资料 介绍:游戏引擎,可以用C#配置的游戏引擎.嗯,就这样.其余度娘. 安装:官网,因为现在个人版是免费的.直接官网下.干净利落. 调试插件: ...