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 latency on page load times.

What Is PhpFastCache?

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

Make no mistake by assuming that it's merely a traditional file system caching scheme. In fact, PhpFastCache supports 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 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 basic functionalities, the PhpFastCache library also provides the 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 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 the 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 containing 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 would ask you to install them first.

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

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 off things.

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

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

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

Demonstration

We've already discussed that the PhpFastCache library supports various adapters when it comes to caching. In this section, we'll demonstrate using the filesystem 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 by 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 fairly easy setup, wasn't it? In fact, you can go ahead and run the file to check 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 files 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 at 6379 port, which is the default port for Redis.

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

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

Of course, you would like to change host and port settings to match your requirements if you're running the 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!

Conclusion

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 took the opportunity to introduce couple of examples to demonstrate 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!

Categories: