Caching - create, load and reset. Where to store the cache, methods and types of caching
Caching is an integral part of any modern system. It helps to get rid of unnecessary operations, and instead use ready-made, once calculated results. Caching can significantly increase system speed and overall performance, as well as reduce the load on the server , which means savings on hosting fees.
How to make website caching
If a ready-made system (CMS or framework) is used, then it usually has special tools for working with the cache. In the case of your own development, you can provide in the system a special API for working with the cache. It can be a single class or a collection of methods.
What to cache ? Code sections that are difficult to calculate are subject to caching. For example, it can be the formation of a large list of materials. A concrete example is a block with a list of the latest articles on the site, which is enough to create once and update only when new materials have appeared on the site.
Where to store the cache ? Cache storage locations depend on the specific needs of the web application, the choice of the method should be made in each specific case individually. It could be:
- special table in the database ;
- special extension for the programming language used (for example, Memcached for PHP) ;
- keeping the cache in regular files, which is the easiest way .
Methods or types of caching can be as follows:
- client caching (caching of different files, protocols, CA cache, page cache) ;
- server caching (caching of pages or their individual blocks, cache of programming language calculation results, caching at the database level) .
How do I create a cache ? The cache can be created automatically or manually. Automatic cache creation occurs when trying to load from the cache, a sample code will be shown below. In the case of the manual mode of creating a cache, at the end of the required section of the code, a code is added that saves the results obtained in the cache store, example code:
add_cache ('section_name', 'name', $ data) .
Load from cache
Loading from the cache is straightforward. In this case, an automatic operation of creating a cache can be implemented - when a page, block or other entity is requested. If there is no cache, the calculation is performed and the results are stored in the cache, after which, at the next request, a check is performed - if there is a cache, loading from the cache is performed:
load_cache ('section_name', 'name', $ data) .
Timely cache reset is also important, otherwise visitors will see irrelevant and old information. At the same time, the cache should not be flushed too often, or it would incur unnecessary computation overhead. Sample code to reset the cache completely:
clear_cache () .
Also, clearing the cache can be performed not completely, but partially, by sections. Flushing the cache by partitions allows you to flush only the necessary sections of the cached data and does not cause unnecessary re-creation of the cache of the rest of the data that has not been changed. This is done by calling methods in your code after specific operations. For example, a new material has been added, so you need to reset the entire cache related to the "content" section:
clear_cache ('section_name') .
- IT / Misc 08.07.21 How to make a free translation for a website without an API, translate documents in Google Translate
- IT / Misc 06.07.21 How to make a subscription button on a website, a subscriber base and automatic mailing
- Food / Misc 06.07.21 How to quickly cook delicious fried pies with potatoes and onions
- IT / Misc 04.07.21 Caching - create, load and reset. Where to store the cache, methods and types of caching
- IT / Database 03.07.21 Custom NoSQL - storing data in files and not only in a database. Storing settings, small data and caching files