molajo/cache

支持APC、数据库、文件、Memcached、内存、Redis、Wincache和xCache处理器的PHP应用程序缓存API。

安装: 476

依赖: 2

建议: 0

安全: 0

星星: 6

关注者: 8

分支: 1

公开问题: 1

类型:molajo-package

v0.4 2014-04-07 15:46 UTC

This package is auto-updated.

Last update: 2024-09-08 04:36:45 UTC


README

======= Molajo Cache API

Build Status

为PHP应用程序提供简单、清晰的缓存API,用于获取、[设置] (https://github.com/Molajo/Cache/Cache#set)、[移除] (https://github.com/Molajo/Cache/Cache#remove)、[清除] (https://github.com/Molajo/Cache/Cache#clear)缓存。

可用的缓存处理器包括

快速浏览 ...

  1. 构建一个缓存处理器类。
  2. 实例化缓存适配器,并向其注入缓存处理器实例。
  3. 设置缓存。
  4. 获取缓存。
  5. 移除缓存。
  6. 清除缓存。
    // 1. Instantiate a Cache Handler.
    $options = array();
    $options['cache_folder']  = SITE_BASE_PATH . '/' . $application->get('system_cache_folder', 'cache');
    $options['cache_time']    = $application->get('system_cache_time', 900);
    $options['cache_handler'] = $application->get('cache_handler', 'File');

    use Molajo\Cache\Adapter\File as FileCache;
    $adapter_handler = new FileCache($options);

    // 2. Instantiate the Adapter, injecting it with the Handler.
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

    // 3. Set cache.
    $adapter->set('key value', 'cache this value for seconds =>', 86400);

    // 4. Get Cache.
    $cacheItem = $adapter->get('this is the key for a cache item');

    if ($cacheItem->isHit() === false) {
        // deal with no cache
    } else {
        echo $cacheItem->value; // Use the Cached Value
    }

    // 5. Remove cache.
    $adapter->remove('key value');

    // 6. Clear cache.
    $adapter->clear();

缓存API

缓存操作的常用API:get、[set] (https://github.com/Molajo/Cache/Cache#set)、[remove] (https://github.com/Molajo/Cache/Cache#remove)、[clear] (https://github.com/Molajo/Cache/Cache#clear)方法。

获取

检索与指定键关联的CacheItem对象。如果找不到值,则抛出异常。

    try {
        $cacheItem = $adapter->get($key);

    } catch (Exception $e) {
        // deal with the exception
    }

    if ($cacheItem->isHit() === true) {
        $cached_value = $cacheItem->getValue();
    } else {
        // cache is not available - do what you have to do.
    }

参数

  • $key 包含请求缓存的键值

设置

将值作为缓存存储到指定的键值和指定秒数的缓存中。

    try {
        $adapter->set($key, $value, $ttl);

    } catch (Exception $e) {
        // deal with the exception
    }

参数

  • $key 包含用于缓存键的值
  • $value 包含要存储为缓存的值
  • $ttl "生命周期",即缓存被认为是相关的秒数

移除

移除与指定键值关联的缓存条目。

    try {
        $adapter->remove($key);

    } catch (Exception $e) {
        // deal with the exception
    }

参数

  • $key 包含用于缓存键的值

清除

移除此缓存处理器实例的所有缓存。

    try {
        $adapter->clear();

    } catch (Exception $e) {
        // deal with the exception
    }

参数

  • n/a 不需要任何参数

缓存适配器处理器

可用的缓存处理器包括

Apc

APC (Alternative PHP Cache) 是PHP的标准配置。Molajo Cache提供了APC Cache处理器,可以使用如下。

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Apc;
    $adapter_handler = new Apc($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

数据库

在使用数据库缓存处理器之前,必须有一个包含四个列的表:id(标识列)、key(varchar(255))、value(text)和expiration(integer)。在实例化缓存处理器时,传入数据库连接、缓存数据库表名称、RDBMS引号和名称引号的值,如下例所示。

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the Database Handler
    $options['database_connection'] = $connection;
    $options['database_table']      = 'xyz_cache_table';
    $options['database_quote']      = "'";
    $options['database_namequote']  = '`';

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Database;
    $adapter_handler = new Database($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

哑元

哑元缓存处理器可用于测试目的。它实际上并不缓存数据。使用方法如下

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Dummy as DummyCache;
    $adapter_handler = new DummyCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

文件

文件缓存处理器可用于将本地文件系统转换为缓存设备。使用方法如下

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the File Handler
    $options['cache_handler']       = '/Absolute/Path/To/Cache/Folder';

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\File as FileCache;
    $adapter_handler = new FileCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

Memcached

Memcached缓存处理器需要加载memcached PHP扩展,并存在Memcached类。有关更多信息,请参阅PHP手册中的Memcached。使用方法如下

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the Memcached Handler
    $options['memcached_pool']         = $connection;
    $options['memcached_compression']  = 'xyz_cache_table';
    $options['memcached_servers']      = "'";

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Memcached
    $adapter_handler = new Memcached($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

内存

内存缓存处理器可用于在内存中存储变量。这可以与Session一起使用来创建持久性,如果需要的话。使用方法如下

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Memory
    $adapter_handler = new Memory($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

Redis

Redis缓存处理器可用于在内存中存储变量。这可以与Session一起使用来创建持久性,如果需要的话。使用方法如下

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Redis
    $adapter_handler = new Redis($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

Wincache

Wincache 缓存处理器 需要加载 PHP 扩展 wincache 并且 wincache_ucache_get 是可调用的。更多详细信息,请参阅 Windows Cache for PHP。 除了使用 Windows 操作系统,使用 Wincache 不需要其他配置选项。

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Wincache
    $adapter_handler = new Wincache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

xCache

xCache 处理器 需要加载 PHP 扩展 xcache 并且 xcache_get 是可调用的。

    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\XCache
    $adapter_handler = new XCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

使用 Composer 从 Packagist 安装

步骤 1:在您的项目中安装 composer

    curl -s https://getcomposer.org/installer | php

步骤 2:在项目根目录中创建一个 composer.json 文件

{
    "require": {
        "Molajo/Cache": "1.*"
    }
}

步骤 3:通过 composer 安装

    php composer.phar install

需求和合规性