什么是ASP.NET中的模式和最佳实践的缓存? [英] What Are the Patterns and Best Practices for Caching in ASP.NET?

查看:99
本文介绍了什么是ASP.NET中的模式和最佳实践的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在研究一个大的遗留应用程序,我们正在重新设计业务层和数据层。我们认为,这是重新设计高速缓存的处理方式的好时机。是否有实现缓存层(或在业务层构建它)的任何模式和最佳实践?

We are working on a large legacy application and we're redesigning the business layer and the data layer. We believe that it is a good time to redesign the way cache is handled. Are there any patterns and best practices for implementing a caching layer (or building it in the business layer)?

这是我能想到的是使用SQL缓存依赖(因为我们已经有了有很多不总是符合业务对象表的一大遗留数据库,这是很难)唯一的东西和实施强类型的CacheManager类隐藏字符串键和消除铸造类的问题。

The only things that I can think of are to use SQL Cache Dependency (which is hard because of we've got a big legacy database with a lot of tables that do not always correspond to the business objects) and implementing strongly typed CacheManager class to hide the string keys and eliminate class casting problems.

还有什么更复杂的,我们可以做什么?有什么办法强制执行缓存失效进行更新时/删除?我们应该以某种方式维持高速缓存中的单个对象和检索数据库的ID列表总是以相同的对象工作的?

Is there anything more sophisticated that we can do? Is there any way to enforce cache invalidation when performing update/delete? Should we somehow maintain a single object in the cache and retrieve list of IDs from the DB that always work with the same objects?

基本上,你在ASP.NET中使用什么技术来缓存?太糟糕,我们不能使用IoC容器中,或支持缓存ORM框架:(

Basically what techniques for caching do you use in ASP.NET? Too bad we cannot use IoC containers or ORM frameworks that support cache :(

编辑:我更关心的是维护不是性能

I am more concerned about maintainability than performance.

推荐答案


  • 只需每QueryResult中保存到数据库中(使用缓存键:查询,价值:业务对象列表)

  • 使用分布式缓存像旁边memcached来ASP.Net缓存

  • 使用一个复杂的CacheManager像<一个href=\"https://github.com/enyim/memcached-providers\">https://github.com/enyim/memcached-providers;可以有高速缓存组。一些数据已被存储很长一段时间,一些短的时间。一些数据已被存储在ASP.Net缓存等

  • 请即必须使用的包装函数存储在缓存中如公共牛逼GetFromCache&LT电话; T&GT;(字符串键,Func键&LT; T&GT; ifKeyNotFoundDelegate)来保证缓存始终使用相同的。 [1]

  • 何时使用ASP.Net缓存,以及何时使用分布式缓存思考。被读取每个请求的数据应该存储在ASP.Net,大数据,如搜索结果;有很多不同的密钥和数据等的应memcached中。

  • 添加版本。 preFIX用的versionNumber所有按键,所以更新的Web应用程序时,你就不会遇到麻烦,有的objectcontracts变化。

  • Just save every queryresult to the database (with cache key: your query, value: your list of business objects)
  • Use distributed cache like memcached next to ASP.Net cache
  • Use a sophisticated cachemanager like https://github.com/enyim/memcached-providers; that can have cache-groups. Some data has to be stored for a long time, some short time. Some data has to be stored in ASP.Net cache, etc.
  • Do calls that has to be stored in the cache using a wrapper function like public T GetFromCache<T>(string key, Func<T> ifKeyNotFoundDelegate) to ensure that cache is always used the same. [1]
  • Think of when to use ASP.Net cache, and when to use distributed cache. Data that is read every request should be stored in ASP.Net, large data like search results; with a lot of different keys and data etc. should be in memcached.
  • Add versioning. Prefix all keys with a versionnumber, so you won't get in trouble when updating your web application, and some objectcontracts change.
  • 不错啊,这涵盖了大部分的我们在我们的网站做(20GB memcached的聚类S $ P $垫20服务器)。

    Ah well, that covers most of what we do in our website (20GB memcached cluster spread over 20 servers).

    [1]通过使这样的功能来存储的东西在缓存中唯一的接口,可以实现以下。比方说,我想用的东西从缓存,就像从一个函数的结果。通常你会做这样的事情。

    [1] By making such a function the only interface to store stuff in cache, you can achieve the following. Let's say I want to use something from the cache, like the result from a function. Normally you would do something like

    CacheManager cm = new CacheManager(CacheGroups.Totals);
    object obj = cm.GetFromCache("function1result");
    if(obj == null)
    {
        obj = (object)DAO.Foo();
        cm.StoreInCache("function1result", obj);
    }
    return (List<MyEntity>)obj;
    

    通过使用不同的接口可以确保用户不会犯了错误。

    By using a different interface you can ensure that users won't make a mistake here.

    如同

    public T GetFromCache<T>(string key, Func<T> ifnotfound)
    {
        T obj = this.GetFromCache(key) as T;
        if(obj == default(T)) 
        { 
             obj = ifnotfound.Invoke();
             this.StoreInCache(key, obj);
        }
        return obj;
    }
    

    这确保了


    1. 我们总是用正确的工作类型

    2. 您的用户始终与高速缓存相同的方式工作

    人机工程学:不太可能,他们犯了一个错误。此外:你获得更好,更清晰,code,如:

    Ergo: less probable that they make a mistake. Furthermore: you get nicer, more clear, code, like:

    List<MyEntity> list = new CacheManager(CacheGroups.Total).GetFromCache<List<MyEntity>>("function1result", ()=>DAO.Foo());
    

    这篇关于什么是ASP.NET中的模式和最佳实践的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆