flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.6k stars 407 forks source link

Regarding cache #368

Closed premento closed 5 months ago

premento commented 6 years ago

I was trying to implement phpfastcache.com into flightphp. Could you please share the best possible way to use a single instance of cache across different functions?

I am getting the issue similar to https://github.com/PHPSocialNetwork/phpfastcache/issues/585 I am not a php expert!

levinside commented 6 years ago

Simplest way is: using Psr16Adapter — Want to keep it simple ? In that case it will look like this:

use phpFastCache\Helper\Psr16Adapter;

$cahce_driver = 'files';
$cache_config = [
  'path' => sys_get_temp_dir(),
];

Flight::register('myCache', '\phpFastCache\Helper\Psr16Adapter', [$cahce_driver, $cache_config]);

Flight::route('/test_cache', function(){
    if(!Flight::myCache()->has('test-key')){
        // Setter action
        $data = 'lorem ipsum';
        Flight::myCache()->set('test-key', $data, 300);// 5 minutes
    }
    else{
        // Getter action
        echo Flight::myCache()->get('test-key');
    }
});