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)
Summary:Http://www.cnblogs.com/dunitian/p/4822808.html#mvc
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