When querying thinkphp paginated data, if the cache is set and the second page cannot be read, how can I break it?
怪我咯
怪我咯 2017-05-16 13:04:24
0
5
527

When thinkphp paging data query, if the cache is set, only the first page will be displayed. How to break it?
That is, 12 >> This effect comes out, but when you click 2, the content of page 1 is still displayed.

$count=M('visitdata')->where(array('works_code'=>$get_code))->count();
$Page=new \Think\Page($count,10);
$show=$Page->show();

S($get_code.'visitdata',null); //If the cache is not cleared first, only page 1 will be displayed...
// Perform paging data query. If cache is set here, only page 1 will be displayed.
M('visitdata')->cache($get_code.'visitdata',60)->where(array('works_code'=>$get_code))->order('visit_id')->limit ($Page->firstRow.','.$Page->listRows)->select();
$cache_visitdata=S($get_code.'visitdata');

$this->assign(array(
    'code'=>$get_code,
    'visitdata'=>$cache_visitdata
));
$this->assign('page',$show);
$this->display();

12 >> This effect comes out, but when you click 2, the content on page 1 is still displayed.
How to break it?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(5)
我想大声告诉你

This is TP3.2. I haven’t used it for a long time and I have forgotten how to use it. The questioner can do this:

  • Get page number

  • Check whether the cache corresponding to the page number exists (the cache key can be written like this: 'content_page_' . $page)

  • Return if cache exists

  • If the cache does not exist, execute the database query and cache a copy. The next time the request comes in, it will be cached directly

I think the main problem lies in the cached key value.

给我你的怀抱

s key plus paging mark

phpcn_u1582

Output sql statement for debugging, first see what sql is executing and then find the problem

phpcn_u1582

What you have here: ->cache($get_code.'visitdata',60)
$get_code.'visitdata' is the same in the case of paging, so it will not be updated.
You should be able to include the page number in this key.

刘奇
$p = intval($_GET['p']);
M('visitdata')->cache($get_code.'visitdata'.$p,60)->where(array('works_code'=>$get_code))->order('visit_id')->limit($Page->firstRow.','.$Page->listRows)->select();
$cache_visitdata=S($get_code.'visitdata');

//把页码加上。
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template