PHP code example of 104corp / cache

1. Go to this page and download the library: Download 104corp/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/ */

    

104corp / cache example snippets


use Corp104\Cache\Util\CacheAwareTrait;

class Resource
{
    use CacheAwareTrait;
        
    public function getDefaultTtl()
    {
        // 60 second
        return 60;
    }
    
    public function getData()
    {
        $data = null;
        
        if (null !== $this->cache) {
            $data = $this->cache->get('some-resource-key', null);
        }
        
        if (null === $data) {
            $data = $this->getRealData();
            
            if (null !== $this->cache) {
                $this->cache->set('some-resource-key', $data, $this->getTtl());
            }
        }
        
        return $data;
    }
    
    private function getRealData()
    {
        return 'RealData';
    }
}

$cacheInstance = new \Symfony\Component\Cache\Simple\PhpFilesCache();

$resource = new Resource();
$resource->setCache($cacheInstance);

$data = $resource->getData();