Subversion Repositories Applications.framework

Compare Revisions

Ignore whitespace Rev 247 → Rev 248

/trunk/framework/Cache.php
82,24 → 82,28
'duree_de_vie' => 3600,
);
public function __construct($options) {
protected $stockage = null;
public function __construct($options, $options_stockage) {
$this->setOptions($options);
if ($this->options['stockage_mode'] == self::STOCKAGE_MODE_FICHIER) {
$this->stockage = new CacheFichier($options_stockage);
} else if ($this->options['stockage_mode'] == self::STOCKAGE_MODE_SQLITE) {
$this->stockage = new CacheSqlite($options_stockage);
}
$this->stockage->setEmplacement($this->options['stockage_chemin']);
}
/**
* Fabrique et retourne l'identifiant du cache.
*
* Vérifie l'option 'cache_id_prefixe' et retourne le nouvel id avec préfixe ou simplement l'id lui même si elle vaut null.
*
* @param string $id Identifiant du cache.
* @return string Identifiant du cache avec ou sans préfixe.
*/
protected function getId($id) {
$nouvel_id = $id;
if (($id !== null) && isset($this->options['cache_id_prefixe'])) {
$nouvel_id = $this->options['cache_id_prefixe'] . $id;
private function setOptions($options) {
while (list($nom, $valeur) = each($options)) {
if (!is_string($nom)) {
trigger_error("Nom d'option incorecte : $nom", E_USER_WARNING);
}
$nom = strtolower($nom);
if (array_key_exists($nom, $this->options)) {
$this->options[$nom] = $valeur;
}
}
return $nouvel_id;
}
/**
114,42 → 118,10
if ($emplacement != null) {
$this->executerMethodeStockage('setEmplacement', array($emplacement));
} else {
trigger_error("L'emplacement ne peut pas être null.", E_USER_WARNING);
trigger_error("L'emplacement ne peut pas être null.", E_USER_WARNING);
}
}
private function setEmplacementFichier($emplacement) {
if (!is_dir($emplacement)) {
trigger_error("L'emplacement doit être un dossier.", E_USER_WARNING);
}
if (!is_writable($emplacement)) {
trigger_error("Le dossier de stockage du cache n'est pas accessible en écriture", E_USER_WARNING);
}
$emplacement = rtrim(realpath($emplacement), '\\/').DS;
$this->options['stockage_chemin'] = $emplacement;
}
private function setEmplacementSqlite($emplacement) {
if (!extension_loaded('sqlite')) {
trigger_error("Impossible d'utiliser le mode de sotckage SQLite car l'extenssion 'sqlite' n'est pas chargé dans ".
"l'environnement PHP courrant.\n Le mode de stockage par fichier sera utilisé à la place.");
$emplacement = rtrim(realpath($emplacement), '\\/').DS;
$this->options['stockage_mode'] = self::STOCKAGE_MODE_FICHIER;
}
$this->options['stockage_chemin'] = $emplacement;
}
private function executerMethodeStockage($prefixe, $params) {
$methode = 'sauver'.$this->options['mode_stockage'];
if (method_exists($this, $methode)) {
$resultat = call_user_func_array(array($this, $methode), $params);
} else {
$resultat = false;
trigger_error("La méthode '$methode' n'existe pas dans la classe '".get_class($this)."'.", E_USER_WARNING);
}
return $resultat;
}
/**
* Teste si un cache est disponible pour l'identifiant donné et (si oui) le retourne (false dans le cas contraire)
*
160,7 → 132,7
public function charger($id, $ne_pas_tester_validiter_du_cache = false) {
$donnees = false;
if ($this->options['mise_en_cache'] === true) {
$id = $this->getId($id);
$id = $this->prefixerId($id);
$this->dernier_id = $id;
self::validerIdOuTag($id);
$donnees = $this->executerMethodeStockage('charger', array($id, $ne_pas_tester_validiter_du_cache));
170,6 → 142,23
}
/**
* Test if a cache is available for the given id
*
* @param string $id Cache id
* @return int|false Last modified time of cache entry if it is available, false otherwise
*/
public function tester($id) {
$resultat = false;
if ($this->options['mise_en_cache'] === true) {
$id = $this->prefixerId($id);
self::validerIdOuTag($id);
$this->dernier_id = $id;
$resultat = $this->executerMethodeStockage('tester', array($id));
}
return $resultat;
}
/**
* Sauvegarde en cache les données passées en paramètre.
*
* @param mixed $donnees Données à mettre en cache (peut être différent d'une chaine si serialisation_auto vaut true).
181,7 → 170,7
public function sauver($donnees, $id = null, $tags = array(), $duree_de_vie_specifique = false) {
$resultat = true;
if ($this->options['mise_en_cache'] === true) {
$id = ($id === null) ? $this->dernier_id : $this->getId($id);
$id = ($id === null) ? $this->dernier_id : $this->prefixerId($id);
self::validerIdOuTag($id);
self::validerTableauDeTags($tags);
201,6 → 190,22
}
/**
* Supprime un enregistrement en cache.
*
* @param string $id Identificant du cache à supprimer.
* @return boolean True si ok
*/
public function supprimer($id) {
$resultat = true;
if ($this->options['mise_en_cache'] === true) {
$id = $this->prefixerId($id);
self::validerIdOuTag($id);
$resultat = $this->executerMethodeStockage('supprimer', array($id));
}
return $resultat;
}
/**
* Nettoyage des enregistrements en cache
*
* Mode de nettoyage disponibles :
230,23 → 235,151
}
return $resultat;
}
 
/**
* Return an array of stored cache ids
*
* @return array array of stored cache ids (string)
*/
public function getIds() {
$ids = $this->executerMethodeStockage('getIds');
$ids = $this->supprimerPrefixe($ids);
return $ids;
}
 
/**
* Return an array of stored tags
*
* @return array array of stored tags (string)
*/
public function getTags() {
return $this->executerMethodeStockage('getTags');
}
/**
* Supprime un enregistrement en cache.
*
* @param string $id Identificant du cache à supprimer.
* @return boolean True si ok
*/
public function supprimer($id) {
$resultat = true;
if ($this->options['mise_en_cache'] === true) {
$id = $this->getId($id);
self::validerIdOuTag($id);
$resultat = $this->executerMethodeStockage('supprimer', array($id));
/**
* Return an array of stored cache ids which match given tags
*
* In case of multiple tags, a logical AND is made between tags
*
* @param array $tags array of tags
* @return array array of matching cache ids (string)
*/
public function getIdsAvecLesTags($tags = array()) {
$ids = $this->executerMethodeStockage('getIdsAvecLesTags', array($tags));
$ids = $this->supprimerPrefixe($ids);
return $ids;
}
 
/**
* Return an array of stored cache ids which don't match given tags
*
* In case of multiple tags, a logical OR is made between tags
*
* @param array $tags array of tags
* @return array array of not matching cache ids (string)
*/
public function getIdsSansLesTags($tags = array()) {
$ids = $this->executerMethodeStockage('getIdsSansLesTags', array($tags));
$ids = $this->supprimerPrefixe($ids);
return $ids;
}
 
/**
* Return an array of stored cache ids which match any given tags
*
* In case of multiple tags, a logical OR is made between tags
*
* @param array $tags array of tags
* @return array array of matching any cache ids (string)
*/
public function getIdsAvecUnTag($tags = array()) {
$ids = $this->executerMethodeStockage('getIdsAvecUnTag', array($tags));
$ids = $this->supprimerPrefixe($ids);
return $ids;
}
 
/**
* Return the filling percentage of the backend storage
*
* @return int integer between 0 and 100
*/
public function getPourcentageRemplissage() {
return $this->executerMethodeStockage('getPourcentageRemplissage');
}
 
/**
* Return an array of metadatas for the given cache id
*
* The array will include these keys :
* - expire : the expire timestamp
* - tags : a string array of tags
* - mtime : timestamp of last modification time
*
* @param string $id cache id
* @return array array of metadatas (false if the cache id is not found)
*/
public function getMetadonnees($id) {
$id = $this->prefixerId($id);
return $this->executerMethodeStockage('getMetadonnees', array($id));
}
 
/**
* Give (if possible) an extra lifetime to the given cache id
*
* @param string $id cache id
* @param int $extraLifetime
* @return boolean true if ok
*/
public function ajouterSupplementDureeDeVie($id, $supplement_duree_de_vie) {
$id = $this->prefixerId($id);
return $this->executerMethodeStockage('ajouterSupplementDureeDeVie', array($id, $supplement_duree_de_vie));
}
 
/**
* Fabrique et retourne l'identifiant du cache avec son préfixe.
*
* Vérifie l'option 'cache_id_prefixe' et retourne le nouvel id avec préfixe ou simplement l'id lui même si elle vaut null.
*
* @param string $id Identifiant du cache.
* @return string Identifiant du cache avec ou sans préfixe.
*/
private function prefixerId($id) {
$nouvel_id = $id;
if (($id !== null) && isset($this->options['cache_id_prefixe'])) {
$nouvel_id = $this->options['cache_id_prefixe'].$id;
}
return $nouvel_id;
}
private function executerMethodeStockage($methode, $params = null) {
if (method_exists($this->stockage, $methode)) {
if ($params == null) {
$resultat = call_user_func(array($this->stockage, $methode));
} else {
$resultat = call_user_func_array(array($this->stockage, $methode), $params);
}
} else {
$resultat = false;
trigger_error("La méthode '$methode' n'existe pas dans la classe '".get_class($this)."'.", E_USER_WARNING);
}
return $resultat;
}
}
private function supprimerPrefixe($ids) {
// Il est nécessaire de retirer les cache_id_prefixe des ids (voir #ZF-6178, #ZF-7600)
if (isset($this->options['cache_id_prefixe']) && $this->options['cache_id_prefixe'] !== '') {
$prefixe =& $this->options['cache_id_prefixe'];
$prefixe_longueur = strlen($prefixe);
foreach ($ids as &$id) {
if (strpos($id, $prefixe) === 0) {
$id = substr($id, $prefixe_longueur);
}
}
}
return $ids;
}
private function controlerEcriture($id, $donnees_avant_ecriture) {
$resultat = true;
if ($this->options['controle_ecriture']) {
283,7 → 416,7
if ($this->options['nettoyage_auto'] > 0) {
$rand = rand(1, $this->options['nettoyage_auto']);
if ($rand == 1) {
$this->clean(self::NETTOYAGE_MODE_EXPIRER);
$this->nettoyer(self::NETTOYAGE_MODE_EXPIRATION);
}
}
}
296,7 → 429,7
*/
protected static function validerIdOuTag($chaine) {
if (!is_string($chaine)) {
trigger_error('Id ou tag invalide : doit être une chaîne de caractères', E_USER_WARNING);
trigger_error('Id ou tag invalide : doit être une chaîne de caractères', E_USER_ERROR);
}
if (substr($chaine, 0, 9) == 'internal-') {
trigger_error('"internal-*" identifiants ou tags sont réservés', E_USER_WARNING);