gameplayjdk/php-file-cache

使用 var_export() 和一些魔法进行低级、基于文件的缓存。可选 PSR-16 兼容。

v1.2.3 2020-04-03 18:59 UTC

This package is auto-updated.

Last update: 2024-08-29 05:28:30 UTC


README

使用 brick/varexporter 进行低级、基于文件的缓存。可选符合 PSR-16

安装

composer require gameplayjdk/php-file-cache

用法

<?php

require __DIR__ . '/vendor/autoload.php';

use Cache\Cache;
use Cache\Psr16\Cache as Psr16Cache;

$key = 'an_example_key';

// Create cache object.
$cache = new Cache(__DIR__ .  '/cache');

// Set something to the cache.
$value = 'some string value';
echo 'value set to the cache is ' . $value;
echo PHP_EOL;
$cache->set($key, $value);

// Get something from the cache.
$value = $cache->get($key);
echo 'value got from the cache is ' . $value;
echo PHP_EOL;

// Check if something is expired in cache.
$expired = $cache->exp($key, strtotime('+1 Minute'));
echo $expired ? 'expired' : 'not expired';
echo PHP_EOL;

// Check if something is present in the cache.
$present = $cache->has($key);
echo $present ? 'present' : 'not present';
echo PHP_EOL;

// Delete something from the cache.
$cache->del($key);
echo 'deleted';
echo PHP_EOL;

// Check if something is present in the cache. This time after deleting it.
$present = $cache->has($key);
echo $present ? 'still present' : 'no more present';
echo PHP_EOL;

// Caching an object.
echo PHP_EOL;

/**
 * Class Something
 */
class Something {

    /**
     * @var mixed|null
     */
    public $var1 = null;

    /**
     * @var mixed|null
     */
    public $var2 = null;

    /**
     * This is optional.
     * 
     * @param array $an_array
     * @return Something
     */
    public static function __set_state($an_array)
    {
        $obj = new static();
        $obj->var1 = $an_array['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }
}

// Set something to the cache.
$obj = new Something();
$obj->var1 = 1;
$obj->var2 = $value;
echo 'value set to the cache is ' . print_r($obj, true);
echo PHP_EOL;
$cache->set($key, $obj);

// Get something from the cache.
$obj = $cache->get($key);
echo 'value got from the cache is ' . print_r($obj, true);
echo PHP_EOL;

// Check if something is present in the cache.
$present = $cache->has($key);
echo $present ? 'present' : 'not present';
echo PHP_EOL;

// Clear the whole cache.
$cache->clr();
echo 'cleared';
echo PHP_EOL;

// Check if something is present in the cache. This time after clearing the whole cache..
$present = $cache->has($key);
echo $present ? 'still present' : 'no more present';
echo PHP_EOL;

// The PSR-16 compliant adapter can also be used. It takes a cache instance as constructor argument.
$psrCache = new Psr16Cache($cache);
// Set the cache expiry time to one minute.
$psrCache->setCacheExpiryTime(strtotime('+1 Minute', 0));

// Now the object can be used using the standard interface methods.
echo PHP_EOL;

// The PSR-16 compliant cache also supports sanitizing keys before using them.
// It is generally advised to use the adapter class.

许可证

它是 MIT 许可证。