PHP 网站缓存库,用于 HTML 和对象

此包的官方仓库似乎已不存在,因此该包已被冻结。

1.2.2 2016-09-16 14:19 UTC

This package is not auto-updated.

Last update: 2024-01-20 12:26:36 UTC


README

Latest Stable Version Total Downloads Build Status

PHP 缓存

PHP 缓存是一个简单的 HTML 和对象缓存库。

安装

将以下内容添加到您的 composer.json 中

{
    "require": {
        "rubyrainbows/cache": "1.1.*"
    }
}

设置

注意: 我构建了这个库时对不同的缓存提供商持中立态度,但由于我主要使用 redis,所以 redis 是唯一支持的缓存。

<?php

use RubyRainbows\Cache\Cache;

// a redis cache at localhost and database of 0
$cache = new Cache ();

// a more specified cache
$cache = new Cache( Cache::REDIS_CACHE, 'tcp://127.0.0.1:6379?database=0');

使用缓存对象

缓存对象是一个 PHP 对象,其值存储在缓存中以方便存储和检索。在 redis 中,这是通过哈希数据类型完成的。当你用正确的缓存键重新创建对象时,对象将自动恢复到其之前的状态。

<?php

use RubyRainbows\Cache\Cache;

$cache = new Cache();

// a basic cached object
$object = $cache->createObject('cache_key'); 

// a cached object that expires in 10 seconds
$object = $cache->createObject('cache_key', 10);

// a cached object with a value foo that equals bar.
$object = $cache->createObject('cache_key', 0, ['foo' => 'bar']);

// filling the object with an array of variables
$object->fill(['foo' => 'bar']);

// setting a variable directly
$object->set('foo', 'bar');

// getting a variable
$foo = $object->get('foo');

使用缓存树

缓存树是将树对象存储在缓存中的方法。如果使用相同的键创建,这些树也将自动恢复到之前的状态。

<?php

use RubyRainbows\Cache\Cache;

$cache = new Cache();

// a basic tree
$tree = $cache->createTree('cached_key');

// a tree that expires after 10 seconds
$tree = $cache->createTree('cached_key', 10);

// a root node with the id of 1 ('the id can also be a string')
$root = $tree->makeRootNode(1);

// a root node with the id of 1 and a value of foo that equals bar
$root = $tree->makeRootNode(1, ['foo' => 'bar']);

// set the value foo to bar
$root->set('foo', 'bar');

// get the value stored under bar
$root->get('foo');

// add a new child
$node = $root->addChild(2);

// add a new child with the value foo equaling bar
$node = $root->addChild(2, ['foo' => 'bar']);

// saving the tree
$tree->save();

// turns the tree to an array starting from root
$tree->getArray();

// turns the tree to an array starting at the child with the id of 2
$tree->getArray(2);