In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces PHP how to achieve data cache class, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The idea goes like this:
For general variables, change the variable to PHP format and write it to a file. As long as include is used, it is equivalent to loading the PHP data cache class.
For array variables, convert array to a string defined by PHP language array, write it to a file, and as long as include is used, it is equivalent to loading cache.
PHP data cache class time control, by comparing the creation time of the cache file with the current time, if it is not the update time, read the cache directly; if it is the update time, query the database, return the data, and then update the cache. (not yet implemented)
Here is my PHP-kcache class (PHP_kcache_class.PHP):
Note: if it is a cache string, please write an extra'\ 'for the escape character, that is, "\ n" should be written as "\ n".
/ *
/ / PHP-kcache class v_0.1
/ / Author: kangzj
/ / Email: kangzj@mail.bnu.edu.cn
/ / Blog: http://kangzj.net.ru
/ / the author does not guarantee that there is no bug in this program, for using this program
/ / do not bear any responsibility for any problems caused.
, /
Class PHP_kcache {
/ / relative or absolute directory, do not add'/'at the end
Var $cache_dir ='. / cache'
Var $cache_extension = '.cache.PHP'
Function set_cache ($name, $value) {
$pre = "
< ?\n//Cache Created at: " .date('Y-m-d H:i:s')."\n"; if(!is_array($value)){ $value = $value; $str = "\$$name = '$value';"; }else{ $str = "\$$name = " . $this->Arrayeval ($value). ';'
}
$end = "\ n? >"
Echo $cache = $pre. $str. $end
$cache_file = $this- > cache_dir.
'/'. $name. $this- > cache_extension
If ($fp = @ fopen ($cache_file, 'wb')) {
Fwrite ($fp, $cache)
Fclose ($fp)
Return true
} else {
Echo $cache_file
Exit ('Can not write to cache files
Please check cache directory')
Return false
}
}
/ / change array into a string, from discuz!
Function arrayeval ($array, $level = 0) {
If (! is_array ($array)) {
Return ". $array."
}
$space =''
For ($I = 0; $I $val) {
$key = is_string ($key)?'\ '.addcslopes
($key,'\').'\': $key
$val =! is_array ($val) &
(! preg_match ("/ ^\ -? [1-9]\ dwells /", $val)
| | strlen ($val) > 12)?\ '.addcslopes |
($val,'\').'\': $val
If (is_array ($val)) {
$evaluate. = "$comma$key = >".
Arrayeval ($val, $level + 1)
} else {
$evaluate. = "$comma$key = > $val"
}
$comma = ",\ n$space"
}
}
$evaluate. = "\ n$space)"
Return $evaluate
}
}
The simplest PHP data cache class invokes the method:
Include'. / PHP_kcache_class.PHP'; $pc = new PHP_kcache; $a = array ('averse,' baked,'c'); $pc- > set_cache ('averse, addslashes ($a)) Thank you for reading this article carefully. I hope the article "how to implement data caching in PHP" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.