PHP code example of aportela / simple-fs-cache

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

    

aportela / simple-fs-cache example snippets




    er = new \Psr\Log\NullLogger("");

    try {
        $cachePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . "cache";

        $ttl = null; // never expires
        //$ttl = 60; // cache expires after 60 seconds
        //$ttl = new \DateInterval("PT60M"); // cache expires after 60 minutes

        $cache = new \aportela\SimpleFSCache\Cache($logger, $cachePath, $ttl, \aportela\SimpleFSCache\CacheFormat::JSON);

        // json example data
        $data = json_encode(array("str" => "this is the data to store in cache"));

        // you can use another hash algorithm (sha1?) if you don't trust that MD5 value is unique
        $cacheUniqueIdentifier = md5($data);

        if ($cache->set($cacheUniqueIdentifier, $data)) {
            $cachedData = $cache->get($cacheUniqueIdentifier, null);
            if ($cachedData !== null) {
                echo "Cache load sucessfully, contents: {$cachedData}" . PHP_EOL;
                $cache->delete($cacheUniqueIdentifier);
            } else {
                echo "Cache expired" . PHP_EOL;
            }
        } else {
            echo "Error saving cache" . PHP_EOL;
        }
    } catch (\aportela\SimpleFSCache\Exception\FileSystemException $e) {
        // this exception is thrown when cache path creation failed
        echo "Cache filesystem error: " . $e->getMessage();
    }