eghojansu/cache-helper

该软件包已被放弃,不再维护。作者建议使用ekok/php-cache软件包。

缓存助手

dev-master 2018-02-22 05:18 UTC

This package is auto-updated.

Last update: 2022-02-22 01:50:08 UTC


README

PHP 缓存助手。它支持多种缓存引擎。

  • Apc;
  • Apcu;
  • Memcached;
  • Redis;
  • Wincache;
  • Xcache;
  • FileCache(作为替代或后备)。

背景

原始代码来自Fatfree Cache类。由于原始代码对我来说非常复杂,我决定创建这个仓库。

安装

composer require eghojansu/cache-helper:dev-master

用法

主缓存类将代理方法调用到驱动器。要使用驱动器,您可以将简单的dsn格式传递给第一个构造函数。

<?php

use Fal\Cache\Cache;
use Fal\Cache\Serializer;

$cache = new Cache(
    // simple dsn, @see simple dsn table below, pass '' (empty string) to disable
    'apcu',
    // prefix, can pass '' (empty string) but not null value
    '',
    // fallback file cache dir
    '/path/to/cache/dir/fallback',
    // Serializer helper
    new Serializer()
);

// get cache, it will return empty array if cache does not exist or cache was expired
// if cache exist it return array of [data, time, ttl]
$cached = $cache->get('foo');
if ($cached) {
    list( $data ) = $cached;
} else {
    $data = 'foo';
    $cache->set('foo', $data);
}

echo $data;

DSN 格式

+-----------+----------------------------------------------------+------------------------+
| Driver    | DSN                                                | ~                      |
+-----------+----------------------------------------------------+------------------------+
| Apc       | apc                                                |                        |
| Apcu      | apcu                                               |                        |
| Memcached | memcached=host[:port];host2[:port];...hostn[:port] |                        |
| Redis     | redis=host[:port[:db]]                             |                        |
| Wincache  | wincache                                           |                        |
| Xcache    | xcache                                             |                        |
| Filecache | folder=/path/to/cache/dir/                         | Require trailing slash |
| NoCache   | (empty string)                                     | To disable cache       |
+-----------+----------------------------------------------------+------------------------+

如果提供了dsn且没有根据上述规则匹配,则将使用FileCache。

致谢