我有两个设置为CQRS的服务 - 所以一个是仅查询服务,另一个是可以更改数据的命令服务。
服务由MongoDB支持,并且也是高度并行化的 - 一些查询使用聚合框架,并且聚合可能会消耗大量的连接 - 当它们实际上不断重新读取相同的数据时。
所以我认为Redis非常适合MongoDB上的缓存层。我使用CachingFramework.Redis
library,它建立在StackExchange.Redis
之上。
对于查询方法,我创建一个密钥对象,将其序列化为字符串,然后使用FetchHashedAsync
存储/获取结果。这似乎运行良好,我可以通过在发生写入时重建各种键来轻松更新/无效缓存项 - 当可以从主缓存项重建密钥时。
当这不容易实现时,事情变得更加复杂;也就是说,当我缓存一般过滤器查询的结果时。
到目前为止,我的方法是存储一个字典,将过滤器映射到从MongoDB中获取的对象的主键 - 并将它们存储在IRedisDictionary
实例中。
这似乎不是一个好主意 - 因为我看到了超时:
StackExchange.Redis.RedisTimeoutException occurred
HResult=-2146233083
HelpLink=http://stackexchange.github.io/StackExchange.Redis/Timeouts
Message=Timeout performing HSET audit:correlationIdByHash, inst: 2, mgr: ProcessReadQueue, err: never, queue: 609, qu: 0, qs: 456, qc: 153, wr: 0, wq: 0, in: 8192, ar: 1, clientName: WSP18652WN, serverEndpoint: Unspecified/localhost:6379, keyHashSlot: 12790, IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=9,Free=32758,Min=8,Max=32767) (Please take a look at this article for some common client-side issues that can cause timeouts: http://stackexchange.github.io/StackExchange.Redis/Timeouts)
Source=StackExchange.Redis
StackTrace:
at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server)
at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server)
at StackExchange.Redis.RedisDatabase.HashSet(RedisKey key, RedisValue hashField, RedisValue value, When when, CommandFlags flags)
at CachingFramework.Redis.RedisObjects.RedisDictionary`2.Add(TK key, TV value)
at CachingFramework.Redis.RedisObjects.RedisDictionary`2.set_Item(TK key, TV value)
at SPMO.Providers.Audit.AuditIndex.UpdateIndexImpl(Int32 keyHash, Guid correlationId) in E:\dev\SPMO\trunk\Providers\SPMO.Providers\Audit\AuditIndex.cs:line 61
InnerException:
有没有更好的方法来解决这个问题?