snakano / cache-store

提供缓存任何数据的通用方法

1.5.0 2016-01-16 08:02 UTC

This package is auto-updated.

Last update: 2024-09-25 20:52:08 UTC


README

Build Status Latest Stable Version Total Downloads License

提供缓存任何数据的通用方法。

  • 为缓存库提供通用接口。
  • 支持缓存库APC、memcached和Redis。
  • 支持命名空间。(使用命名空间删除)

安装

使用Composer(推荐)

{
    "require": {
        "snakano/cache-store": "1.*"
    }
}

或克隆此仓库。

使用方法

// configure cache setting.
Domino\CacheStore\Factory::setOptions(
    array(
        array(
            'storage'     => 'apc'
            'default_ttl' => 360
        ),
        array(
            'storage'     => 'memcached',
            'prefix'      => 'domino_test',
            'default_ttl' => 360,
            'servers'     => array(
                array('server1', 11211, 20),
                array('server2', 11211, 80)
            )
        ),
        array(
            'storage'     => 'redis',
            'prefix'      => 'domino_test',
            'host'        => '127.0.0.1',
            'port'        => 6379,
            'default_ttl' => 360
        )
    )
);

// factory cache storage
$storage = Domino\CacheStore\Factory::factory('memcached');

// register custom cache storage
$storage = Domino\CacheStore\Factory::registerStorage('acme_storage', 'Acme\Storage\AcmeStorage');

// set data
$storage->set('ns1', 'key1', 'value1');
$storage->set('ns1', 'key2', 'value2');
$storage->set('ns2', 'key1', 'value1');
$storage->set('ns2', 'key2', 'value2', 10); // specify ttl

// get data
$storage->get('ns1', 'key1');
# => 'value1'

// delete by namespace and key
$storage->clear("ns1", 'key1');
$storage->get('ns1', 'key1');
# => null

// delete by namespace
$storage->clearByNamespace('ns1');
$storage->get('ns1', 'key2');
# => null
$storage->get('ns2', 'key1');
# => 'value1'

// delete all
$storage->clearAll();
$storage->get('ns2', 'key1');
# => null

// disable caching at runtime
Domino\CacheStore\Factory::disableCaching();
// since caching is disabled, the factory will return an instance of "NoCache"
$storage = Domino\CacheStore\Factory::factory('memcached');

// enable caching at runtime
Domino\CacheStore\Factory::enableCaching();
// since caching is enabled again, the factory will return an instance of "Memcached"
$storage = Domino\CacheStore\Factory::factory('memcached');

许可证

MIT许可证。