Maison > php教程 > PHP开发 > le corps du texte

Code d'implémentation de l'encapsulation du cache de fichiers PHP Memcached APC

高洛峰
Libérer: 2016-12-30 13:34:44
original
1176 Les gens l'ont consulté

Utilisation :
Memcached

$cache = new Cache_MemCache(); 
$cache->addServer('www1'); 
$cache->addServer('www2',11211,20); // this server has double the memory, and gets double the weight 
$cache->addServer('www3',11211); 
// Store some data in the cache for 10 minutes 
$cache->store('my_key','foobar',600); 
// Get it out of the cache again 
echo($cache->fetch('my_key'));
Copier après la connexion

Cache de fichiers

$cache = new Cache_File(); 
$key = 'getUsers:selectAll'; 
// check if the data is not in the cache already 
if (!$data = $cache->fetch($key)) { 
// assuming there is a database connection 
$result = mysql_query("SELECT * FROM users"); 
$data = array(); 
// fetching all the data and putting it in an array 
while($row = mysql_fetch_assoc($result)) { $data[] = $row; } 
// Storing the data in the cache for 10 minutes 
$cache->store($key,$data,600); 
}
Copier après la connexion

Téléchargement : class_cache3.php

<?php 

abstract class Cache_Abstract { 
abstract function fetch($key); 
abstract function store($key, $data, $ttl); 
abstract function delete($key); 
} 

class Cache_APC extends Cache_Abstract { 

function fetch($key) { 
return apc_fetch($key); 
} 

function store($key, $data, $ttl) { 
return apc_store($key, $data, $ttl); 
} 

function delete($key) { 
return apc_delete($key); 
} 

} 

class Cache_MemCache extends Cache_Abstract { 
public $connection; 

function __construct() { 
$this->connection = new MemCache; 
} 

function store($key, $data, $ttl) { 
return $this->connection->set($key, $data, 0, $ttl); 
} 

function fetch($key) { 
return $this->connection->get($key); 
} 

function delete($key) { 
return $this->connection->delete($key); 
} 

function addServer($host, $port = 11211, $weight = 10) { 
$this->connection->addServer($host, $port, true, $weight); 
} 

} 

class Cache_File extends Cache_Abstract { 

function store($key, $data, $ttl) { 
$h = fopen($this->getFileName($key), &#39;a+&#39;); 
if (!$h) 
throw new Exception(&#39;Could not write to cache&#39;); 
flock($h, LOCK_EX); 
fseek($h, 0); 
ftruncate($h, 0); 
$data = serialize(array(time() + $ttl, $data)); 
if (fwrite($h, $data) === false) { 
throw new Exception(&#39;Could not write to cache&#39;); 
} 
fclose($h); 
} 

function fetch($key) { 
$filename = $this->getFileName($key); 
if (!file_exists($filename)) 
return false; 
$h = fopen($filename, &#39;r&#39;); 
if (!$h) 
return false; 
flock($h, LOCK_SH); 
$data = file_get_contents($filename); 
fclose($h); 
$data = @ unserialize($data); 
if (!$data) { 
unlink($filename); 
return false; 
} 
if (time() > $data[0]) { 
unlink($filename); 
return false; 
} 
return $data[1]; 
} 

function delete($key) { 
$filename = $this->getFileName($key); 
if (file_exists($filename)) { 
return unlink($filename); 
} 
else { 
return false; 
} 
} 

private function getFileName($key) { 
return &#39;/tmp/s_cache&#39; . md5($key); 
} 

} 
?>
Copier après la connexion

Plus de fichiers PHP Memcached APC Pour les articles concernant le code d'implémentation de l'encapsulation du cache, veuillez faire attention au site Web PHP chinois !

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!