How to handle cache data in Laravel

A cache is defined as a short-term computer memory where information is stored for easy retrieval. This means that this information doesn’t have to be recreated every time the request is made.

Laravel provides a cache() function to help store desired data in memory, fetch it when needed, and remove it when it is no longer needed, which means a faster response time to requests to applications.

cache()->remember()

cache()->remember() is used to store some data in memory.

The definition of remember() is shown below.

function remember($key, $ttl, Closure $callback)

remember() takes in a unique key ($key). This is to be able to differentiate between all the cached data, and also to be used to fetch the data when needed.

It also takes in time duration ($ttl), which tells it how long to store this data for (usually in seconds). But you may use the now() helper to avoid stress.

Finally, it takes a callback ($callback), where the data to be stored is created - or processed.

Example

To store an array of registered users for 2 days:

$users = cache()->remember('app-users', now()->addDays(2), function () {
        return User::all();
    });

cache()->rememberForever()

This works just like cache()->remember(), except that it doesn’t take in duration and caches the data forever, unless it is deleted.

rememberForever() is defined as follows.

function rememberForever($key, Closure $callback);

Example

To store an array of accepted currencies forever:

$accepted = cache()->rememberForever('app-users', function () {
        return ['usd', 'gbp'];
    });

cache()->get()

To get some cached data, pass its key to cache()->get().

get() is defined as follows.

function get($name);

All it takes in is the unique key that is used in the remember() method.

Example

To get all the stored registered users:

$users = cache()->get('app-users');

cache()->delete()

To remove cached data before its expiration, you can delete it by passing its unique key to the cache()->delete() function.

The definition of delete() shows that it only takes the key of the data to be deleted.

function delete($key);

Example

To delete the cached list of registered users:

$users = cache()->delete('app-users');

cache()->deleteMultiple()

Deleting each cached entry might be a long process if you have cached many things. Use cache()->deleteMultiple() to delete multiple caches by passing their unique keys as an array.

function deleteMultiple($keys);

Example

To delete the cached list of users and posts:

$cache = cache()->deleteMultiple(['app-users', 'app-posts']);

cache()->clear()

clear() takes in no parameters and clears the cache of the application.

Example

Clear all the application cache:

$cache = cache()->clear();

cache()->has()

To check if data has been cached, or still exists, use has(). Pass its unique key to cache()->has() to check. has() returns a Boolean.

function has($key);

Example

Check if users have been cached:

return cache()->has('app-users');

Free Resources