Subversion Repositories Applications.framework

Compare Revisions

Ignore whitespace Rev 248 → Rev 249

/trunk/exemple/services/modules/0.1/ServiceCache.php
New file
0,0 → 1,165
<?php
class ServiceCache extends Service {
public function consulter($ressources, $parametres) {
$texte = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
$options = array(
'stockage_mode' => Cache::STOCKAGE_MODE_FICHIER,
'stockage_chemin' => $this->getTmpDir(),
'controle_ecriture' => false,
'mise_en_cache' => true,
'cache_id_prefixe' => null,
'serialisation_auto' => false,
'nettoyage_auto' => 10,
'duree_de_vie' => 3600,
);
$options_stockage = array(
'fichier_verrou' => false,
'controle_lecture' => false,
'controle_lecture_type' => 'crc32',
'dossier_niveau' => 2,
'dossier_umask' => 0707,
'fichier_prefixe' => 'tbf',
'fichier_umask' => 0606,
'metadonnees_max_taille' => 100
);
$cache = new Cache($options, $options_stockage);
$id = 'monCache'; // id de cache de "ce que l'on veut cacher"
$id01 = $id.'01';
$id02 = $id.'02';
$id03 = $id.'03';
if (!($donnees = $cache->charger($id01))) {
$donnees = 'Mise en cache à : '.strftime('%A %d %B %Y à %H:%M:%S', time()).'<br/>';
for ($i = 0; $i < 100000; $i++) {
$donnees .= $i;
}
$cache->sauver($donnees, $id01);
$cache->sauver($donnees, $id02, array('tagTest01'));
$cache->sauver($donnees, $id03, array('tagTest01','tagTest02', 'tagTest03'));
}
// Affichage des données
echo $donnees.'<br/>';
// Affichage des ids du cache
echo 'Ids des enregistrements en cache : <pre>'.print_r($cache->getIds(), true).'</pre>';
// Affichage des ids du cache
echo 'Tags des enregistrements en cache : <pre>'.print_r($cache->getTags(), true).'</pre>';
// Affichage des ids du cache pour un tag donnée
$tag01 = 'tagTest01';
$tag02 = 'tagTest02';
$tag03 = 'tagTest03';
echo "Ids avec les tag '$tag02, $tag03' : ".'<pre>'.print_r($cache->getIdsAvecLesTags(array($tag02, $tag03)), true).'</pre>';
echo "Ids possédant le tag '$tag01' : ".'<pre>'.print_r($cache->getIdsAvecUnTag(array($tag01)), true).'</pre>';
echo "Ids sans les tag '$tag01, $tag02' : ".'<pre>'.print_r($cache->getIdsSansLesTags(array($tag01, $tag02)), true).'</pre>';
// Test du test de l'existence du cache
$id_test = $id01;
echo ($cache->tester($id_test) ? "OK : le cache '$id_test' existe" : "KO : le cache '$id_test' devrait exister").'<br/>';
// Affichage du pourcentage de remplissage
echo 'Pourcentage de remplissage : '.$cache->getPourcentageRemplissage().'%<br/>';
// Affichage des métadonnées
$meta01 = $cache->getMetadonnees($id_test);
echo 'Métadonnées du cache : <pre>'.print_r($meta01, true).'</pre>';
// Nettoyage & suppression
echo "Temps avant suppression du cache : ".(time() - $meta01['expiration']).'s<br/>';
if ((time() - $meta01['expiration']) == 0) {
// Suppressions
$cache->supprimer($id_test);
echo (!$cache->tester($id_test) ? "OK : le cache '$id_test' a bien été supprimé" : "KO : le cache '$id_test' n'a pas été supprimé").'<br/>';
// Nettoyage
$cache->nettoyer(Cache::NETTOYAGE_MODE_AVEC_LES_TAGS, array($tag02, $tag03));
echo (!$cache->tester($id03) ? "OK : le cache '$id03' a bien été supprimé" : "KO : le cache '$id03' n'a pas été supprimé").'<br/>';
$cache->nettoyer(Cache::NETTOYAGE_MODE_SANS_LES_TAGS, array($tag01));
echo ($cache->tester($id02) ? "OK : le cache '$id02' n'a pas été supprimé" : "KO : le cache '$id02' a été supprimé").'<br/>';
$cache->nettoyer(Cache::NETTOYAGE_MODE_EXPIRATION);
echo ($cache->tester($id02) ? "OK : le cache '$id02' n'a pas été supprimé" : "KO : le cache '$id02' a été supprimé").'<br/>';
$cache->nettoyer(Cache::NETTOYAGE_MODE_TOUS);
echo (!$cache->tester($id01) ? "OK : le cache '$id02' a été supprimé" : "KO : le cache '$id02' n'a pas été supprimé").'<br/>';
} else if ((time() - $meta01['expiration']) < -60) {
// Test ajout d'une durée de vie supplémentaire
$meta01 = $cache->getMetadonnees($id_test);
echo "Durée de vie du cache '$id_test' : ".$meta01['expiration'].'<br/>';
$cache->ajouterSupplementDureeDeVie($id_test, -3540);
$meta02 = $cache->getMetadonnees($id_test);
echo "Durée de vie du cache '$id_test' après ajout supplément de -3540s : ".$meta02['expiration'].
' - diff. : '.($meta02['expiration'] - $meta01['expiration']).'<br/>';
}
}
/**
* Determine system TMP directory and detect if we have read access
*
* inspired from Zend_File_Transfer_Adapter_Abstract & Zend_Cache
*
* @return string
*/
public function getTmpDir() {
$tmpdir = array();
foreach (array($_ENV, $_SERVER) as $tab) {
foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
if (isset($tab[$key])) {
if (($key == 'windir') or ($key == 'SystemRoot')) {
$dir = realpath($tab[$key] . '\\temp');
} else {
$dir = realpath($tab[$key]);
}
if ($this->isGoodTmpDir($dir)) {
return $dir;
}
}
}
}
$upload = ini_get('upload_tmp_dir');
if ($upload) {
$dir = realpath($upload);
if ($this->isGoodTmpDir($dir)) {
return $dir;
}
}
if (function_exists('sys_get_temp_dir')) {
$dir = sys_get_temp_dir();
if ($this->isGoodTmpDir($dir)) {
return $dir;
}
}
// Attemp to detect by creating a temporary file
$tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
if ($tempFile) {
$dir = realpath(dirname($tempFile));
unlink($tempFile);
if ($this->isGoodTmpDir($dir)) {
return $dir;
}
}
if ($this->isGoodTmpDir('/tmp')) {
return '/tmp';
}
if ($this->isGoodTmpDir('\\temp')) {
return '\\temp';
}
}
/**
* Verify if the given temporary directory is readable and writable
*
* @param $dir temporary directory
* @return boolean true if the directory is ok
*/
protected function isGoodTmpDir($dir){
if (is_readable($dir)) {
if (is_writable($dir)) {
return true;
}
}
return false;
}
}
?>