另一个缓存实现

dev-master 2013-04-06 07:37 UTC

This package is auto-updated.

Last update: 2024-09-15 03:49:47 UTC


README

另一个缓存实现。

Build Status

使用方法

基本使用适用于所有缓存存储选项

use Websoftwares\Cache, Websoftwares\Storage\File;

$cache = Cache::storage(new File());

$cache->save('key',["a","b","c"]);

// Retrieve the cache

$cache->get('key');

存储

可用的存储选项

  • Apc (将缓存保存到apc)
  • 文件 (将缓存保存到文件)
  • Memcache (将缓存保存到memcache实例)
  • Memcached (将缓存保存到memcached实例)
  • MongoDB (将缓存保存到mongo实例)
  • Redis (将缓存保存到redis实例)
  • Riak (将缓存保存到riak实例)

Apc

use Websoftwares\Cache, Websoftwares\Storage\Apc;

$apc = Cache::storage(new Apc())
 ->setExpiration(86400);

$apc->save('key',["a","b","c"]);

// Retrieve the cache

$apc->get('key');

文件

use Websoftwares\Cache, Websoftwares\Storage\File;

$cache = Cache::storage(new File())
 ->setPath('/super/spot')
 ->setExpiration(86400);

$cache->save('key',["a","b","c"]);

// Retrieve the cache

$cache->get('key');

Memcache

这需要您已安装PHP memcache扩展。

例如,在Debian/Ubuntu系统上安装如下(需要管理员密码)。

sudo apt-get install php5-memcache

use Websoftwares\Cache, Websoftwares\Storage\Memcache;

$memcache = Cache::storage(new Memcache())
    ->setConnection(function() {
        $instance = new \Memcache;
        $instance->connect('localhost','11211');
        return $instance;
    })
    ->setExpiration(86400);

$memcache->save('key',["a","b","c"]);

// Retrieve the cache

$memcache->get('key');

Memcached

这需要您已安装PHP memcached扩展。

例如,在Debian/Ubuntu系统上安装如下(需要管理员密码)。

sudo apt-get install php5-memcached

use Websoftwares\Cache, Websoftwares\Storage\Memcached;

$memcached = Cache::storage(new Memcached())
    ->setConnection(function() {
        $instance = new \Memcached();
        $instance->addServer("localhost", 11211);
        return $instance;
    })
    ->setExpiration(86400);

$memcached->save('key',["a","b","c"]);

// Retrieve the cache

$memcached->get('key');

Mongo

此存储选项使用“ensureIndex”方法选项“expireAfterSeconds”。

只有在满足以下要求的情况下才能使用此选项。

要求

  • 已安装最新版本的PHP Mongo扩展
  • mongoDB守护进程版本2.2+ | 了解更多

在Debian/Ubuntu系统上运行以下命令安装mongo扩展(需要管理员密码)。

sudo pecl install mongo
use Websoftwares\Cache, Websoftwares\Storage\Mongo;

$mongo = Cache::storage(new Mongo())
    ->setConnection(function() {
        $m = new \MongoClient();
        $db = $m->mongocache;
        return $db;
    })
    ->setCollection('test')
    ->setExpiration(86400);

$mongo->save('key',["a","b","c"]);

// Retrieve the cache

$mongo->get('key');

Redis

这需要您已安装PHP Predis 包。

use Websoftwares\Cache, Websoftwares\Storage\Redis;

$redis = Cache::storage(new Redis())
    ->setConnection(function() {
        $client = new \Predis\Client([
            'scheme'   => 'tcp',
            'host'     => '127.0.0.1',
            'port'     => 6379,
        ]);
        return $client;
    })
    ->setExpiration(86400);

$redis->save('key',["a","b","c"]);

// Retrieve the cache

$redis->get('key');

Riak

这需要您已安装PHP Riak 官方包。

use Websoftwares\Cache, Websoftwares\Storage\Riak;

$riak = Cache::storage(new Riak())
    ->setConnection(function() {
        $client = new \Basho\Riak\Riak('127.0.0.1', 8098);
        return $client;
    })
    ->setBucket('testBucket')
    ->setExpiration(86400);

$riak->save('key',["a","b","c"]);

// Retrieve the cache

$riak->get('key');