Search code examples
phpcachingwebphpfastcache

How to cache a query with phpFastCache without Composer?


I am trying to use phpFastCache for all caching needs but I really don't understand how to use it. I understand their are examples and yes I have tried them and they are successful but it's not helping me in terms of what I need to do.

I am trying to cache a query (Valve's source query protocol to be exact.)

Here are the results, also, I am using a separate SourceQuery script and this is just the results (queryresults.php):

$serveroneip = "example.ip";
$serveroneport = "27015"
$server = new SourceQuery($serveroneip, $serveroneport);
$infos  = $server->getInfos();

And then just add that to the index.php page:

<?php
include ("queryresults.php")
?>
<p>'.$infos['players'].' / '.$infos['places'].'</p>

That would just print the current player count and the total players on the source server. I am basically trying to cache that query because it helps page load times.

If I sound like a complete noob at this I am very sorry. This is just an issue that has been frustrating me for the past few days and I looked here as a last resort. If you need more info I can happily provide it! Thank you so much for the help!


Solution

  • As of Phpfastcache V5, the library comply with PSR6 interface

    So basically the code would be pretty simple, and even easier with composer:

    composer require phpfastcache/phpfastcache
    

    If its not installed globally:

    php composer.phar require phpfastcache/phpfastcache
    

    The composer.phar can be downloaded here: https://getcomposer.org/composer.phar

    Now the code, with your case:

    use Phpfastcache\CacheManager;
    
    /**
     * You have two many ways...
     * Via composer:
     */
    require 'vendor/autoload.php';
    
    /**
     * Or if you have absolutely no choice, we provide a standalone autoloader
     */
    // require 'phpfastcache/src/autoload.php';
    
    /**
     * We are using the default but most used driver: Files
     * You can use redis/predis, etc but it's a bit more complexe
     */
    $cachePool = CacheManager::getInstance('Files');
    $cacheItem = $cachePool->getItem('mySteamServer');
    
    /**
     * Does we found something in cache ?
     */
    if($cacheItem->isHit()){
        /**
         * Yes, let's use it
         */
        $infos = $cacheItem->get();
    }else{
        /**
         * Nahh, let's retrieve the server data and cache them
         */
        $serveroneip = "example.ip";
        $serveroneport = "27015";
        $server = new SourceQuery($serveroneip, $serveroneport);
        $infos  = $server->getInfos();
        $cacheItem->set($infos)->expiresAfter(300);// The TTL in seconds, here is 5 minutes
        $cachePool->save($cacheItem);// Persist the cache item
    }
    
    /**
     * Rest of your code goes here
     */
    

    Anyway I warmly suggest you to make use of composer. This will make your dependencies management easier and allow you to gain an absolute control over automatic updated, conflict management, autoloading nightmare, etc.