Home  >  Article  >  Backend Development  >  Develop Android server using php to create dynamic cache_PHP tutorial

Develop Android server using php to create dynamic cache_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:11:31844browse

Developing Android server using php to create dynamic cache

Why there is a cache: Reduce database server pressure.
1. Static cache:
Static files saved on the server disk, use PHP to generate data and put them in the static files
php operation cache:
1. Generate cache
2. Get cache

3. Delete cache


Generate cache file:


file.php operates static cache classes

<?php

class File {//操作静态缓存的业务
	private $_dir;

	const EXT = &#39;.txt&#39;;

	public function __construct() {
		//获取文件当前目录,把缓存文件放到当前目录files下
		$this->_dir = dirname(__FILE__) . &#39;/files/&#39;;
	}
	//key是缓存文件文件名,value是缓存数据
	public function cacheData($key, $value = &#39;&#39;, $path=&#39;&#39;) {
		$filename = $this->_dir . $path . $key . self::EXT;

		if($value !== &#39;&#39;) { // 将value值写入缓存
			 
			 //获取目录,判断如果目录不存在,创建目录
			$dir = dirname($filename);
			if(!is_dir($dir)) {
				mkdir($dir, 0777);//生成目录,给出权限
			}
			//第一个参数文件名,第二个数据(String),把数组vlaue转换成字符串
			return file_put_contents($filename,json_encode($value));
		}
		
	}
}

testfile.php


<?php
require_once(&#39;./file.php&#39;);
$data=array(
&#39;id&#39;=>1,
&#39;name&#39;=>&#39;david&#39;,
&#39;type&#39;=>array(4,5,6)
);
$file=new File();
if($file->cacheData(&#39;davidcache&#39;,$data)){
echo "success";}else{
echo "error";
}

If the generation is successful, success

will be displayed.

This creates a cache file named davidchche.txt in the file directory of the current file directory.



Advanced version: also implements cache reading and deletion


file.php

<?php

class File {//操作静态缓存的业务
	private $_dir;
	const EXT = &#39;.txt&#39;;
	public function __construct() {
		//获取文件当前目录,把缓存文件放到当前目录files下
		$this->_dir = dirname(__FILE__) . &#39;/files/&#39;;
	}
	//key是缓存文件文件名,value是缓存数据
	public function cacheData($key, $value = &#39;&#39;, $path=&#39;&#39;) {
		$filename = $this->_dir . $path . $key . self::EXT;

		if($value !== &#39;&#39;) { // 将value值写入缓存
			 
             if(is_null($value)) {//如果value值穿null则删除这个缓存文件
				return @unlink($filename);
			}
			 //获取目录,判断如果目录不存在,创建目录
			$dir = dirname($filename);
			if(!is_dir($dir)) {
				mkdir($dir, 0777);//生成目录,给出权限
			}
			//第一个参数文件名,第二个数据(String),把数组vlaue转换成字符串
			return file_put_contents($filename,json_encode($value));
		}

		if(!is_file($filename))//读取缓存文件
		{
			return FALSE;
		}else{
            return json_decode(file_get_contents($filename),true);
		}
		
	}
}

According to the file class, if value is empty='', read cached data,
If value is null, delete cached data,
If value is not empty or null, create cache data

testfile.php


<?php
require_once(&#39;./file.php&#39;);
$data=array(
&#39;id&#39;=>1,
&#39;name&#39;=>&#39;david&#39;,
&#39;type&#39;=>array(4,5,6)
);
$file=new File();
//删除缓存文件
if($file->cacheData(&#39;davidcache&#39;,null)){
	//if($file->cacheData(&#39;davidcache&#39;)){
	//var_dump($file->cacheData(&#39;davidcache&#39;));
	//exit;
echo "success";}else{
echo "error";
}
    /*下面这个是读取缓存时使用的
	if($file->cacheData(&#39;davidcache&#39;)){
	var_dump($file->cacheData(&#39;davidcache&#39;));
	exit;
echo "success";}else{
echo "error";
}*/

/*下面这个是创建缓存调用的
if($file->cacheData(&#39;davidcache&#39;,$data)){
echo "success";}else{
echo "error";
}*/

//根据file类,如果value为空=&#39;&#39;,读取缓存数据,
//如果value为null,删除缓存数据,
//如果value不为空,也不是null,那么创建缓存数据


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/929777.htmlTechArticleThe use of php to develop Android servers to create dynamic caches. Why there is a cache: to reduce the pressure on the database server. 1. Static cache: static files saved on the server disk, generated with php...
Statement:
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