I want to use global variable to store&read data in Yii.when I access the view.php,the readtimes will be incremented.but it always echo "1" when i access the view.php.
component:
readTimes.php:
<?php
class ReadTimes extends CApplicationComponent{
private static $readTimes=0;
public static function incReadTimes(){
self::$readTimes++;
}
public static function showReadTimes(){
return self::$readTimes;
}
}
class ReadTimes extends CApplicationComponent{
private static $readTimes;
public static function incReadTimes(){
if (self::$readTimes== null){
self::$readTimes=0;
}
self::$readTimes++;
}
public static function showReadTimes(){
return self::$readTimes;
}
}
Each time you refresh the page you’re starting a brand new request and all of the classes are reinitialised. You need to store that value somewhere, probably in the database, if you want it to persist.
If you want it in RAM, you should look into using a caching mechanism like APC. This will of course be reset on system reboot or when the cache is purged.
thanks to all,I use CFileCache to resolve this problem,so I can’t setup MemCache.
ReadTimes.php
class ReadTimes{
public static function incReadTimes(){
$rs=Yii::app()->cache->get("app_readTimes");
if($rs==null){
Yii::app()->cache->set("app_readTimes", "0");
}else{
Yii::app()->cache->set("app_readTimes",$rs+1);
}
}
public static function showReadTimes(){
return Yii::app()->cache->get("app_readTimes");
}
}