发表时间:2014-09-05来源:网络
先看一段代码:
[php]
/**
* 获取设置信息
*/
public function getCoinSetting() {
$cache = Common::getTair();
$ckey = Common::hashKey("Hello");
$ret = $cache->get($ckey);
if ($ret) return json_decode($ret, true);
$taomanyiApiService = $this->_getTmiApiService();
$result = $taomanyiApiService->getCoinSetting();
$cache->set($ckey, json_encode($result), 3600);
return $result;
}
这是一个使用Tair内存缓存的实例,这段代码中,设置了缓存,缓存时间为3600秒。数据是从Api中获取的,如果这么写会出现什么问题呢?假如:
[php]
$result = $taomanyiApiService->getCoinSetting();
$result获取的数据为空,因为$result数据是从HTTP请求过来的,数据不正常也是比较常见的事情。在这种状况下,HTTP请求失败,那么接口数据就请求不到,接下来的流程是设置缓存
[php ]
$cache->set($ckey, json_encode($result), 3600);
我们会发现,因为一次接口HTTP请求的失败,我们不小心将空数据缓存了起来,缓存时间为3600秒。这样就会出现页面上,例如分类出现了数据的空白,影响了整个业务流程
我们做以下的优化:
[php]
if ($result) $cache->set($ckey, json_encode($result), 3600);
CI框架连接数据库配置操作以及多数据库操作
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
C语言关键字及其解释介绍 C语言32个关键字详解
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
PHP中的(++i)前缀自增 和 (i++)后缀自增
将视频设置为Android手机开机动画的教程
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
常用dos命令及语法
PHP中include和require区别之我见
2014-09-05
2022-03-20
2022-03-21
2022-03-24
2014-09-05
2014-09-05
2015-07-05
2014-09-05
2022-03-21
2014-09-05