首页 文章

使用ServiceStack OrmLite和SQL Server持久性进行Redis缓存

提问于
浏览
3

我们有一个带有SQL Server后端的Web应用程序(ASP.NET / C#) . 我们使用ServiceStack OrmLite作为我们的POCO Micro ORM . 我们现在想扩展我们的应用程序的一部分来缓存频繁读取的数据(主要是POCO对象的集合作为值,使用数字键) . 但我不确定如何集成一个简单的缓存解决方案(基于内存或基于Redis),它与OrmLite和MSSQL无缝地作为主数据库 .

我已经阅读了ServiceStack Redis客户端,MemoryCacheClient和多嵌套数据库连接(OrmLiteConnectionFactory),但我找不到任何示例,教程或代码示例,以了解有关实现与OrmLite一起使用的缓存的更多信息 .

任何建议或链接将是有帮助的,非常感谢 .

3 回答

  • 3

    您需要自己实现缓存逻辑,但它没有多大工作 - 这是一个伪代码示例:

    public class QueryObject
        {
            public DateTime? StartDate { get; set; }
            public string SomeString { get; set; }
        }
    
        public class Foo
        {
            public DateTime DateTime { get; set; }
            public string Name { get; set; }
        }
    
        public class FooResponse
        {
            public List<Dto> Data { get; set; }
        }
    
    
        public FooResponse GetFooData(QueryObject queryObject)
        {
            using (var dbConn = connectionFactory.OpenDbConnection())
            using (var cache = redisClientsManager.GetCacheClient())
            {
                var cacheKey = string.Format("fooQuery:{0}", queryObject.GetHashCode()); //insert your own logic for generating a cache key here
                var response = cache.Get<Response>(cacheKey);
    
                //return cached result
                if (response != null) return response;
    
                //not cached - hit the DB and cache the result
                response = new FooResponse()
                    {
                        Data =
                            dbConn.Select<Foo>(
                                x => x.DateTime > queryObject.StartDate.Value && x.Name.StartsWith(queryObject.SomeString)).ToList()
                    };
                cache.Add(cacheKey, response, DateTime.Now.AddMinutes(15)); //the next time we get the same query in the next 15 mins will return cached result
                return response;
    
            }
        }
    
  • 4

    我使用此扩展来帮助简化db和缓存之间的集成 .

    public static class ICacheClientExtensions
    {
        public static T ToResultUsingCache<T>(this ICacheClient cache, string cacheKey, Func<T> fn, int hours = 1) where T : class
        {
            var cacheResult = cache.Get<T>(cacheKey);
            if (cacheResult != null)
            {
                return cacheResult;
            }
            var result = fn();
            if (result == null) return null;
            cache.Set(cacheKey, result, TimeSpan.FromHours(hours));
            return result;
        } 
    }
    
    public class MyService : Service
    {
        public Data Get(GetData request)
        {
            var key = UrnId.Create<Data>(request.Id);
    
            Func<Data> fn = () => Db.GetData(request.Id);
    
            return Cache.ToResultUsingCache(key, fn);
        }
    
        [Route("/data/{id}")]
        public class GetData: IReturn<Data>
        {
            public int Id{ get; set; }
        }
    }
    
  • 2

    你检查过Service stack caching wiki . 它提供了有关缓存的详细信息 . 现在根据您提供的详细信息,我可以说您可以进行任何类型的缓存 . 截至目前,它没有任何区别 .

    PS:当应用程序中没有选项或唯一待处理的东西时,应该进行一条建议缓存 . 因为它带来了它自己的问题是使缓存,管理和所有这些无效 . 所以,如果你的应用程序不是太大,那就暂时离开吧 .

相关问题