search

Home  >  Q&A  >  body text

When querying thinkphp paginated data, if the cache is set and the second page cannot be read, how can I break it?

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?

怪我咯怪我咯2756 days ago543

reply all(5)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:06:24

    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.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:06:24

    s key plus paging mark

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:06:24

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

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:06:24

    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.

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:06:24

    $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');
    
    //把页码加上。

    reply
    0
  • Cancelreply