search
Homephp教程php手册使用static来避免“重复读”

使用static来避免“重复读”

Jun 13, 2016 am 11:38 AM
staticwebuseexistcomplexobjectoperatedataofProgram developmentavoiduseFor

在较复杂的web程序开发中,由于采用面向对象的数据操作方式,或者业务逻辑太过复杂,开发人员在开发过程中,经常会无意识的重复读取数据。
比如:
$result1 = tableobjectPeer::getResult($var1,$var2,$var3);
开发人员在需要的该table数据的时候,经常直接调用 tableobjectPeer::getResult方法。
又或者程序在进行forward的时候,也会导致重复调用  tableobjectPeer::getResult 方法,导致“重复读”。

要避免类似的“重复读”,最主要的办法是,开发人员在代码开发的时候,就要有“避免重复读”的意识。
其实只要在:
$result1 = tableobjectPeer::getResult($var1,$var2,$var3);
之后,需要的地方进行
$result2 = $result1;
$result3 = $result1;
即可。这样子就可以避免掉大量的“重复读”了。
但是如果开放人员一开始没有这样做,那要进行这方面的重构可能是很大的工作量。
另外,由于框架中的forward()也容易造成“重复读”。如果“重复读”是forward()造成的,那该方法就不可行了(这点也许和不同的开发语言,不同的开发框架有关,在php的symfony框架中是如此)。

因此,在采用上面的方法进行优化的同时,对于一些较复杂的情况,决定采用另一种方式:使用static,设置变量为静态变量,来避免重复读取数据

以下为引用的内容:
//要改写的函数也请加上$is_static=1变量,用来控制是否开启 static。
function staticFunc ($var1,$var2,$var3,$is_static=1)
{
if ( $is_static == 1 )//默认需要缓存函数结果
{
static $result_array;//该数组用来保存函数的结果,支持不同参数的结果缓存
$vars_string = serialize( func_get_args() );

if ( empty( $result_array ) )//第一次运行需要初始化
{
$result_array = array();
}

if ( array_key_exists( $vars_string, $result_array ) )//参数已经存在
{
return $result_array[$vars_string];//返回静态变量中已经保存的结果
}else//参数不存在
{
$result_array[$vars_string] = '';//后面会把结果补充进来
}
}else//不利用static 缓冲结果
{
if ( empty( $result_array ) )
{
$result_array = array();
}
}

$result_array[$vars_string] = rand();//获取结果,请把获取的代码放在此处即可
return $result_array[$vars_string];
}

echo staticFunc(1,2,3);
echo "
";
echo staticFunc(2,2,2);
echo "
";
echo staticFunc(1,2,3);
echo "
";
echo staticFunc(2,2,2);
echo "
";
echo staticFunc(3,3,3);
echo "
";
echo staticFunc(3,3,3,0);
echo "
";
?>

运行上述代码,产生的结果类似:
16667
8888
16667
8888
2193
1014
可以看出,第1行和第3行的结果一致,第2行和第4行的结果一致,说明只要函数的参数相同,函数结果就进行了有效的“cache”。
从第4行和第5行,可以看出,通过设置$is_static变量能有效控制是否开启“cache”。

补充:上述使用static方法,可以有效的避免在一个线程内,重复读取数据,但是该cache只存在一个线程内,不同线程是相互独立的。虽然只是线程内的函数结果“cache”,但是其原理和其他方式的cache是类似的,就是要针对不同的参数(不同的情况)构造出cache的key。

转自:http://www.cnblogs.com/rethink/

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor