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') .
- 09.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes
- 07.07.22IT / Safety How to protect PHP, JS, HTML, CSS source code - obfuscation, minification, compression and encryption
- 06.07.22IT / Safety Connection not secure, problem with Lets Encrypt - how to fix expired 09/30/2021 DST Root CA X3, remove it manually and install ISRG Root X1. Example on MS Windows 7
- 08.07.21IT / Misc How to make a free translation for a website without an API, translate documents in Google Translate
- 06.07.21IT / Misc How to make a subscription button on a website, a subscriber base and automatic mailing