Memcached PHP Memcached + APC + file cache encapsulation Implementation code

Source: Internet
Author: User
Tags apc flock php memcached
How to use:
Memcached

Copy the Code code as follows:


$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 ten minutes
$cache->store (' My_key ', ' foobar ', 600);
Get it out of the cache again
Echo ($cache->fetch (' My_key '));


File cache

Copy the Code code as follows:


$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 ten minutes
$cache->store ($key, $data, 600);
}


Download: class_cache3.php

Copy the Code code as follows:


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), ' A + ');
if (! $h)
throw new Exception (' Could not write to cache ');
Flock ($h, LOCK_EX);
Fseek ($h, 0);
Ftruncate ($h, 0);
$data = Serialize (Array (time () + $ttl, $data));
if (fwrite ($h, $data) = = = = False) {
throw new Exception (' Could not write to cache ');
}
Fclose ($h);
}
function Fetch ($key) {
$filename = $this->getfilename ($key);
if (!file_exists ($filename))
return false;
$h = fopen ($filename, ' R ');
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 '/tmp/s_cache '. MD5 ($key);
}
}
?>

The above describes the Memcached php Memcached + APC + file cache encapsulation implementation code, including the Memcached aspect of the content, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.