initphp/cache

PSR-16 是一个简单的缓存库,它使用 Simple Cache 接口。

0.1 2022-03-17 08:22 UTC

This package is auto-updated.

Last update: 2024-09-24 11:56:16 UTC


README

PSR-16 是一个简单的缓存库,它使用 Simple Cache 接口。

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

要求

根据您将使用的处理器,可能需要 PHP 扩展。

  • Redis
  • PDO
  • Wincache
  • Memcache 或 Memcached

安装

composer require initphp/cache

使用

require_once "../vendor/autoload.php";
use \InitPHP\Cache\Cache;

$cache = Cache::create(\InitPHP\Cache\Handler\File::class, [
    'path'      => __DIR__ . '/Cache/';
]);

if(($posts = $cache->get('posts', null)) === null){
    $posts = [
        ['id' => '12', 'title' => 'Post 12 Title', 'content' => 'Post 12 Content'],
        ['id' => '15', 'title' => 'Post 15 Title', 'content' => 'Post 15 Content'],
        ['id' => '18', 'title' => 'Post 18 Title', 'content' => 'Post 18 Content']
    ];
    $cache->set('posts', $posts, 120);
}

echo '<pre>'; print_r($posts) echo '</pre>';

配置和选项

\InitPHP\Cache\Handler\File::class

$options = [
    'prefix'    => 'cache_',
    'mode'      => 0640,
];

\InitPHP\Cache\Handler\Memcache::class

$options = [
    'prefix'    => 'cache_',
    'host'          => '127.0.0.1',
    'port'          => 11211,
    'weight'        => 1,
    'raw'           => false,
    'default_ttl'   => 60,
];

\InitPHP\Cache\Handler\PDO::class

$options = [
    'prefix'    => 'cache_',
    'dsn'           => 'mysql:host=localhost;dbname=test',
    'username'      => null,
    'password'      => null,
    'charset'       => 'utf8mb4',
    'collation'     => 'utf8mb4_general_ci',
    'table'         => 'cache'
];

以下是针对 MySQL 的示例缓存表创建查询。

CREATE TABLE `cache` (
    `name` VARCHAR(255) NOT NULL,
    `ttl` INT(11) NULL DEFAULT NULL,
    `data` TEXT NOT NULL,
    UNIQUE  (`name`)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;

\InitPHP\Cache\Handler\Redis::class

$options = [
    'prefix'    => 'cache_',
    'host'      => '127.0.0.1',
    'password'  => null,
    'port'      => 6379,
    'timeout'   => 0,
    'database'  => 0
];

\InitPHP\Cache\Handler\Wincache::class

$options = [
    'prefix'    => 'cache_', // Cache Name Prefix
    'default_ttl'   => 60, // Used if ttl is NULL or not specified.
];

鸣谢

许可证

版权所有 © 2022 MIT 许可证