PHP code example of nilportugues / cache

1. Go to this page and download the library: Download nilportugues/cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nilportugues / cache example snippets



NilPortugues\Cache\Adapter\MemcachedAdapter;
use NilPortugues\Cache\Adapter\PredisAdapter;
use NilPortugues\Cache\Cache;

$parameters = ameters['memcached']['persistent_id'],
        $parameters['memcached']['connections']
    );
);

return [
    'user_cache' => new Cache($cache, 'user', 60*5), //5 minutes cache
    'image_cache' => new Cache($cache, 'image', 60*60), //1 hour cache
];

$db = $this->serviceContainer->get('database');
$cache = $this->serviceContainer->get('cache');

$userId = 1;
$cacheKey = sprintf("user:id:%s", $userId);

$user = $cache->get($cacheKey);
if(null !== $user) {
  return $user;
}

$user = $db->findById($userId);
$cache->set($cacheKey, $user);

return $user;