Changeset 30895


Ignore:
Timestamp:
01/19/15 17:43:43 (4 years ago)
Author:
plg
Message:

speed improvement with the new persistent_cache (new in Piwigo 2.7)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/AMenuManager/amm_pip.class.inc.php

    r28688 r30895  
    398398  private function getRandomPictures($num=25) 
    399399  { 
    400     global $user; 
     400    global $user, $persistent_cache, $conf; 
    401401 
    402402    $returned=array(); 
     
    407407    } 
    408408 
    409     $sql=array(); 
    410  
    411     // because ORDER BY RAND() can be very slow on a big database, let's 
    412     // make a first query with no join and by security take 5 times 
    413     // $num. We keep the result in session for 5 minutes. 
    414     if (!isset($_SESSION['amm_random_pics']) 
    415         or !isset($_SESSION['amm_random_pics_generated_on']) 
    416         or $_SESSION['amm_random_pics_generated_on'] < time() - 5*60) // 5 minutes ago 
    417     { 
    418       $query = ' 
     409    $cache_key = $persistent_cache->make_key( 
     410      array( 
     411        'amm_random_pics', 
     412        $conf['order_by'], 
     413        $user['id'], 
     414        $user['cache_update_time'], 
     415        $this->config['amm_randompicture_selectMode'] 
     416        ) 
     417      ); 
     418     
     419    if ($persistent_cache->get($cache_key, $returned)) 
     420    { 
     421      shuffle($returned); 
     422      return $returned; 
     423    } 
     424 
     425    $query = ' 
    419426SELECT id 
    420427  FROM '.IMAGES_TABLE.' 
     
    422429  ORDER BY RAND() LIMIT '.($num*5).' 
    423430;'; 
    424       $_SESSION['amm_random_pics'] = query2array($query, null, 'id'); 
    425       $_SESSION['amm_random_pics_generated_on'] = time(); 
    426     } 
    427      
     431    $image_ids = query2array($query, null, 'id'); 
     432 
     433    $sql = array(); 
     434 
    428435    $sql['select'] = ' 
    429436SELECT 
     
    445452     
    446453    $sql['where'] = ' 
    447   WHERE i.id IN ('.implode(',', $_SESSION['amm_random_pics']).') 
     454  WHERE i.id IN ('.implode(',', $image_ids).') 
    448455    AND i.level <= '.$user['level'].' 
    449456'; 
     
    493500    } 
    494501 
     502    if (count($returned) > 0) 
     503    {// cache the results only if not empty - otherwise it is useless 
     504      $persistent_cache->set($cache_key, $returned, 300); 
     505    } 
     506     
    495507    return($returned); 
    496508  } 
Note: See TracChangeset for help on using the changeset viewer.