Commit 87ba1dcd authored by jlaval's avatar jlaval

On continue a ameliorer le backend redis

parent bae1cd4d
......@@ -638,6 +638,11 @@ elseif ($_REQUEST["action"] == "newstudent")
/* on va lui creer un compte utilisateur */
$user = new utilisateur($site->db, $site->dbrw);
/* D'abord on desactive le cache de fastsearch histoire que
l'etudiant soit immediatement accessible par fsearch */
$cache = new fsearchcache ();
$cache->disable_cache_temporarily (5);
/* Si on a le nom d'une ecole parce que c'est un etudiant */
$email_utbm_needed = false;
if ($_REQUEST['ecoleform'] == "ecole")
......
......@@ -31,35 +31,25 @@ if ( isset($_REQUEST['topdir']) && ($_REQUEST['topdir']=="./" || $_REQUEST['topd
if ( $_REQUEST['module']=="fsearch" )
{
// Une semaine
define ('DEFAULT_CACHE_TIMEOUT', 604800);
header("Content-Type: text/html; charset=UTF-8");
if ($_REQUEST["pattern"] == "")
exit();
$redis = new Redis ();
$redis->pconnect ('/var/run/redis/redis.sock');
require_once($topdir. "include/cts/fsearchcache.inc.php");
$cache = new fsearchcache ();
$content = null;
if ($site->user->is_valid() && $site->user->cotisant) {
$content = $redis->get (strtolower ($_REQUEST["pattern"]));
}
if ($cache->can_get_cached_contents ())
$content = $cache->get_cached_contents ($site->user, $_REQUEST["pattern"]);
if ($content == null) {
require_once($topdir. "include/cts/fsearch.inc.php");
$fsearch = new fsearch ( $site, false );
$content = $fsearch->buffer;
if (!empty ($content) && strlen ($_REQUEST["pattern"]) > 4) {
$key = strtolower($_REQUEST["pattern"]);
$redis->set ($key, $content);
$redis->expire($key, DEFAULT_CACHE_TIMEOUT);
}
if (!empty ($content) && strlen ($_REQUEST["pattern"]) > 4)
$cache->set_temporarily_cached_contents($_REQUEST["pattern"], $content);
}
$redis->close ();
echo $content;
exit ();
}
......
......@@ -8,6 +8,9 @@
* Cette classe utilise l'instance Redis pour stocker ses valeurs, l'uid devenant la clé
* Dans notre cas, toutes les données cachées sont stockés dans la base 1
*/
require_once ('../redis.inc.php');
class cachedcontents extends stdcontents
{
protected $uid;
......@@ -74,8 +77,7 @@ class cachedcontents extends stdcontents
private function get_redis_instance ()
{
$redis = new Redis ();
$redis->popen ('/var/run/redis/redis.sock');
$redis = redis_open_connection ();
$redis->select (1);
return $redis;
}
......
<?php
/* Copyright 2011
* - Jeremie Laval <jeremie dot laval at gmail dot com>
*
* Ce fichier fait partie du site de l'Association des Étudiants de
* l'UTBM, http://ae.utbm.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
require_once('../redis.inc.php');
class fsearchcache
{
var $redis;
public function fsearchcache ()
{
$this->redis = redis_open_connection ();
}
function __destruct ()
{
$this->redis->close ();
}
public function can_get_cached_contents ()
{
return !$this->redis->exists('_disable_cache');
}
public function disable_cache_temporarily ($minutes = 5)
{
$this->redis->setex('_disable_cache', $minutes * 60, true);
}
public function enable_cache ()
{
$this->redis->del('_disable_cache');
}
public function get_cached_contents ($user, $request)
{
$content = null;
if ($user->is_valid() && $user->cotisant)
$content = $this->redis->get ($this->format_request ($request));
return $content;
}
public function set_cached_contents ($request, $result)
{
$this->redis->set ($this->format_request ($request), $result);
}
public function set_temporarily_cached_contents ($request, $result, $seconds = 604800) // default is one week
{
$this->redis->setex ($this->format_request ($request), $seconds, $result);
}
public function must_revalidate_for ($input)
{
$input = $this->format_request ($input);
// add each leading substring of input to be reprocessed
for ($i = 1; $i < strlen($input); $i++)
$this->redis->sAdd (substr($input, 0, $i));
}
private function format_request ($request)
{
if ($request[0] == '_')
$request = substr($request, 1);
return strtolower ($request);
}
}
function fsearch_revalidate_cache_for ($input)
{
$cache = new fsearchcache ();
$cache->must_revalidate_for ($input);
}
?>
\ No newline at end of file
......@@ -1238,6 +1238,9 @@ class utilisateur extends stdentity
$this->invalidate ("email");
$this->send_first_email($this->email,$password);
}
/* on ajoute le nouvel utilisateur au traitement du cache fsearch */
fsearch_revalidate_cache_for ($this->prenom);
fsearch_revalidate_cache_for ($this->nom);
return true;
}
......@@ -1329,7 +1332,7 @@ class utilisateur extends stdentity
* @todo Fonction à revoir complètement (aussi bien usage dans le site que implémentation)
* @deprecated
*/
function new_utbm_user ( $nom, $prenom, $email, $emailutbm, $password, $semestre, $branche, $promo, $etudiant, $droit_image, $nom_ecole, $date_naissance = null , $sexe = 1)
function new_utbm_user ( $nom, $prenom, $email, $emailutbm, &$password, $semestre, $branche, $promo, $etudiant, $droit_image, $nom_ecole, $date_naissance = null , $sexe = 1)
{
$this->type="std";
......
<?php
/* Copyright 2011
* - Jeremie Laval <jeremie dot laval at gmail dot com>
*
* Ce fichier fait partie du site de l'Association des Étudiants de
* l'UTBM, http://ae.utbm.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
function redis_open_connection ()
{
$redis = new Redis ();
$redis->pconnect ('/var/run/redis/redis.sock');
return $redis;
}
?>
\ No newline at end of file
......@@ -11,6 +11,7 @@ $_SERVER['SCRIPT_FILENAME']="/var/www/ae/www/ae2/phpcron";
$topdir=$_SERVER['SCRIPT_FILENAME']."/../";
require_once($topdir. "include/site.inc.php");
require_once($topdir. 'comptoir/include/produitrecurrent.inc.php');
require_once($topdir. 'include/redis.inc.php');
$site = new site ();
......@@ -24,7 +25,7 @@ new requete($site->dbrw,"DELETE FROM `cpt_verrou` WHERE NOT EXISTS(SELECT * FROM
new requete($site->dbrw,"DELETE FROM `cpt_type_produit` WHERE NOT EXISTS ( SELECT * FROM cpt_produits WHERE cpt_produits.id_typeprod=cpt_type_produit.id_typeprod)");
new requete($site->dbrw,'UPDATE cpt_produits AS p JOIN cpt_produit_recurrent AS r ON p.id_produit = r.id_produit SET p.date_fin_produit=TIMESTAMPADD(DAY, r.ttl, NOW()) WHERE NOW()>p.date_fin_produit AND DAYOFWEEK(CURDATE())-1 = r.jour_remise_en_vente AND p.prod_archive = 0');
new requete($site->dbrw,'UPDATE cpt_produits AS p JOIN cpt_produit_recurrent AS r ON p.id_produit = r.id_produit SET p.date_fin_produit=TIMESTAMPADD(DAY, r.ttl, p.date_fin_produit) WHERE NOW()>p.date_fin_produit AND DAYOFWEEK(CURDATE())-1 = r.jour_remise_en_vente AND p.prod_archive = 0');
// Tâche 2 : Nettoyage des créneaux "vides" expriés
......@@ -44,4 +45,24 @@ $req = new requete($site->db, "SHOW TABLES");
while(list($table)=$req->get_row())
new requete($site->dbrw, "OPTIMIZE TABLE `".$table."`");
// Tâche 6 : on regenere le cache de fsearch pour les choses modifies
$input = null;
$redis = redis_open_connection ();
while (($input = $redis->sPop('_cache_to_process')) != null) {
// Si l'input est plus grand que 4 caractere, on efface juste les entree eventuellement presentes
if (strlen ($input) > 4 && $redis->exists ($input)) {
$redis->del ($input);
continue;
}
// Sinon on recalcule le cache
require_once ($topdir. 'include/cts/fsearch.inc.php');
$_REQUEST['pattern'] = $input;
$fsearch = new fsearch ($site, false, true);
if (!empty ($fsearch->buffer))
$redis->set ($input, $fsearch->buffer);
else
$redis->del ($input);
}
$redis->close ();
?>
......@@ -26,10 +26,10 @@ $topdir="../";
require_once ($topdir. "include/site.inc.php");
require_once ($topdir. "include/cts/fsearch.inc.php");
require_once ($topdir. "include/redis.inc.php");
$site = new site ();
$redis = new Redis ();
$redis->pconnect ('127.0.0.1');
$redis = redis_open_connection ();
function get_char_off ($offset)
{
......
......@@ -15,6 +15,45 @@ $req = new requete($site->db, 'SHOW TABLES');
while(list($table)=$req->get_row())
new requete($site->dbrw, 'OPTIMIZE TABLE \''.$table.'\'');
// On regenere entierement le cache fsearch pour prendre en compte les nouvelles popularites
echo ">> BEGIN FSEARCH CACHE REGEN: ".date('r')."\n";
require_once ($topdir. "include/cts/fsearch.inc.php");
require_once ($topdir. "include/redis.inc.php");
$redis = redis_open_connection ();
// We remove all entries from this database
$redis->flushDB();
$redis->set ('_disable_cache', true);
$letters = array ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
function compute_pattern_with_size ($size)
{
global $site, $redis;
// we do all combination of 4 character
$upper = pow (26, $size);
for ($i = 0; $i < $upper; ++$i) {
$str = '';
for ($j = $size - 1; $j >= 0; $j--)
$str .= $letters[($i / pow (26, $j)) % 26];
$_REQUEST['pattern'] = $str;
$fsearch = new fsearch ($site, false, true);
if (!empty ($fsearch->buffer))
$redis->set($str, $fsearch->buffer);
}
}
compute_pattern_with_size (1);
compute_pattern_with_size (2);
compute_pattern_with_size (3);
compute_pattern_with_size (4);
$redis->del ('_disable_cache');
$redis->close ();
echo ">> END FSEARCH CACHE REGEN: ".date('r')."\n";
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment