carloshb/cache-service

使用 SQLite 简单缓存服务以减少多次数据库查询

0.2.1 2017-05-23 01:24 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:15:25 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

使用 SQLite 简单缓存服务以减少多次数据库查询。

基本信息

  • 默认缓存时间:60分钟
  • 默认缓存路径:Carloshb\CacheService\Driver

如何安装?

使用 composer 安装

composer require carloshb/cache-service 0.2.*

或者在项目中克隆。

如何更改默认配置?

添加 PHP 环境变量

    $_ENV['cache_time'] = 10; // minutes
    $_ENV['cache_path'] = 'yor/storage/path';

用法

resolve(string $key, callable $callback [, int $time = 60]) : Cache

resolve() 方法返回 Cache 类的实例,并生成或更新带有回调返回值的键。第一个参数是键,一个字符串。第二个参数是回调,一个返回预期结果的数组的功能。第三个参数是可选的,用于设置 n 分钟的缓存。

    $cache = new \Carloshb\CacheService\Cache();
    $cache->resolve('getInfo', function(){
        return array(
            'name' => 'Carlos Henrique Bernardes',
            'email' => 'contato@carloshb.com.br'
        );
    });

getCacheContent() : json

resolve() 之后使用以获取缓存内容,该方法返回一个 JSON。

    $cache = new \Carloshb\CacheService\Cache();
    $content = $cache->resolve('getInfo', function(){
        return array(
            'name' => 'Carlos Henrique Bernardes',
            'email' => 'contato@carloshb.com.br'
        );
    })->getCacheContent();

destroy(string $key) : bool

使用此方法通过键名强制销毁缓存。

    $cache = new \Carloshb\CacheService\Cache();
    $response = $cache->destroy('getInfo');
    var_dump($response); // true or false