The author has translated two articles about caching!

Source: Internet
Author: User

Article 1Article

I recently read some cached technical articles and translated some paragraphs to find that many things are so difficult to express in Chinese.

Caching

Caching is one of the best techniques you can use to improve performance. Use

Caching to optimize reference data lookups, avoid network round trips, and avoid

Unnecessary and duplicate processing. To implement caching you need to decide

When to load the cache data. Try to load the cache asynchronously or by using

Batch process to avoid client delays.

Cache Technology

Cache is used to improve applicationsProgramOne of the best performance methods. Cache can be used to optimize data query, avoid unnecessary network data return, and avoid unnecessary identical data processing logic. When implementing the cache, we need to determine when to load the cache data. Use asynchronous loading of cache or batch processing to avoid client data delay.

When you design for caching, consider the following recommendations:

When designing a cache, consider the following:

Decide where to cache data. Determine the cache Data Structure

Decide what data to cache. Determine what data to cache

Decide the expiration Policy and scavenging mechanic. Determine cache expiration rules and clear

Decide how to load the cache data. Determine how to load the cache data

Avoid distributed coherent caches.

 

Decide where to cache data determine the cache Data Structure

Cache state where it can save the most processing and network round trips. This

Might be at the client, a proxy server, your application's presentation logic, business

Logic, or in a database. Choose the cache location that supports the lifetime you want

For your cached items. If you need to cache data for lengthy periods of time, you

Shocould use a SQL Server database. For shorter cache durations, use in-memory

Caches.

The processing result and network status that can be saved in the cache. These may be the client, proxy service, general application logic, business logic or database. Select Local cache support-if you want to make the cache have a longer life cycle, you can use the SQL Server database, and a short period of time can be cached in the memory.

Consider the following scenarios: consider the following items:

Data caching in the presentation layer. Consider caching data in the presentation

Layer when it needs to be displayed to the user and the data is not cached on per-

User basis. For example, if you need to display a list of states, you can look these

Up once from the database and then store them in the cache.

For more information about ASP. NET Caching Techniques, see Chapter 6,

"Improving ASP. NET performance ."

Performance layer cache. When the data is to be displayed as a guest user or a single user data is not cached, consider caching at the presentation layer. For example, if we want to display a status list, we can query the database only once and then cache them

Data caching in the business layer. You can implement caching mechanisms

By using hash tables or other data structures in your application's business logic.

For example, you cocould cache taxation rules that enable tax calculation. Consider

Caching in the business layer when the data cannot be efficiently retrieved from

The database. Data that changes frequently shoshould not be cached.

Service layer cache. You can use hash tables or other data structures in the application business logic to implement the cache mechanism. For example, you can cache tax collection rules to calculate the tax rate. The service layer cache should be considered when the database data cannot be efficiently retrieved. Data with frequent changes cannot be cached.

Data caching in the database. cache large amounts of data in a database and

When you need to cache for lengthy periods of time. The data can be served in

Smaller chunks or as a whole, depending on your requirements. The data will be

Cached in temporary tables, which consume more RAM and may cause memory

Bottlenecks. You shoshould always measure to see whether caching in a database is

Hurting or improving your application performance.

Database cache. When the data is large and persistent, the data should be cached in the database. You can save part or all of the data as needed. When the memory usage is high or causes memory bottlenecks, data can be cached in a temporary table. It is always necessary to measure whether data is cached in the database to reduce or increase the performance of applications.

 

Decide what data to cache determine what data to cache

Caching the right data is the most critical aspect of caching. If you fail to get this

Right, you can end up cing performance instead of improving it. You might end

Up consuming more memory and at the same time suffer from cache misses, where

The data is not actually getting served from cache but is refetched from the original

Source.

Correct data caching is a critical point of the caching mechanism. If the design is not correct, the performance of the application will be reduced rather than improved. You will consume more memory and miss the cache for most of the time. In fact, data is not read from the cache but is re-read from the data source.

Article 2

ASP. NET Cache Technology

A large number of website pages are dynamically created based on different requests submitted by users. As we know, dynamic pages help provide customized dynamic content according to user requirements. Dynamic pages are also used to obtain information updated every moment in the database. The disadvantage is that the system overhead is added for generating the same page for each user request.

To overcome this problem, some websites use the page generation engine to generate HTML static pages for all pages. However, the page generated in this way has the same request content for all users.

ASP. NET provides the caching technology to help us solve this problem to the greatest extent possible. It can cache the output page, save it in the memory, and cache the content requested by the user. The cache features can be customized according to different methods.

Cache a page
To Cache the output content of a page, specify an @ outputcache command at the top of the page. The syntax is as follows:

<% @ Outputcache duration = 5 varybyparam = "NONE" %>

As you can see, this command has two attributes. They are:

Duration-the number of seconds the cached output content is valid. After the expiration time exceeds the specified validity period, the expired cache content will be deleted and the page will be called again in the next user request to generate the cache content. The process repeats after 10 seconds of cached content again.
Varybyparam-this attribute is required and indicates that different query string parameters cause cache changes. AboveCodeIn the section, we specify that the varybyparam attribute is "NONE", which means that no matter how different query string parameters are passed, the returned page content is the same.
If two requests on the same page contain different query string parameters, for example,.../pagecachingbyparam. aspx? Id = 12 and.../pagecachingbyparam. aspx? Id = 15, which should generate different page content. The command should be:

<% @ Outputcache duration = 10 varybyparam = "ID" %>

The duration specified by the cached Duration Attribute for each page content of these two different requests ..

To specify multiple parameters, use semicolons to separate parameter names. If we specify the varybyparam attribute as "*", the cached content varies depending on the passed query string parameters.

Some dynamic pages must generate different content based on different views. In this case, it is necessary to specify that the cached output content varies with different browsers. @ Outputcache command should be changed:

<% @ Outputcache duration = 5 varybyparam = "ID" varybycustom = "Browser" %>

This command not only makes the cached output content different from the browser, but also varies with the browser version, for example, ie5, ie 6, Netscape 4, and Netscape 6 All obtain different output cache versions.

Cache local page
Sometimes we just want to cache a small part of a page. For example, we may use a page with the same content for all browsing users. This page has a title. This title may consist of a text/image, and data may change every day. In this case, we only want to cache the validity period of the title for one day.

The solution is to put the title content in a user control and specify that the user control should be cached. This technology is called fragment caching ).

To specify the user control to be cached, we use the @ outputcache command, just like the usage of the entire page cache.

<% @ Outputcache duration = 10 varybyparam = "NONE" %>

In the preceding command, the cache validity period of the user control is the time specified by the Duration Attribute (10 seconds ). The cached output content is the same regardless of the query string and browser type/version ..

Data Cache
ASP. NET also supports caching object-type data. We can store objects in the memory and use them on different dynamic pages of our applications. This feature can be achieved using the cache class. The cache lifecycle is the same as that of the application. Objects can be stored in the cache as name value pairs. Insert a string into the cache as follows:

Cache ["name"] = "Smitha ";

The stored string value can be obtained as follows:

If (Cache ["name"]! = NULL)
Label1.text = cache ["name"]. tostring ();

To insert an object into the cache, you can use the add method of the cache class or the insert method of different versions. These methods may allow us to use the more powerful functions provided by this cache class. Below is an overload of the insert method:

Cache. insert ("name", strname,
New cachedependency (server. mappath ("name.txt "),
Datetime. Now. addminutes (2), timespan. Zero );

The first two parameters are the key name and the object to be inserted. The third parameter is the cachedependency type. It helps us set the value dependency for the name.txt file. Therefore, once the file changes, the value in the cache will be deleted. We can specify the "null" value to indicate that there is no dependency. The fourth parameter specifies the time when the value is deleted from the cache. The last parameter is the variable cache expiration time, which specifies the interval from the last cache access time to the time when it is deleted.

When there is not much available system memory, the cache automatically deletes a few items from the memory. This process is called scavenging ). We can specify a priority for the items added to the cache so that some items can be prioritized:

Cache. insert ("name", strname,
New cachedependency (server. mappath ("name.txt "),
Datetime. Now. addminutes (2), timespan. Zero,
Cacheitempriority. High, null );

The enumerated type cacheitempriority sets different priorities for members. Cacheitempriority. High assigns a high priority to the project to reduce the possibility of being deleted by the cache.

Key Points
If you are still an old ASP page and cache the page output using the response. expires attribute, they can also be retained like ASP. NET supports this attribute.
The insert method of the cache class overwrites any existing project with the same key name.
The cacheitempriority. notremovable priority value can be used together with the cache. insert method to set the priority of a project so that the project will not be deleted from the Cache during purification.

Conclusion
In this article, I try my best to provide a general summary of multiple options of ASP. NET cache technology. To keep the summary of this article without detailed explanations and instructions.

Local caching is available in a nested style with sub-controls that enable caching. I have not tested how to use this, so there is no description of this technique. The insert method of the cache class is not discussed here. I hope this article is a good start for readers to study the colorful ASP. NET world.

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.