Cache

Understanding Caching in Enma

When using the Enma library, it's beneficial to understand how caching works and how it impacts the performance of your applications. Caching is a crucial feature in Enma that significantly enhances efficiency and speed by storing the results of operations temporarily. This means that when you request the same data multiple times, instead of repeatedly querying the source (like an API), the library can retrieve the data from its cache, reducing load times and network traffic.

How Caching Affects Performance

Caching in Enma is implemented to improve the responsiveness of your applications by reducing the frequency of network requests to manga repositories. For example, if you're developing an application that frequently needs to fetch the latest chapters of a particular manga, caching these requests means that after the first retrieval, subsequent requests will be served from the cache until the cache expires. This reduces the wait time for users and decreases the load on the external APIs, ensuring faster and more reliable performance.

Configuring Cache Settings

By default, Enma comes with predefined cache settings that are optimized for general use. However, you might find that your application has specific needs, such as longer or shorter cache durations depending on the frequency of content updates or user behavior patterns.

You can easily adjust the cache behavior in Enma by setting environment variables. This flexibility allows you to control how long data stays in the cache without needing to modify the code directly.

Cache Expiration Configuration

The duration that data remains in the cache can be controlled through environment variables. For example, if you wish to change how long chapter information is cached in the Mangadex repository, you can set the following environment variable:

  • ENMA_CACHING_FETCH_SYMBOLIC_LINK_TTL_IN_SECONDS

This variable determines the Time-To-Live (TTL) in seconds for caching the results of fetching chapters by symbolic links. Setting this variable to a higher value means that the cached data will be valid for a longer period, while a lower value means the data will expire quicker.

To change the cache duration, you can set the environment variable in your deployment environment or directly in your development environment:

export ENMA_CACHING_FETCH_SYMBOLIC_LINK_TTL_IN_SECONDS=1800  # Sets the cache duration to 30 minutes

By tuning these environment variables, you can optimize the caching strategy to fit the specific needs of your application, balancing between data freshness and performance efficiency.

Available Cache Settings

KeyDefault

ENMA_CACHING_FETCH_SYMBOLIC_LINK_TTL_IN_SECONDS

100

ENMA_CACHING_PAGINATE_TTL_IN_SECONDS

100

ENMA_CACHING_SEARCH_TTL_IN_SECONDS

100

ENMA_CACHING_GET_TTL_IN_SECONDS

300

ENMA_CACHING_AUTHOR_TTL_IN_SECONDS

100

Summary

Caching is a powerful feature in Enma that helps optimize the performance of your applications by reducing the number of API requests, which is particularly beneficial when dealing with rate-limited APIs. Adjusting cache settings through environment variables provides a flexible way to adapt the caching behavior to best suit your application's requirements. This ensures that your application can operate more efficiently while reducing the load on both your servers and the external services.

Last updated