php-strict/storable-cache

可存储缓存可以存储项目超过其生命周期,直到保存时间。

v1.0.0 2019-12-06 11:42 UTC

This package is auto-updated.

Last update: 2024-09-16 16:37:36 UTC


README

Software License Build Status codecov Codacy Badge

可存储缓存可以存储项目超过其生命周期,直到保存时间,并允许在到期后使用存储的项目,例如在数据库过载或错误的情况下。

支持的存储

  • 数组(非真实存储,仅用于测试),
  • 文件,
  • memcached,
  • redis,
  • SQLite,
  • MySQL(使用应用的主数据库连接)。

需求

安装

使用 Composer 安装

composer require php-strict/storable-cache

用法

基本用法

use PhpStrict\Config\Config;
use PhpStrict\StorableCache\StorableCache;

//instance of application configuration class, extending Config
//must provide cacheType property with correct storable cache type
//see PhpStrict\StorableCache\StorageTypes class
$config = new AppConfig();
$config->loadFromFile('config.ini');

//instance of StorableCache
$cache = new StorableCache($config);

//part of generating content method
if ($cache->has('contentKey') && !$cache->expired('contentKey')) {
    return $cache->get('contentKey');
}
//part of generating content method

//saving generated content: key, value, ttl, tts (time to save)
$cache->set('contentKey', $content, 60, 3600);

如果内容生成主进程失败且可以使用过期数据的情况下的用法

use PhpStrict\StorableCache\StorableCache;

//part of generating content method

//generating content failed

if ($cache->has('contentKey')) {
    return $cache->get('contentKey');
}

throw Exception('Generating content failed');
//part of generating content method

测试

要执行测试套件,您需要 Codeception

vendor\bin\codecept run