生成zend cache ID 用于清除指定的zend cache page.
2008-07-02 11:43
267 查看
/*
*
使用方法,清除对'/zhidao/?page=1&id=2'的缓存。
Zend_Loader::loadClass('Custom_Cacheid');
$cacheId=Custom_Cacheid::makeId('/zhidao/?page=1&id=2',Config::$cacheOptions);
$GLOBALS['cache']->remove($cacheId);
*/
class Custom_Cacheid
{
static $Get=NULL;
static $Post=NULL;
static $Session=NULL;
static $Cookie=NULL;
/**
* 根据提供的url,及缓存选项(本系统中是Config::$cacheOptions)返回缓存的id
* 通过设置$param来改变$_GET,$_POST,$_COOKIE,$_SESSION值,
例如:
$param = array(
'Post'=>array('id'=>1,'page'=>1)
);
表示设定$_POST参数为array('id'=>1,'page'=>1)。
注意:get参数是自动根据url获取的
注意:$url 中 ? 后的get参数不能错误比如 ?a==&page=1;str2arr只是一个简单的函数,它可能与php分析方法不同,导致不能取得正确的id
*/
static public function makeId($url, $activeOptions, $param=array())
{
foreach($param as $key=>$value){
self::$$key = $value;
}
if(strpos($url,'?')!==false){
self::$Get = self::str2arr(substr($url,strpos($url,'?')));
}
$tmp = $url;
foreach (array('Get', 'Post', 'Session', 'Files', 'Cookie') as $arrayName) {
$tmp2 = self::_makePartialId(
$arrayName,
isset($activeOptions['cache_with_' . strtolower($arrayName) . '_variables'])?$activeOptions['cache_with_' . strtolower($arrayName) . '_variables']:false,
isset($activeOptions['make_id_with_' . strtolower($arrayName) . '_variables'])?$activeOptions['make_id_with_' . strtolower($arrayName) . '_variables']:true
);
if ($tmp2===false) {
return false;
}
$tmp = $tmp . $tmp2;
}
return md5($tmp);
}
/**
* Make a partial id depending on options
*
* @param string $arrayName Superglobal array name
* @param bool $bool1 If true, cache is still on even if there are some variables in the superglobal array
* @param bool $bool2 If true, we have to use the content of the superglobal array to make a partial id
* @return mixed|false Partial id (string) or false if the cache should have not to be used
*/
private static function _makePartialId($arrayName, $bool1, $bool2)
{
switch ($arrayName) {
case 'Get':
$var = !empty(self::$Get)?self::$Get:$_GET;
break;
case 'Post':
$var = !empty(self::$Post)?self::$Post:$_POST;
break;
case 'Session':
if(!empty(self::$Session)){
$var = self::$Session;
}else{
if (isset($_SESSION)) {
$var = $_SESSION;
} else {
$var = null;
}
}
break;
case 'Cookie':
if(!empty(self::$Cookie)){
$var = self::$Cookie;
}else{
if (isset($_COOKIE)) {
$var = $_COOKIE;
} else {
$var = null;
}
}
break;
case 'Files':
$var = $_FILES;
break;
default:
return false;
}
if ($bool1) {
if ($bool2) {
return serialize($var);
}
return '';
}
if (count($var) > 0) {
echo $arrayName.'<hr>';
return false;
}
return '';
}
/*
* 把字符串转换成数组
例如传入 ?page=1&id=1 将返回array('page'=>1,'id'=>1)
*/
static function str2arr($str){
$str=preg_replace("/^/?/","",$str);
$tmp=split("&",$str);
$return=array();
foreach($tmp as $t){
$string = split("=",$t);
if(count($string)==2){
$return[$string[0]] = $string[1];
}
}
return $return;
}
}
Config::$cacheOptions
static $cacheOptions = array(
'cache' => true,
'cache_with_get_variables' => true, //当有get变量时也进行缓存
'cache_with_post_variables' => false, //当有post变量时不进行缓存
'cache_with_cookie_variables' => true, //当有cookie变量时也进行缓存
'cache_with_session_variables' => true, //当有session变量时也进行缓存
'make_id_with_cookie_variables' => false,
'make_id_with_session_variables' => false,
'make_id_with_post_variables' => false,
'make_id_with_files_variables' => false,
'make_id_with_get_variables' => true
);
*
使用方法,清除对'/zhidao/?page=1&id=2'的缓存。
Zend_Loader::loadClass('Custom_Cacheid');
$cacheId=Custom_Cacheid::makeId('/zhidao/?page=1&id=2',Config::$cacheOptions);
$GLOBALS['cache']->remove($cacheId);
*/
class Custom_Cacheid
{
static $Get=NULL;
static $Post=NULL;
static $Session=NULL;
static $Cookie=NULL;
/**
* 根据提供的url,及缓存选项(本系统中是Config::$cacheOptions)返回缓存的id
* 通过设置$param来改变$_GET,$_POST,$_COOKIE,$_SESSION值,
例如:
$param = array(
'Post'=>array('id'=>1,'page'=>1)
);
表示设定$_POST参数为array('id'=>1,'page'=>1)。
注意:get参数是自动根据url获取的
注意:$url 中 ? 后的get参数不能错误比如 ?a==&page=1;str2arr只是一个简单的函数,它可能与php分析方法不同,导致不能取得正确的id
*/
static public function makeId($url, $activeOptions, $param=array())
{
foreach($param as $key=>$value){
self::$$key = $value;
}
if(strpos($url,'?')!==false){
self::$Get = self::str2arr(substr($url,strpos($url,'?')));
}
$tmp = $url;
foreach (array('Get', 'Post', 'Session', 'Files', 'Cookie') as $arrayName) {
$tmp2 = self::_makePartialId(
$arrayName,
isset($activeOptions['cache_with_' . strtolower($arrayName) . '_variables'])?$activeOptions['cache_with_' . strtolower($arrayName) . '_variables']:false,
isset($activeOptions['make_id_with_' . strtolower($arrayName) . '_variables'])?$activeOptions['make_id_with_' . strtolower($arrayName) . '_variables']:true
);
if ($tmp2===false) {
return false;
}
$tmp = $tmp . $tmp2;
}
return md5($tmp);
}
/**
* Make a partial id depending on options
*
* @param string $arrayName Superglobal array name
* @param bool $bool1 If true, cache is still on even if there are some variables in the superglobal array
* @param bool $bool2 If true, we have to use the content of the superglobal array to make a partial id
* @return mixed|false Partial id (string) or false if the cache should have not to be used
*/
private static function _makePartialId($arrayName, $bool1, $bool2)
{
switch ($arrayName) {
case 'Get':
$var = !empty(self::$Get)?self::$Get:$_GET;
break;
case 'Post':
$var = !empty(self::$Post)?self::$Post:$_POST;
break;
case 'Session':
if(!empty(self::$Session)){
$var = self::$Session;
}else{
if (isset($_SESSION)) {
$var = $_SESSION;
} else {
$var = null;
}
}
break;
case 'Cookie':
if(!empty(self::$Cookie)){
$var = self::$Cookie;
}else{
if (isset($_COOKIE)) {
$var = $_COOKIE;
} else {
$var = null;
}
}
break;
case 'Files':
$var = $_FILES;
break;
default:
return false;
}
if ($bool1) {
if ($bool2) {
return serialize($var);
}
return '';
}
if (count($var) > 0) {
echo $arrayName.'<hr>';
return false;
}
return '';
}
/*
* 把字符串转换成数组
例如传入 ?page=1&id=1 将返回array('page'=>1,'id'=>1)
*/
static function str2arr($str){
$str=preg_replace("/^/?/","",$str);
$tmp=split("&",$str);
$return=array();
foreach($tmp as $t){
$string = split("=",$t);
if(count($string)==2){
$return[$string[0]] = $string[1];
}
}
return $return;
}
}
Config::$cacheOptions
static $cacheOptions = array(
'cache' => true,
'cache_with_get_variables' => true, //当有get变量时也进行缓存
'cache_with_post_variables' => false, //当有post变量时不进行缓存
'cache_with_cookie_variables' => true, //当有cookie变量时也进行缓存
'cache_with_session_variables' => true, //当有session变量时也进行缓存
'make_id_with_cookie_variables' => false,
'make_id_with_session_variables' => false,
'make_id_with_post_variables' => false,
'make_id_with_files_variables' => false,
'make_id_with_get_variables' => true
);
相关文章推荐
- 生成zend cache ID 用于清除指定的zend cache page.
- 生成zend cache ID 用于清除指定的zend cache page.
- dom4j相关操作,生成指定的xml文档-修改id为2的学生姓名-删除id为2的学生
- Java用于生成唯一ID
- System.Web.UI.Page.Cache 页面 缓存 清除
- java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题
- 反回指定页的记录,用于静太页的生成
- 用于生成Service所需的各种ID
- JavaScript- 通常用于操作 HTML 元素\通过指定的 id 来访问 HTML 元素,并改变其内容\写到文档输出
- ElasticSearch5:Document id的手动指定和自动生成两种解析
- Python实现的用于从布局文件的id生成成员变量声明和findViewById语句的Android开发小工具
- Jsp+Ext2.0用递归实现任意指定节点ID的无限级Tree的生成
- java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题
- Trojan.StartPage 生成%temp%/se.dll的清除方法
- 根据id生成指定位数的序号
- Zend_Cache_Frontend_Page使用问题
- nginx反向代理,动静态请求分离,以及nginx缓存应用,以及使用ngx_cache_purge清除指定URL
- cocos2d-x生成的ios应用和安卓应用的ID(用于区分不同应用的标识)
- 生成丶清除指定目录下的HTML文件
- 关于Zend ID的生成方法