PHPMemcached + APC + file cache encapsulation implementation code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags flock
PHPMemcached + APC + file cache encapsulation implementation code. Usage: Memcached copy code: $ cachenewCache_MemCache (); $ cache-addServer (www1); $ cache-addServer (www2, expires); thisserverhasdoublethememory usage:
Memcached

The code is as follows:


$ Cache = new Cache_MemCache ();
$ Cache-> addServer ('www1 ');
$ Cache-> addServer ('www2 ', expires 11,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 '));


File Cache

The code is 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 10 minutes
$ Cache-> store ($ key, $ data, 600 );
}


Download: class_cache3.php

The code is 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 Memcached code is as follows: $ cache = new Cache_MemCache (); $ cache-addServer ('www1'); $ cache-addServer ('www2 ', expires 11,20 ); // this server has double the memory...

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.