PHP WeChat development uses Cache to solve data caching

高洛峰
Release: 2017-03-28 14:03:50
Original
2462 people have browsed it

This article mainly introduces how to solve the problem of datacaching during PHPWeChat development. Here we use the Cache class as an example. It has reference value and is useful for those who are interested. Partners can refer to the following

When using php for WeChat development, I encountered the problem of long-term storage of access_token. In the past, I used the Cache in the framework to directly set and get it. That's it. Now that there is no framework available, I have to write a cache myself for temporary use.

This Cache class is used to cache some time-sensitive data, such as access_token of WeChat basic interface , access_token of web page Auth verification, etc.

The following code Use local files to cache data.

//测试
 $cache = new Cache();
 $cache->dir = "../cc/";
 //$cache->setCache("zhang", "zhangsan", 100);
 echo $cache->getCache("zhang");
 //$cache->removeCache("zhang");
 
 $cache->setCache("liu", "liuqi", 100);
 echo $cache->getCache("liu");

 class Cache{
 public $cacheFile = "cache.json"; //文件
 public $dir = "./cach2/"; //目录

 //缓存
 public function setCache($name, $val, $expires_time){
 $file = $this->hasFile();
 //字符串转数组
 $str = file_get_contents($file);
 $arr = json_decode($str, true);
 
 //值为空,则移除该缓存
 if(empty($val)){
 unset($arr[$name]);
 }else{
 $arr[$name] = array("value"=>$val, "expires_time"=>$expires_time, "add_time"=>time());
 } 
 //数组转字符串
 $str = json_encode($arr);
 file_put_contents($file, $str);
 }
 public function getCache($name){
 $file = $this->hasFile();
 
 //字符串转数组
 $allArr = json_decode($str, true);
 $arr = $allArr[$name];

 if(!$arr || time() > ($arr["expires_time"] + $arr["add_time"])){
 $this->removeCache($name); //过期移除
 return false;
 }
 return $arr["value"];
 }
 public function removeCache($name){
 $this->setCache($name, '', 0);
 }
 
 private function hasFile(){
 //如果不存在缓存文件,则创建一个
 if(!file_exists($this->dir)){
 mkdir($this->dir);
 }
 if(!file_exists($this->dir . $this->cacheFile)){
 touch($this->dir . $this->cacheFile);
 }
 return $this->dir . $this->cacheFile;
 }
}
Copy after login

The Cache class above has three operations: set, get, and remove. In addition, you can also customize the save path of cache files, just set the dir attribute of Cache.

The above is the method of data caching during PHP WeChat development. I hope it will be helpful to everyone's learning.

The above is the detailed content of PHP WeChat development uses Cache to solve data caching. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template