PHP code example of fastd / cache

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

    

fastd / cache example snippets



return [
    'file' => [
        'adapter' => [
            'class' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class,
        ],
        'namespace' => 'app',
        'lifetime' => 3600,
        'directory' => __DIR__ . '/../runtime/cache/',
    ]
];


return [
    'services' => [
        \FastD\Cache\ServiceProvider\CacheServiceProvider::class,
    ]
];

// 获取缓存适配器
$cache = cache('file');

// 存储数据
$item = $cache->getItem('user_123');
$item->set(['name' => 'John'])->expiresAfter(3600);
$cache->save($item);

// 读取数据
$item = $cache->getItem('user_123');
if ($item->isHit()) {
    $user = $item->get();
}

// 配置缓存中间件
// config/cache.php
return [
    'httpCache' => [
        'lifetime' => 3600,  // 缓存 1 小时
        'cache_keys' => ['page', 'limit'],  // 参与缓存键的查询参数
    ],
];

// 注册中间件
// config/middleware.php
return [
    \FastD\Cache\Middleware\XMCache::class,
];