使用Redis,MongoDB,ServiceStack进行.NET会话状态缓存

ikr*_*ain 2 distributed-caching mongodb redis servicestack

我一直在研究是否可以在外部Dbs(如Redis,MongoDb或其他高度可扩展的工具)中缓存.NET会话状态.

我的研究结果是,尽管MongoDB有更多的集成来完成这类工作,但似乎Redis的性能要高得多,而且还有更多的选项(密钥过期,集合等)可供使用.

还有另一个名为ServiceStack的框架,它有一个RedisClient的实现,但是恕我直言的实现方式比我想要的更加耦合.

 public override object OnGet(CachedOrders request)
    {
        var cacheKey = "some_unique_key_for_order";
        return base.RequestContext.ToOptimizedResultUsingCache(this.CacheClient, cacheKey, () =>
            {
                    //This delegate will be executed if the cache doesn't have an item
                    //with the provided key

                //Return here your response DTO
                //It will be cached automatically
            });
    }
Run Code Online (Sandbox Code Playgroud)

所以在这项研究之后,我想知道您的意见,以及您是否在任何应用程序中实现了这种缓存.你能分享一下你的经历吗?

谢谢!

myt*_*thz 5

ServiceStack的缓存没有耦合,该ToOptimizedResultUsingCache()方法只是一种方便的扩展方法,允许您在必要的最小样板中实现一个通用的缓存模式.该ToOptimizedResult方法基于IRequestContext中的MimeType和CompressionType返回最优化的结果.例如,在JSON服务中,它通常是JSON Response DTO的deflate'd输出.

您不必使用扩展方法,并且可以直接访问ICacheClient API,因为它是类中的自动连接属性ServiceBase.如果您需要比ICacheClient API更多的功能,我建议使用RedisServiceStack的C#RedisClient,它可以快速,原子地访问分布式comp-sci集合.

使用ICacheClientAPI 的好处是它是一个可测试的实现无关的缓存接口,目前有InMemory,Redis和Memcached提供程序.