Enable Dark Mode!
how-to-apply-cache-on-a-website.jpg
By: Swathy KS

How to Apply Cache on a Website?

Technical

Caches are essential for speeding up systems worldwide. Storing frequently accessed data closer to users reduces latency and alleviates strain on backend resources, resulting in faster response times and enhanced user experiences. From internet browsing to finance and healthcare, caching plays a vital role in optimizing efficiency and ensuring swift access to critical information. With the rise of edge computing and IoT, caching is becoming even more indispensable for delivering content and services globally, bolstering performance and scalability.

Improving the performance of websites requires the introduction of LRU (Least Recently Used) caching techniques. LRU cache minimizes server load and expedites page loading by keeping frequently visited content, such as articles and photos, in memory. It works on the tenet of removing the least used content while maintaining accessibility to relevant content. This increases user pleasure, speeds up the website, and uses less server resources.

How to add LRU cache?

First of all, install the cache tools package.

pip install cachetools

Implement the following lines in the cache applied python file.

import cachetools
cache = cachetools.LRUCache(maxsize=2048)

* cachetools: This is the name of the module being imported, which likely contains various caching utilities and classes.

* LRUCache: To generate the cache object, this particular class is being used. When the cache fills up to its maximum, things that haven't been used in the last few seconds are removed from the LRU cache.

* maxsize=2048: The maximum number of items that the cache can hold is specified by this option. In this instance, the cache's maximum size is set at 2048 items. The least recently used items will be deleted from the cache to create room for new things once it reaches this limit.

Create a cache key 

Make a unique cache key for identifying pages, like template id, content, etc.

In an LRU Cache, each data entry is assigned a unique cache key. This key serves as the identifier for the stored item, allowing for efficient retrieval and management. When the cache reaches capacity, the key of the least recently used item is used to determine which entry to remove, ensuring optimal performance and resource utilization.

Eg : cache_key = f"{values}.{template_key}.{lang}.{write_date}”

Get data from Cache

cache_result = cache.get(cache_key)

* cache: This refers to the cache object, which seems to be an instance of an LRU (Least Recently Used) cache initialized earlier in the code.

* get(cache_key): On the cache object, this is a method call. When a value (cache_key) is supplied, the.get() method is used to obtain it from the cache. None is returned if the cache has no value linked to the key.

* cache_key: To store and retrieve values from the cache, use this key. The template, language, and write date are among the several aspects that determine its generation.

Set data on cache

cache[cache_key] = content

* cache: This is an LRUCache object created using cache tools. It's a Least Recently Used (LRU) cache, which means that if the cache reaches its maximum size (maxsize), it will evict the least recently used items to make space for new ones.

* cache_key: This is a string that uniquely identifies the data stored in cache.

* content: This variable holds the rendered content of the template. Stored the actual content that was stored for reducing page rendering time.

* cache[cache_key]: This line assigns the content to the cache with the key cache_key. This means that the next time the same template with the same key is requested, the content will be retrieved from the cache instead of re-rendering the template. This helps improve performance by avoiding redundant rendering operations.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message