您的位置:首页 > 数据库 > Memcache

ci框架搜索memcache缓存

2016-10-26 11:29 92 查看
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class NewsCollect extends CI_Controller
{

public function __construct()
{
parent::__construct();
set_time_limit(0);
}
/**
* 展示列表
*/
public function nationList($page=1){
//查询天气

$count=$this->db->count_all('nation');
$page_num=5;
$page_limit=($page - 1)*$page_num;
$this->load->library('Page');
$lists = $this->page->getPageStr("http://localhost/Project/test/CURL/NewsCollect/nationList",$count,$page_num,$page);
$shop = $this->db->conn_id->query("select * from nation limit $page_limit,$page_num")->fetchALL(PDO::FETCH_ASSOC);
$this->load->vars('shop', $shop);
$this->load->vars('lists', $lists);
$this->load->vars('page',$page);
$this->load->view('Index/nationList.html');
}
/**
* 查询天气
*/
//   public function weather()
//  {
//     $city=$this->input->post('city');
//     $city="北京";
//     $apiurl="http://api.k780.com:88/?app=weather.future&weaid=".$city."&&appkey=21376&sign=241bf4e35fe1b54f13fee939124dec41&format=json";
//     $content=@file_get_contents($apiurl);
//     echo $content;
//  }
/** * 搜索 */ public function nationListTwo($page=1){ $title=$this->input->post('title'); $editor=$this->input->post('editor'); //$page=$this->input->post('page'); if(empty($title)&&empty($editor)){ redirect("NewsCollect/nationList"); } $this->page($title,$editor,$page); $this->load->view('Index/nationListTwo.html'); } public function page($title,$editor=null,$page) { $this->load->library('Page'); //多条件判断非空 if($title!=null){ $search=$title; $sum = $this->db->conn_id->query("select count(*) as num from nation where title like '%$search%'")->fetchAll(PDO::FETCH_ASSOC); $count = $sum[0]['num']; $page_num = 5; $page_limit = ($page - 1) * $page_num; $sql="select * from nation where title like '%$title%' limit $page_limit,$page_num"; }elseif($editor!=null){ $search=$editor; $sum = $this->db->conn_id->query("select count(*) as num from nation where title like '%$search%'")->fetchAll(PDO::FETCH_ASSOC); $count = $sum[0]['num']; $page_num = 5; $page_limit = ($page - 1) * $page_num; $sql="select * from nation where editor like '%$editor%' limit $page_limit,$page_num"; } //实例化memcache $memcache = new Memcache(); $memcache->connect('127.0.0.1', 11211) or die("连接失败"); //进入memcache缓存 if($val=$memcache->get("$search")) { echo "我是memcache"; $this->load->vars('shop',$val); //分页 $sum = $this->db->conn_id->query("select count(*) as num from nation where title like '%$search%'")->fetchAll(PDO::FETCH_ASSOC); $count = $sum[0]['num']; $page_num = 5; $lists = $this->page->getPageStr("http://localhost/Project/test/CURL/NewsCollect/nationListTwo",$count,$page_num,$page); $this->load->vars('lists', $lists); } else { $lists = $this->page->getPageStr("http://localhost/Project/test/CURL/NewsCollect/nationListTwo",$count,$page_num,$page); $result = $this->db->conn_id->query("$sql")->fetchAll(PDO::FETCH_ASSOC); $memcache->add($search,$result,false,0); echo '我是数据库'; $this->load->vars('shop',$result); $this->load->vars('lists', $lists); } }

public function add()
//      {
//        //采取的内容pdo入库
//        $this->load->library('Curl');
//        $url="http://wenxue.yjbys.com/xiaoshuo/";
//        $url="http://wenxue.yjbys.com/xiaoshuo/list_2.html";
//        $url="http://wenxue.yjbys.com/xiaoshuo/list_3.html";
//        $content=file_get_contents($url);
//        $con = iconv("gbk","utf-8",$content);
//        $preg1='#<dl class="newlist">(.*)</dl>#isU';
//        preg_match($preg1,$con,$arr1);
//        $preg='#<dd><p><a href="(.*)" target="_blank">(.*)</a> </p>#isU';
//        preg_match_all($preg,$arr1[0],$arr3);
//        $preg2='#<dt><span class="time">(.*)</span><a href="(.*)" target="_blank">(.*)</a></dt>#isU';
//        preg_match_all($preg2,$arr1[0],$arr2);
//        $arr['title']=$arr2[3];
//        $arr['time']=$arr2[1];
//        $arr['html']=$arr2[2];
//        $arr['content']=$arr3[2];
//        try {
//            $this->db->conn_id->beginTransaction(); // 开启一个事务
//            $sql="insert into nation(title,add_time,html,content) values ";
//            for($i=0;$i<count($arr['title']);$i++)
//            {
//                $sql.="('".$arr['title'][$i]."','".$arr['time'][$i]."','".$arr['html'][$i]."','".$arr['content'][$i]."'),";
//            }
//            $sql=substr($sql,0,-1);
//            $count=$this->db->conn_id->exec("$sql");
//            if(!$count)
//                throw new PDOException("采集失败");
//            $this->db->conn_id->commit();//如果执行到此处前面两个更新sql语句执行成功,整个事务执行成功
//            header("refresh:3;url=http://localhost/Project/Curl/CURL/index.php/NewsCollect/nationList");
//        } catch (PDOException $e) {
//            $this->db->conn_id->rollback();//执行事务中的语句出了问题,整个事务全部撤销
//            echo $e->getMessage();
//        }
//    }
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: