Home  >  Article  >  php教程  >  Yii框架中memcache用法实例

Yii框架中memcache用法实例

WBOY
WBOYOriginal
2016-06-06 20:16:141286browse

这篇文章主要介绍了Yii框架中memcache用法,以实例形式分析了在Yii框架中加入memcache的具体操作方法,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了Yii框架中memcache用法。分享给大家供大家参考。具体分析如下:

在现在的公司用的是YII的框架,接触到的东西也比较多,可以学到的东西也比较多,在以前的公司没有接触过memcache,只是听过,但是从来没有真正用过。现在终于有机会使用一下了,就以我做的项目为例吧!

我做的项目是一个手机排行榜,但是排行榜每隔15分钟刷新一次啊,排行榜有一个前三名,可能前15分钟这三个人是前三名,也许下一个15分钟又是别人前三名了,产品要求,这些人中只要是前三名的都要发奖品,思考了好久,最终决定用memcache来保存每15分钟的榜单前三名的信息。代码如下:

复制代码 代码如下:

public function getChargeRank3()
{
        $startime='2014-04-10 00:00:00';
        $endtime='2014-04-30 23:59:59';
        $sql="select top 3 user_id, sum (point) as total from  table where state=5 and cp_id>=11 and cp_id='$startime' and pay_time group by user_id order by total desc";
        $result=  Yii::app()->controller>createCommand($sql)->queryAll();
        $cache = yii::app()->cache->get($key);
        if($cache!=false)
        {
            foreach ($result as $key=>$val)
            {
  //这里为内部赠送礼物代码,,不便透漏
            }
        }
        YII::app()->cache->set($key, $result, strtotime($endtime));//把前三名写入缓存
}

希望本文所述对大家基于Yii框架的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