In this article, we’ll dive into the RavenDB extension for the PhpFastCache library. This powerful tool lets you add caching to your PHP applications, enhancing your website’s performance and reducing page load times.

What is PhpFastCache?

PhpFastCache is a high-performance backend cache system designed to speed up dynamic web applications by reducing database load. When implemented effectively, it can drastically reduce database load, resulting in faster page load times for users and better resource utilization. This library is both simple and powerful, making it easy to integrate caching into your PHP applications.

PhpFastCache provides an array of APIs that allow you to implement a caching strategy of your choice with ease. It’s not just a traditional file system caching solution; it supports a variety of adapters, giving you the flexibility to choose from high-performance back-ends like Memcache, Redis, RavenDB, and others. This versatility makes PhpFastCache an essential tool for PHP developers looking to enhance their website’s performance and efficiency.

In the next sections, we’ll guide you through installing and configuring PhpFastCache with the RavenDB extension. We’ll explore using RavenDB, and we’ll walk you through a few examples to demonstrate how it works.

Using RavenDB as the backend for PhpFastCache allows you to use a distributed replicated database as the backend cache for your website. This provides high performance and scalable infrastructure to speed your up your web applications.

Installation and Configuration

In this section, we’ll cover how to install and configure the RavenDB extension for the PhpFastCache library.

We’ll be installing the PhpFastCache & PhpFastCache RavenDB extension as a Composer package, which is the preferred method since it makes future maintenance and upgrades easier. Composer is a dependency manager for the PHP language, if you haven’t installed Composer yet, you’ll need to do that first. You can see the Composer installation guide for various OSes on their website.

For other installation option please refer to the PhpFastCache official site.

After installing Composer, you can get the PhpFastCache and RavenDB extension packages with these commands:

# Install phpfastcache 
composer require phpfastcache/phpfastcache
# Install phpfastcache RavenDB extension
composer require phpfastcache/ravendb-extension

Once the command is successfully executed, you should see a vendor directory, which includes everything needed to run the PhpFastCache library.

The only remaining step is to include the autoload.php file in your application to start using PhpFastCache. You can find autoload.php in the vendor directory.

// load fastphpcache package

require __DIR__ . '/vendor/autoload.php';

You’re all set to start caching and enjoying the benefits of the PhpFastCache library. In the next section, we’ll explore a couple of practical examples showing how to use PhpFastCache with the RavenDB extension in your application.

Caching Using the RavenDB Extension Driver

Before we continue, you need to have the RavenDB server up and running and make sure the server has database called phpfastcache.

For demonstration I will use the RavenDB public instance, this means the server can be on a dedicated machine, or even a cluster of RavenDB servers can be used as the persistence layer for PhpFastCache.

To install RavenDB on your own, you can follow the insturctions in the documentation or host RavenDB in the Cloud.

Let’s create a file named example-ravendb-cache.php with the following content:

<?php
// load fastphpcache package
require __DIR__ . '/vendor/autoload.php';

use Phpfastcache\CacheManager;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Drivers\Ravendb\Config as RavenDBConfig;

// create config
$config = new RavenDBConfig();
$config->setHost(['http://live-test.ravendb.net']);
$config->setDatabaseName('phpfastcache');
$config->setCollectionName('CachedItems');

// create cache instance
$cacheInstance = CacheManager::getInstance('RavenDB', $config);

$key = "product_page";
$product_data = 'First product';

// init
$val = $cacheInstance->getItem($key);
if ($val->isHit() == false) {
    // if the item is not cached, add it
    // save the product_data string to cache under key
    // set expire to 5 seconds
    $val->set($product_data)->expiresAfter(5); 
    $cacheInstance->save($val);

    echo "Added to cache the value of product_page: " . $val->get();
} else {
    // if the item is cached, return from cache
    echo "Returned from cache the value of product_page: " . $val->get();
}
?>

Let’s go through the code and give some explanations about it. We add using of Phpfastcache\Drivers\Ravendb\Config class:

use Phpfastcache\CacheManager; 
use Phpfastcache\Drivers\Ravendb\Config as RavenDBConfig;

We set the RavenDB server URL and select the database to use. In the example I used the RavenDB public instance URL, database named phpfastcache and using the collection CachedItems.

// create config
$config = new RavenDBConfig();
$config->setHost(['http://live-test.ravendb.net']);
$config->setDatabaseName('phpfastcache');
$config->setCollectionName('CachedItems');

In cache instance creation we specify that we want to use RavenDB as the backing store and provide the config we just created:

$cacheInstance = CacheManager::getInstance('RavenDB', $config);

This is all the setup you need in order to integrate RavenDB into PhpFastCache. From this point forward, all the API is the same API that you are already used to.

In RavenDB Server, in the phpfastcache database there will be a new document created in CacheItems collection with id pfc/product_page which is a combination of static pfc prefix and the cached item key, you can see the document in RavenDB studio:

If we go into the RavenDB Studio and open the document, we can see the actual data cached and the expiration date of the cached item:

The nice thing about using the RavenDB extension for PhpFastCache is that storing the data inside of RavenDB not only make it highly available and distributed. It also gives you the ability to inspect the content of the cache and understand what the system is doing on the backend.

As you can see, there isn’t much involved in actually making use of RavenDB & PhpFastCache. You can also integrate RavenDB more deeply into your PHP application. We have a full blown PHP client API that you can use to gain the same benefits as PhpFastCache is using. If you have any questions, we’ll be happy to answer them in the GitHub discussions group.