ans / cache
PHP 缓存库
dev-master
2013-08-15 12:03 UTC
Requires
- php: >=5.3.3
This package is auto-updated.
Last update: 2024-09-11 14:42:42 UTC
README
此脚本作为 APC、Memcache、Memcached 和文件存储数据的简单接口。
您可以将不同的数据存储到不同的缓存方法中(例如,在 settings-example.php 中的一些示例)。
示例
文件缓存
<?php
include (__DIR__.'/libs/ANS/Cache/Cache.php');
include (__DIR__.'/settings-example.php');
$Cache = new \ANS\Cache\Cache($settings['js']);
if ($Cache->exists('js-files')) {
$content = $Cache->get('js-files');
} else {
$content = file_get_contents('https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.7.2/jquery.min.js');
$content .= file_get_contents('https://ajax.googleapis.ac.cn/ajax/libs/jqueryui/1.8/jquery-ui.min.js');
$content .= file_get_contents('http://modernizr.com/i/js/modernizr.com-custom-1.6.js');
// Cache expired time is loaded from settings
// but you can set your own time in seconds from now
// Third parameter is optional
$Cache->set('js-files', $content, $custom_time);
}
数据库查询缓存(在 APC 中)
<?php
include (__DIR__.'/libs/ANS/Cache/Cache.php');
include (__DIR__.'/settings-example.php');
$Cache = new \ANS\Cache\Cache($settings['db']);
$query = 'SELECT * FROM Users;';
$cache_key = md5($query);
if ($Cache->exists($cache_key)) {
$rows = $Cache->get($cache_key);
} else {
$rows = mysql_fetch_assoc(mysql_query($query));
// Cache expired time is loaded from settings
// but you can set your own time in seconds from now
// Third parameter is optional
$Cache->set($cache_key, $rows, $custom_time);
}
配置缓存(在 Memcache 中)
<?php
include (__DIR__.'/libs/ANS/Cache/Cache.php');
include (__DIR__.'/settings-example.php');
$Cache = new \ANS\Cache\Cache($settings['config']);
$config_files = array(
'db', 'paths', 'events', 'routes', 'templates', 'mail',
'session', 'tables', 'actions', 'css', 'languages'
);
$config_key = md5(serialize($config_files));
if ($Cache->exists($cache_key)) {
$configuration = $Cache->get($cache_key);
} else {
$configuration = array();
foreach ($config_files as $file) {
if (is_file(__DIR__.'/config/'.$file.'.php')) {
$configuration = array_replace_recursive($configuration, include(__DIR__.'/config/'.$file.'.php'));
}
}
// Cache expired time is loaded from settings
// but you can set your own time in seconds from now
// Third parameter is optional
$Cache->set($cache_key, $configuration, $custom_time);
}