怎么样刷新缓存文件

xiachao2008 2012-06-06 11:25:26
怎么样刷新缓存文件

我在一个循环里写了 判断一下(当数组a的长度小于1时就执行一次) 写进缓存的操作,反之,不执行写进缓存的操作。

问题就出来这儿

当执行到 数组a 小于1时,就写一次缓存b.txt 然后我立既读这个缓存b.txt文件(我用记事本打开b.txt,可以得到正确的内容),而得到的是一个错误的结果 还是数组a的值,并没有变化,只有当我在执行一下这个方法时,才能读到这个b.txt 文件。

我在该怎么做?才能解决这个问题。
...全文
167 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
amani11 2012-06-06
  • 打赏
  • 举报
回复
静态方法,$this会有问题吧
xiachao2008 2012-06-06
  • 打赏
  • 举报
回复
那会错在那里啊,我怎么改呢?
xiachao2008 2012-06-06
  • 打赏
  • 举报
回复
通过了
[Quote=引用 8 楼 的回复:]

问题不出在文件读写,应该检查整个逻辑
当然也不排除你不能通过下面的测试例PHP code
file_put_contents('zzz', 'a');
$s = file_get_contents('zzz');
echo $s;

$fp = fopen('zzz', 'w');
fwrite($fp, 'b');
$fp = fopen('zzz', 'r');
echo fgets($……
[/Quote]
xiachao2008 2012-06-06
  • 打赏
  • 举报
回复

<?php

file_put_contents("1.txt","aabbcc",LOCK_EX);
$a = file_get_contents('1.txt');
echo $a;
?>


这这样写了一个都得行
xuzuning 2012-06-06
  • 打赏
  • 举报
回复
问题不出在文件读写,应该检查整个逻辑
当然也不排除你不能通过下面的测试例
file_put_contents('zzz', 'a');
$s = file_get_contents('zzz');
echo $s;

$fp = fopen('zzz', 'w');
fwrite($fp, 'b');
$fp = fopen('zzz', 'r');
echo fgets($fp);
ab
xiachao2008 2012-06-06
  • 打赏
  • 举报
回复

public function set($id,$value,$expire=0)
{
//echo "<pre>";echo "a";print_r($value);exit();
file_put_contents($this->getFile($id),'<?php return array('.$expire.','.var_export($value, true).');',LOCK_EX);
}
public function get($id)
{
$cFile = $this->getFile($id);
if (file_exists($cFile))
{
$data = include $cFile;
if(SYSTIME < $data[0] || $data[0] == 0)
{
return $data[1];
}else
{
@unlink($cFile);
}
}
return false;
}
xiachao2008 2012-06-06
  • 打赏
  • 举报
回复
读他是用的


file_put_contents($this->getFile($id),'<?php return array('.$expire.','.var_export($value, true).');',LOCK_EX);
craboy1 2012-06-06
  • 打赏
  • 举报
回复
算我没说,你先看下cache类吧。没人知道它怎么实现缓存了
craboy1 2012-06-06
  • 打赏
  • 举报
回复
读之前,使用fclose了吗,另外把问题简单化,把代码从项目中提练出来,(提练本身也是分析问题),然后再测试.
xiachao2008 2012-06-06
  • 打赏
  • 举报
回复
$urlArray = cache::get('tuangouBaiduType');
echo "<pre>a";print_r($urlArray); //$urlArray 为 空
if(count($urlArray)<=1){
cache::init("php");
$type = array('a','b','c','d');
cache::set('tuangouBaiduType',$type);
}
cache::init("php");
$urlArray = cache::get('tuangouBaiduType');

echo "<pre>d";print_r($urlArray); // $urlArray的内容为空 并不是 'a','b','c','d'

大约就是这样的
xiachao2008 2012-06-06
  • 打赏
  • 举报
回复
$urlArray = cache::get('tuangouBaiduType');
echo "<pre>a";print_r($urlArray); //$urlArray 为 空
if(count($urlArray)<=1){
cache::init("php");
$type = array('a','b','c','d');
cache::set('tuangouBaiduType',$type);
}
cache::init("php");
$urlArray = cache::get('tuangouBaiduType');

echo "<pre>d";print_r($urlArray); // $urlArray的内容为空 并不是 'a','b','c','d'

大约就是这样的
amani11 2012-06-06
  • 打赏
  • 举报
回复
无码无真相

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧