mf

Set Up Caching in PHP With the Symfony Cache Component

Today, I’ll show you the Symfony Cache component, an easy way to add caching to your PHP applications. This helps improve the overall performance of your application by reducing the page load time.

The Symfony Cache Component

The Symfony Cache component allows you to set up caching in your PHP applications. The component itself is very easy to install and configure and allows you to get started quickly. Also, it provides a variety of adapters to choose from, as shown in the following list:

  • database adapter
  • filesystem adapter
  • memcached adapter
  • Redis adapter
  • APCu adapter
  • and more

When it comes to caching using the Symfony Cache component, there are a couple of terms that you should get familiar with.

To start with, the cache item refers to the content which is stored. Each item is stored as a key-value pair. The cache items are managed by the cache pool, which groups them logically. In fact, you need to use the cache pool to manipulate cache values. Finally, it’s the cache adapter which does all the heavy lifting to store items in the cache back-end.

In this article, we’ll explore how you can unleash the power of the Symfony Cache component. As usual, we’ll start with installation and configuration, and then we’ll go on to explore a few real-world examples in the latter half of the article.

Installation and Configuration

In this section, we’re going to install the Cache component. I assume that you have already installed Composer in your system—you’ll need it to install the Cache component available at Packagist.

Once you have installed Composer, go ahead and install the Cache component using the following command.

That should have created a composer.json file that should look like this:

That’s it for installation, but how are you supposed to add it to your application? It’s just a matter of including the autoload.php file created by Composer in your application, as shown in the following snippet.

A Real-World Example

In this section, we’ll go through an example which demonstrates how you could use the Cache component in your applications to cache content.

To start with, let’s go ahead and create the index.php file with the following contents.

Let’s go through the main parts of the index.php file to understand their purpose.

Create the Cache Pool

As we discussed earlier, cached items are stored in a cache pool. Furthermore, each cache pool is backed by a specific cache back-end and adapter. If you want to store items in the file system cache, for example, you need to initialize the cache pool of the file system adapter.

You can provide three optional arguments to the FilesystemAdapter object:

  • the namespace in which you would like to create cache entries 
  • a lifetime in seconds for cache items
  • the directory in which the cache will be stored.

How to Store String Values

Since we’ve already created the cache pool, we can use it to store cache items.

Firstly, we use the getItem method to fetch the cache item with the demo_string key. Next, we use the isHit method to check if the value we’re looking for is already present in the cache item $demoString.

Since this is the first time we’re fetching the demo_string cache item, the isHit method should return false. Next, we use the set method of the $demoString object to set the cache value. Finally, we save the $demoString cache item into the $cachePool cache pool using the save method.

Now that we’ve stored the item in the cache, let’s see how to fetch it from the cache.

Here, we use the hasItem method to check the existence of the cache item in the cache pool before retrieving it. 

Next, let’s see how to delete all cache items from the cache pool:

How to Store Array Values

In the previous section, we discussed how to store basic values in the cache pool. Storing array values is much the same, as you can see in the following example.

As you can see, we can simply set the cache item with an array value, just the same as we did for a string. 

Next, let’s see how to delete the specific cache item from the cache pool.

Here, we use the deleteItem method to delete the demo_array item from the cache pool.

How to Set an Expiry Date for Cached Items

So far, we’ve cached items into the pool without an expiry date. However, you don’t typically want to store items in the cache permanently. For example, you might like to refresh cache items periodically, so you need a mechanism which purges expired cache items.

In this section, we’ll discuss how to store items in the cache along with an expiry date.

As you can see in the above snippet, you can use the expiresAfter method to set an expiry date for the cached item. You can pass the number of seconds you would like to cache an item for in the first argument of the expiresAfter method.

In our example, we use the sleep method to test if the cached item is still available in the cache pool.

Go ahead and test it to see how it works!

Conclusion

Today, we had a brief look at the Symfony Cache component, which allows you to set up caching in your PHP applications. It also supports a variety of caching adapters that together give you the flexibility to choose the kind of back-end you want to use.

Feel free to express your thoughts and queries using the form below.

Powered by WPeMatico

Leave a Comment

Scroll to Top