PHP使用redis可以减少服务器以及数据的压力,以下是一种使用方法,redis安装可以参考这篇文章:Window 及 linux 下安装Redis
<?php $vod_id=$_GET['id']; //启动缓存 $cache_obj = new Redis(); $cache_re=$cache_obj->connect('127.0.0.1',6379); if(empty($cache_re)){ exit('1'); } //$cache_obj->flushall(); //$cache_obj->flushDB(); $redis_key='vod_'.$vod_id.'_info'; $vod_info=$cache_obj->get($redis_key); if(empty($vod_info)){ $vod_field='d_id,d_name,d_pic,d_starring,d_directed,d_area,d_content,d_playurl,d_year,d_type,b.t_name,d_remarks,d_score'; $sql='SELECT '.$vod_field.' FROM mac_vod a left join mac_vod_type b on a.d_type = b.t_id WHERE d_id='.$vod_id.' '; $db=DBconnect(); $res = mysqli_query($db,$sql); $vod_info = mysqli_fetch_assoc($res); mysqli_free_result($res); mysqli_close($db); //print_r($vod_info); if(!empty($vod_info)){ $vod_info_str=json_encode($vod_info); $cache_obj->setex($redis_key,86400*3,$vod_info_str); } }else{ $vod_info=json_decode($vod_info,true); //print_r($vod_info); } $cache_obj->close();
文章评论(0)