简体   繁体   English

缓存和ASP.NET

[英]Caching and ASP.NET

I have a question that is more related to how ASP.NET works and will try my best to explain so here goes. 我有一个与ASP.NET的工作方式更相关的问题,将尽我所能解释。

I have a four tiered architecture in my app 我的应用程序中有一个四层体系结构

  1. Web (ASP.NET Web Application) Web(ASP.NET Web应用程序)
  2. Business (Class Library) 商业(类库)
  3. Generic CRUD layer ** (Class Library) 通用CRUD层**(类库)
  4. Data (Class Library) 数据(类库)

This generic CRUD layer of mine uses reflection to set and read properties of an object, however I have read that using PropertyInfo is fairly expensive and thus want to cache these items. 我的这个通用CRUD层使用反射来设置和读取对象的属性,但是我已经读到,使用PropertyInfo相当昂贵,因此想缓存这些项目。

Here is the scenario: 这是场景:

Two people access the site, lets call them Fred and Jim. 有两个人访问该站点,因此称他们为Fred和Jim。 Fred creates a Customer which in turn called the Generic CRUD layer and caches the property info of the Customer class within System.RuntimeCache. 弗雷德创建了一个客户,该客户又称为通用CRUD层,并将客户类的属性信息缓存在System.RuntimeCache中。 Jim, then seconds later also creates a Customer. 吉姆,然后在几秒钟后还创建了一个客户。

My question is will the two requests from both Fred and Jim cause the obtaining of propery info to be triggered twice? 我的问题是,弗雷德(Fred)和吉姆(Jim)的两个请求是否会导致两次获取属性信息? Or will ASP.NET retrieve it from cache the second time, ie Jim's request is quicker as property info is obtained via the cache? 还是ASP.NET会第二次从缓存中检索它,例如,通过缓存获取属性信息,Jim的请求会更快?

My thinking is that because my CRUD is a class library and not having access to System.Web.Cache, the property info won't be cached across all sessions / users? 我的想法是,因为我的CRUD是一个类库,并且无法访问System.Web.Cache,所以属性信息是否不会在所有会话/用户之间进行缓存?

No, it will issue new queries for each request (unless you've coded otherwise). 不,它将为每个请求发出新查询(除非您另有编码)。

There are multiple layers of caching that could happen in ASP.Net application (browser, proxies, server side response caching, intermediate objects caching, DA layer caching) which all can be configured/used. ASP.Net应用程序中可能会发生多层缓存(浏览器,代理,服务器端响应缓存,中间对象缓存,DA层缓存),所有这些都可以配置/使用。 But nothing is ever cached in ASP.Net (or in any application) unless one specifically wrote code/rules/configuration to do so. 但是,除非有人专门编写代码/规则/配置,否则任何内容都不会缓存在ASP.Net(或任何应用程序)中。

As Alexei Levenkov points out, you have to configure caching to happen explicitly: you're not going to get automatic caching of specific property values. 正如Alexei Levenkov指出的那样,您必须配置缓存以显式进行:您不会获得对特定属性值的自动缓存。

But let me point out that while using PropertyInfo is expensive compared to writing code to directly access properties, it pales in comparison to the cost of a database round-trip or the latency between your server and your end user. 但是,我要指出的是,尽管与编写直接访问属性的代码相比,使用PropertyInfo昂贵,但与数据库往返的成本或服务器与最终用户之间的延迟相比,它显得苍白无力。 Tools like Entity Framework, WebForms, and MVC all make extensive use of reflection because the performance cost it incurs is totally worth the reduced maintenance costs. 诸如Entity Framework,WebForms和MVC之类的工具都大量使用了反射,因为它所产生的性能成本完全值得减少维护成本。

Avoid premature optimization. 避免过早优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM