Explore ASP. NET MVC5 series ~~~ 5. cache (page cache + secondary cache), asp. netmvc5

Source: Internet
Author: User

Explore ASP. NET MVC5 series ~~~ 5. cache (page cache + secondary cache), asp. netmvc5

In fact, any knowledge points in any materials do not matter, it is not important, the important thing is the learning method, self-exploration process (if not, please correct me)


Demo in this Chapter: Bytes


This is a relaxing article. We don't need to worry about security. Caching is not exclusive to MVC. It was available N years ago. I would like to briefly introduce what is used in projects (Redis, Memcache, and so on)

1. Page cache:


First, the most common one is:[OutputCache (Duration = 100)]The Duration unit is second.

After returning to the breakpoint for the first time, no A () in the Controller will be executed for any refresh within 100 seconds.

Cache with Parameters

[OutputCache (Duration = 100, VaryByParam = "id")] [OutputCache (Duration = 100, VaryByParam = "id; name")]

First access to cache/B? When id = 1, the breakpoint stops, and the breakpoint does not enter within S. When id = 2, the breakpoint is re-opened. Multiple parameters are similar. Note that the parameter segmentation is;


Bytes ---------------------------------------------------------------------------------------------------

2. Second-level cache: (System. Web; System. Web. Caching ;)

2.1 absolute expiration Cache

HttpRuntime. Cache. Add (key, list, null, DateTime. Now. AddSeconds (60), Cache. NoSlidingExpiration, CacheItemPriority. Default, null ); 

2.2 relatively expired Cache(A Bit Of Session feeling)

HttpRuntime. Cache. Add (key, list, null, Cache. NoAbsoluteExpiration, new TimeSpan (0, 0, 10), CacheItemPriority. Default, null );

2.3 database dependent Cache

1. Start the database cache dependency: Open it as an administratorVS developer commands(Parameter Details: https://msdn.microsoft.com/zh-cn/library/ms229862)

Aspnet_regsql-C "data source = xxx; initial catalog = xxx; uid = xxx; password = xxx;"-ed-et-t "table name"

If VS is not installed, open cmd with the Administrator and switch to the following directory: (v4.0.xxx may be different)

C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319

C: \ Windows \ Microsoft. NET \ Framework64 \ v4.0.30319

In fact, we can see that his internal implementation is implemented through triggers, so it is not as efficient:



2. Configure WebConfig:

<! -- Database cache dependency configuration Start --> <caching> <! -- PollTime, in milliseconds, indicates that the database is detected every 10 seconds, check whether the table has changed --> <sqlCacheDependency pollTime = "10000"> <databases> <add name = "MySQLCacheDB" connectionStringName = "cacheConn"/> </databases> </sqlCacheDependency> </caching> <! -- Database cache dependency configuration End -->

Code:HttpRuntime. Cache. Add (key, list, new SqlCacheDependency ("SQLCacheDB", "SeoTKD"), Cache. NoAbsoluteExpiration, Cache. NoSlidingExpiration, CacheItemPriority. Default, null );

Generally, the application scenario is to cache the data in the database dependent table. When the data in the database changes, the cache becomes invalid, so that the cache can be updated automatically in a timely manner.(Here is just a demonstration)

Before data is updated:

No updates are refreshed in the dead.

After the table is updated:

Others:File (folder) dependent Cache, Generally someWebsite fixed configurationIt is usually used to determine the time difference based on the last modification time of the file.

For example:

HttpRuntime. cache. add (key, list, new CacheDependency (Request. mapPath ("/App_Data/Config. xml "), Cache. noAbsoluteExpiration, Cache. noSlidingExpiration, CacheItemPriority. default, null );


Change content:

Extra-curricular expansion:Http://www.cnblogs.com/knowledgesea/p/3904929.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.