1. Go to this page and download the library: Download zoujingli/type-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/ */
zoujingli / type-cache example snippets
declare(strict_types=1);
use Type\Cache\JsonCodec;
use Type\Cache\NamespaceStore;
use Type\Cache\TypedCache;
use Type\Redis\Purpose;
use Type\Redis\RedisConfiguration;
use Type\Redis\RedisManager;
use Type\Runtime\ExecutionScope;
/**
* 缓存有限 TTL 的公开示例数据,资源不得跨本次执行作用域使用。
*/
function main(): void
{
$host = getenv('REDIS_HOST');
$port = filter_var(getenv('REDIS_PORT') === false ? '6379' : getenv('REDIS_PORT'), FILTER_VALIDATE_INT,
['options' => ['min_range' => 1, 'max_range' => 65535]]);
if (!is_int($port)) {
throw new InvalidArgumentException('REDIS_PORT 必须为有效整数端口');
}
$manager = new RedisManager(['default' => new RedisConfiguration($host === false ? '127.0.0.1' : $host, $port)]);
$scope = new ExecutionScope();
try {
$redis = $manager->connection($scope, 'default', Purpose::SCRIPT);
$store = new NamespaceStore($redis, 'readme-example', 'local', 'profile-v1');
$cache = new TypedCache($store, JsonCodec::data('profile-v1'), 60000);
$profile = $cache->remember('user:7', static fn (): array => ['id' => 7, 'name' => '示例']);
echo json_encode($profile, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) . "\n";
} finally {
try {
$scope->close();
} finally {
$manager->close();
}
}
}