fsi/cache

此包已被弃用且不再维护。未建议替代包。

FSi Cache - 支持命名空间的不同缓存存储的轻量级组件。

0.9.3 2012-11-08 13:18 UTC

This package is auto-updated.

Last update: 2021-02-10 10:31:25 UTC


README

请勿使用此包,因为它不会收到任何更新,未来可能会被删除。

FSi Cache 组件文档

特性

  • 支持数组、APC 和文件缓存存储。未来将添加新的存储。
  • 支持缓存内部的命名空间

默认情况下,所有缓存类型使用 fsicache 命名空间,但您可以使用 setNamespace 方法更改它

<?php

$cache->setNamespace('namespace-name');

您还可以在创建新缓存时使用选项 'namespace'(它适用于所有类型的缓存)。

<?php

$cache = new ArrayCache(array('namespace' => 'namespace-name'));

还可以将命名空间作为可选参数传递给方法

  • getItem($key, $namespace = null)
  • hasItem($key, $namespace = null)
  • addItem($key, $item, $lifetime = 0, $namespace = null)
  • setItem($key, $item, $lifetime = 0, $namespace = null)
  • removeItem($key, $namespace = null)

如果 $namespace 为 null,则从方法 getNamespace() 获取当前命名空间。

设置和自动加载

我们强烈建议使用由 composer.phar 生成的自动加载器。

将反射添加到 composer.json 中

{
    ...
    "require": {
        ...
        "fsi/cache": "0.9.*"
        ...
    },
    ...
}

数组缓存

数组缓存应仅在开发环境中使用,以模拟正常的缓存行为。

示例

<?php

$cache = new ArrayCache();

APC 缓存

APC 缓存需要在 web 服务器中启用 APC 扩展。有关 APC 的信息,请参阅 php.net

示例

<?php

$cache = new ApcCache();

文件缓存

文件缓存需要在构造函数中传递变量 $options['directory'] 内的缓存目录路径。还有一个额外的参数 $options['dirlvl'],它描述了缓存应嵌套多深。当您知道缓存将包含大量文件时,dirlvl 参数可能很有用。较高的 dirlvl 意味着单个缓存目录中的文件更少。

示例

<?php

$cache = new FileCache(array('directory' => '/tmp', 'dirlvl' => 3));

示例

基本使用

<?php

use FSi\Component\Cache\ApcCache;

// Create APC cache instance with default namespace.
$cache = new ApcCache();

// Check if there is a foo.
if ($cache->hasItem('foo')) {
    echo 'foo exists in cache!';
} else {
    // Store foo-value in cache under key foo for 3600 seconds.
    $cache->setItem('foo', 'foo-value', 3600);
}

命名空间使用

<?php

use FSi\Component\Cache\ApcCache;

// Create APC cache instance with default namespace.
$cache = new ApcCache();

$cache->setItem('key1', 'test', 0, 'testnamespace1');
$cache->setItem('key2', 'test', 0, 'testnamespace2');

$cache->hasItem('key1', 'testnamespace1'); // Will return true.
$cache->hasItem('key2', 'testnamespace2'); // Will return true.

$cache->hasItem('key2', 'testnamespace1'); // Will return false.
$cache->hasItem('key1', 'testnamespace2'); // Will return false.