PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.37k stars 451 forks source link

Multi Instance Cachea - Example Request #347

Closed kennylajara closed 8 years ago

kennylajara commented 8 years ago

Hi,

I am new on this of caching (first time doing it) and I want to implement a multicaching but I am not pretty sure how to do it. I found an example in the site but is kind of confusing to me (maybe due to the tag thing).

I'd like to see if you can provide the simplest posible example of preparing the use of the library (not using composer, yet), preparing the APC and memcached instances and trying to check the value of a variable and setting it for both instances if does not exist.

This is the logic of what I am looking for:

// The way to prepare it to be uses without composer
require('./phpfastcache.php'), // That's it?

// Please, insert here an example of how to create APC instance
// Please, insert here an example of how to create Mencached instance

// Complex function that return the value you want to cache (example only)
function  getValueOnTheHardestPosibleWay(){
    // Get the value
    return 123;
}

// The logic of the function this request is about. You need to modify it.
function getValue(&$var, $callBackFunction){
    // Try to see if the value exist in memory
    if(isset($var)){
        // Set APC expiration within 5 minutes since now or create it if not exist.
        // Set Memcached expiration within 1 Hour since now or create it if not exist.
    }else{
        // Check if the value exist in APC Instance
        if(existOnAPC($var)){
            $var = getFromAPC(); // Get the value from APC
            // Set APC expiration within 5 minutes since now or create it if not exist.
            // Set Memcached expiration within 1 Hour since now or create it if not exist.
        }else{
            // Check if the value exist in APC Instance
            if(existOnMemcached($var)){
                $var = getFromMemcached(); // Get the value from APC
                // Set APC expiration within 5 minutes since now or create it if not exist.
                // Set Memcached expiration within 1 Hour since now or create it if not exist.
            }else{
                 $var =call_user_func('callBackFunction');
                 // Cache on APC for 5 minutes.
                 // Cache on Memcached for 1 hour.
            }   
        }
    }
}

// Example of use
getValue($var, 'getValueOnTheHardestPosibleWay');

Well, I hope you understand my example, if you don't ask and I'll try to explain what you don't get. I know there is an example in the webpage but I couldn't undestand. It loks to me like if both instanses use the same variable, so, I don't get how to set different expiration time and so.

Geolim4 commented 8 years ago

Hi @thingNumber1 If you don't use composer, then follow this example: https://github.com/PHPSocialNetwork/phpfastcache/blob/final/examples/legacy.php

Now if you want to use multiple driver instance, this is possible:

$MemcacheCache = CacheManager::getInstance('memcache'); $ApcCache = CacheManager::getInstance('apc'); $XcacheCache = CacheManager::getInstance('xcache');

For each driver instance (also called ItemPool) that you get, you can get an item using getItem(). See the Wiki: https://github.com/PHPSocialNetwork/phpfastcache/wiki#item-api

Cheers, Georges

Geolim4 commented 8 years ago

Also please note that the Website is not yet updated, for now you must rely on the Wiki, we'll update the website asap.

Thanks you.

kennylajara commented 8 years ago

Ok, Thanks. =)