Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. PHP

Boost Your Website Performance With PhpFastCache


In this article, we're going to explore the PhpFastCache library, which allows you to implement caching in your PHP applications. Thus, it helps to improve overall website performance and page load times.

What Is PhpFastCache?

PhpFastCache is a library that makes it a breeze to implement caching in your PHP applications. It's an easy-to-use and yet powerful library that provides several APIs to help you implement a caching strategy of your choice without much hassle.

Don't make the mistake of assuming that it's merely a traditional file system caching scheme. In fact, PhpFastCache supports a plethora of adapters that let you choose from high-performance back-ends like Memcache, Redis, MongoDB, CouchDB, and others.

Let's have a quick look at a couple of the most popular adapters:

  • file system
  • Memcache, Redis, and APC
  • CouchDB and MongoDB
  • Zend Disk Cache and Zend Memory Cache

If you don't find your choice of adapter in the above list, you could easily develop a custom driver that plugs into the system and works effortlessly.

In addition to the basic functionality, the PhpFastCache library also provides an event mechanism that allows you to respond to certain predefined events. For example, when something is deleted from the cache, you could catch this event and refresh or delete related data as well.

In the upcoming sections, we'll go through the installation and configuration of PhpFastCache, along with the demonstration of a few examples.

Installation and Configuration

In this section, we'll go through the installation and configuration of the PhpFastCache library. There are different ways you could approach this in your project.

If you just want to download the .zip or .tar.gz version of the library without much hassle, you could go ahead and grab it from the official site.

On the other hand, you could install it as a Composer package as well. That should be the preferred way as it makes maintenance and upgrading easier in the future. If you haven't installed Composer yet, you'll have to do that first.

Once you've installed Composer, let's go ahead and grab the PhpFastCache library using the following command.

Upon the successful completion of that command, you should have the vendor directory in place, which contains everything you need to run the PhpFastCache library. On the other hand, if you're missing any libraries or extensions required by the PhpFastCache library, Composer will ask you to install them first.

You should also find the composer.json file that looks like this:

No matter the way you've chosen to install the PhpFastCache library, the only thing that's necessary is to include the autoload.php file in your application to kick things off.

If you're using the Composer-based workflow, autoload.php is located under the vendor directory.

On the other hand, if you've downloaded the .zip or .tar.gz package, autoload.php should be available at src/autoload.php.

And with that, you're all set to start caching and gaining the benefits of the amazing PhpFastCache library. In the next section, we'll go through a couple of practical examples that demonstrate how to use PhpFastCache in your application.


I've already mentioned that the PhpFastCache library supports various adapters when it comes to caching. In this section, I'll demonstrate using the file system and Redis adapters.

Caching Using the Files Adapter

Go ahead and create the file_cache_example.php file with the following contents. I assume that you're using the Composer workflow and thus the vendor directory is at the same level as that of file_cache_example.php. If you've installed PhpFastCache manually, you can change the file structure accordingly.

Let's go through this to understand what each piece of code stands for. The first obvious thing is to include the autoload.php file and import the namespace that we intend to use.

When you're using the files cache, you're supposed to provide the directory path that holds files generated by the caching system. And that's what exactly we've configured in the following snippet.

Of course, we need to make sure the cache directory exists and it's writable by the web server.

Next, we instantiate the cache object and try to load the cached item with the welcome_message key.

If the item doesn't exist in the cache, we'll add it to the cache for 60 seconds and display it from the cache. On the other hand, if it exists in the cache, we'll just fetch it!

That was a fairly easy setup, wasn't it? In fact, you can go ahead and run the file to check the results!

When you run it for the first time, you should see the following output:

In the next run, the output looks something like this:

So that was file system caching at your disposal. In the next section, we'll mimic the same example using the Redis cache adapter.

Caching Using the Redis Adapter

Before we move ahead, I assume that you've already installed the Redis server and it's running on port 6379, which is the default port for Redis.

With that set up, let's go ahead and create the redis_cache_example.php file with the following contents.

As you can see, the file is pretty much the same, except the section that initializes the configuration specific to the Redis adapter.

Of course, you should change the host and port settings to match your requirements if you're running a Redis server other than localhost.

Go ahead and run the redis_cache_example.php file to see how it works. You could also confirm it by checking the output in the Redis CLI.

So that's all you need to use the Redis adapter. I would encourage you to try different adapters and their options!


Today, we went through one of the most popular caching libraries for PHP—PhpFastCache. In the first half of the article, we discussed the basics along with installation and configuration. Later in the article, we went through a couple of examples to demonstrate the concepts that we discussed.

I hope you've enjoyed the article and that you will be motivated to integrate the PhpFastCache library in your upcoming projects. Feel free to post any questions and comments below!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.