标签:
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 > |
1
2
3
4
5
|
protected virtual void AddCurrentQueuedOperation() { this .QueuedCommands.Add(CurrentQueuedOperation); CurrentQueuedOperation = null; } |
标签:
原文地址:http://www.cnblogs.com/tyhj-zxp/p/4825631.html