vicgarcia / kaavii
KaaVii(发音为 kay-vee)是一个包,用于提供与Redis交互的组件,用于缓存和键值数据存储
该包的规范存储库似乎已丢失,因此该包已被冻结。
Requires
- php: >=5.4.0
- ext-redis: *
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-06-26 16:43:52 UTC
README
Kaavii
Kaavii(发音为 kay-vee)是一个简单的库,用于提供与Redis交互的功能,用于缓存和键值数据存储。Kaavii旨在提供一套非常基本的工具,非常适合简单应用或快速原型设计。
Kaavii需要PHP >=5.4和Redis扩展。
我创建Kaavii是因为我喜欢使用Redis。缓存和简单存储是我最常使用Redis的两种情况,我想有一种一致的方式来设置和为这些情况使用Redis。
将此添加到composer.json的require部分并更新Composer
"vicgarcia/kaavii": "1.0.*"
Kaavii\Redis
Kaavii\Redis类将是您开始使用Kaavii的地方。此类提供了一个全局可访问的数组,用于Redis配置数据,以及访问用于创建\Redis客户端对象的工厂方法。
要使用它,您提供一个配置数据数组,无论是作为全局配置还是作为工厂方法调用的参数。
// config array looks like so ...
$config = array(
'scheme' => 'tcp',
'host' => <ip or hostname>,
'port' => <port>,
'database' => <# of db to use>,
'password' => <password>,
);
// nb: see code for use w/ unix socket
// using Kaavii global configuration for Redis
// with a file ...
Kaavii\Redis::$config = require 'config/redis.php';
// with an array ...
Kaavii\Redis::$config = [
'sheme' => tcp,
'host' => '127.0.0.1',
'port' => '6379'
];
// getting a redis client object
$redis = Kaavii\Redis::connect();
// per-instance config can be provided as method param
$redis = Kaavii\Redis::connect($config);
// the $redis object is the PHP extension \Redis class
使用工厂方法创建一个新的\Redis客户端。\Redis客户端作为依赖项注入到其他Kaavii组件的构造函数中。
Kaavii\Cache和Kaavii\NoCache
Kaavii\Cache对象用于将数据缓存到Redis中。
在存储到Redis缓存之前,对象会被序列化,并且在检索时会反序列化。任何支持序列化的对象都可以被缓存。
// configure redis and connect
Kaavii\Redis::$config = require 'config/redis.php';
$redis = Kaavii\Redis::connect();
// a prefix is prepended to keys, seperated by a colon
// this type of namespacing is convention with Redis
$prefix = 'cache';
// create cache object
$cache = new Kaavii\Cache($redis, $prefix);
// a simple cache block
if ( ($lifestream = $cache->load('lifestream')) === false ) {
$lifestream = (new Lifestream())->getCurrent();
$cache->save('lifestream', $lifestream, 14400);
}
// delete a cached value by key
$cache->delete('lifestream');
// clear all cached values, requires a prefix was used above
$cache->clear();
Kaavii\NoCache对象用于在您想禁用缓存功能时使用。当使用此对象时,当对象被保存到缓存中时不会采取任何操作,并且对象永远不会从缓存中检索。
// a NoCache object needs no Redis client
$env = 'dev';
$cache = new Kaavii\NoCache;
if ($env == 'prod') {
// not called when $env is 'dev' as it is
$cache = new Kaavii\Cache(Kaavii\Redis::connect(), 'cache');
}
// with NoCache, the code within the block will always execute
if ( ($weatherchart = $cache->load('weatherchart')) === false ) {
$weatherchart = (new WeatherChart())->getDaily();
// with NoCache, nothing is 'saved', the method simply returns
$cache->save('weatherchart', $weatherchart);
}
Kaavii\Storage
Kaavii\Storage组件是一种简单的方式来使用Redis作为键值存储。它通过使用简单的get()和set()方法提供命名空间键和值数据的序列化处理。
// configure redis and connect
Kaavii\Redis::$config = require 'config/redis.php';
$redis = Kaavii\Redis::connect();
// create storage object
$storage = new Kaavii\Storage($redis, 'prefix');
// get a value from storage
$value = $storage->get('key');
// save value to storage
$storage->set('key', $value);
// delete a key
$storage->delete('key');
// get all keys, requires a prefix be used above
$keys = $storage->keys();
与Slim框架一起使用
KaaVii主要是为了我在Slim框架应用中使用而创建的。
// static config for redis is provided in bootstrap.php
// setup a singleton method for the cache
$app->container->singleton('cache', function() use{
if ($GLOBALS['environment'] == 'production')
return new Kaavii\Cache( Kaavii\Redis::connect() );
return new Kaavii\NoCache;
}
// now you can use the cache as part of the app object
$app->get('/stations', function() use ($app) {
if (($stations = $app->cache->load('stations')) === false) {
$stations = $app->divvy->getStationsData();
$app->cache->save('stations', $stations, 600);
}
echo json_encode($stations);
});