Cache
The `Cache` module in Lithe provides an efficient way to store and retrieve frequently accessed data, improving application performance by avoiding repeated operations on data that does not change often.
Installation
To install the lithemod/cache package, execute the following command in your project directory:
composer require lithemod/cache
For more details, you can check the source code at github.com/lithemod/cache.
Storing and Retrieving Cached Data
Storing Data in Cache
To store data in the cache, utilize the add method. This method requires a unique key, the data to be cached, an optional expiration time (in seconds, default is 3600 seconds), and an optional serialization format.
You can store data in two formats:
- JSON: Use this format for simple data structures like arrays or objects.
- Serialized: Use this format for complex data types, which can be stored and retrieved in their original state.
Here’s how to store data:
use Lithe\Support\Cache;
$app->post('/store-data', function ($req, $res) {
$data = $req->input('data'); // Assuming $data is an array
$cacheKey = 'data_key';
// Storing as JSON
Cache::add($cacheKey, $data, 3600, 'json');
// Or storing as serialized
// Cache::add($cacheKey, $data, 3600, 'serialize');
$res->send('Data stored in cache successfully! 🎉');
});
Retrieving Data from Cache
To retrieve cached data, use the get method. This method returns the stored data or null if the data is not found in the cache.
use Lithe\Support\Cache;
$app->get('/get-data', function ($req, $res) {
$cacheKey = 'data_key';
$data = Cache::get($cacheKey) ?? 'Data not found in cache. 😞';
$res->json(['data' => $data]);
});
Invalidating Cache Items
You can remove specific items from the cache using the invalidate method. This method can accept a single cache key or an array of keys, allowing for bulk invalidation.
use Lithe\Support\Cache;
// Invalidating a single item
$app->post('/invalidate-cache', function ($req, $res) {
$cacheKey = 'data_key';
Cache::invalidate($cacheKey);
$res->send('Cache invalidated successfully! 🗑️');
});
// Invalidating multiple items
$app->post('/invalidate-multiple-cache', function ($req, $res) {
$cacheKeys = ['data_key', 'another_key'];
Cache::invalidate($cacheKeys);
$res->send('Multiple cache items invalidated successfully! 🗑️');
});
Retrieving Data or Executing a Callback
The remember method allows you to either retrieve data from the cache or execute a callback function to generate the data if it is not found in the cache.
use Lithe\Support\Cache;
$app->get('/settings', function ($req, $res) {
$settings = Cache::remember('app_settings', function() {
return ['theme' => 'dark', 'language' => 'en'];
});
$res->json($settings);
});
Final Thoughts
With the Cache module, you can optimize your application’s performance by effectively managing cached data. You can choose between JSON and serialized formats for storing data based on your needs. Make sure to define a suitable cache strategy based on your application's specific needs and data access patterns. Happy coding! 🚀