Installing and using PHPMemcache and interacting with mysql instances

Source: Internet
Author: User
I. INTRODUCTION to Memcache: Memcache is a high-performance distributed memory object cache system that maintains a unified and huge hash table in the memory. it can be used to store data in various formats, including image, video, file, and database retrieval results. Memcache is one of danga... "> <LINKhref =" http://www.php100.com//stat

1. Introduction to Memcache

Memcache is a high-performance distributed memory object cache system. by maintaining a unified and huge hash table in the memory, Memcache can be used to store data in various formats, including image, video, file, and database retrieval results.

Memcache is a danga project that was first developed for the LiveJournal service and was later used by many large websites to accelerate the LiveJournal access speed.

The Memcache module is an efficient daemon process that provides process-based programs and object-oriented interfaces with memory cache, especially when designing dynamic Web programs to reduce access to the database.



II. install memcache in Windows
1. download Memcache for win32

Http://www.php100.com/html/download/tools/2010/0125/3857.html

2. download php_memcache.dll (php 5.3.8 memcache extension dll)

Http://hi.baidu.com/%E5%E2%E5%CB%C3% AB %C3% AB %B3%E6/blog/item/7612cf35a4425896a71e1267.html

(Note: pay attention to the PHP version you have installed to download the corresponding dll file)

1. download the stable version of memcache for windows and decompress it to a disk, for example, in c: \ memcached
2. enter 'C: \ memcached \ memcached.exe-d install' on the terminal (that is, the cmd command interface) to install
3. enter 'C: \ memcached \ memcached.exe-d start' to start.

4. download php_memcache.dll. find the corresponding php version file (in the php/ext folder)
5. add a line 'extension = php_memcache.dll 'to php. ini'
6. restart Apache and check phpinfo. if memcache exists, the installation is successful!



III. how to monitor the running status of the memcache server
• A memcache. php file is added to the latest memcache pecl. This php file can be used to conveniently view the memcache status.

• You can download the source file here: http://livebookmark.net/memcachephp/memcachephp.zip

• Configuration details

• 1. set the user name and password

• Define ('admin _ username', 'memcache'); // ADMIN USERNAME

Define ('admin _ password', 'password'); // ADMIN PASSWORD

• 2. configure the server and its ports (memcache for multiple hosts can be detected)

• $ MEMCACHE_SERVERS [] = 'mymemcache-server1: 8080'; // add more as an array
$ MEMCACHE_SERVERS [] = 'mymemcache-server2: 8080'; // add more as an array

• Changed to: $ MEMCACHE_SERVERS [] = '192. 0.0.1: 100 ′;
$ MEMCACHE_SERVERS [] = '192. 168.0.1: 100 ′;



4. write a small program code that interacts with MySQL.
Header ("Content-Type: text/html; charset = utf-8 ");
$ Db_user = 'root ';
$ Db_passwd = '';
$ Dsn = "mysql: host = localhost; port = 3306; dbname = test ";

$ Memcachehost = '192. 0.0.1 '; // cache server
$ Memcacheport = 11211; // cache server port
$ Memcachelife = 0; // cache lifecycle

$ Mem = new Memcache (); // instantiate a Memcache object
$ Mem-> connect ($ memcachehost, $ memcacheport); // $ mem-> connect (cache host, cache port)

Try
{// Exception capture
$ Pdo = new PDO ($ dsn, $ db_user, $ db_passwd, array (PDO: MYSQL_ATTR_INIT_COMMAND => "set names 'utf8'", PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY => true ));
Unset ($ db_user );
Unset ($ db_passwd); // release variables and database security policies
}
Catch (PDOException $ e)
{
Echo $ e-> getMessage ();
Exit;
}

Function query_memcache ($ SQL ){
Global $ mem, $ memcachelife;
$ Key = md5 ($ SQL );

If (! ($ Value = $ mem-> get ($ key) {// Cache does not exist, then query from My SQL
Echo 11 .'
';
$ Microtime1 = microtime (); // sets the time before the database is retrieved.
$ Query = query ($ SQL );
While ($ item = mysql_fetch_array ($ query ))
{
$ Result [] = $ item;
}
$ Value = $ result;
$ Microtime2 = microtime (); // sets the time after the database is retrieved.
// Write Key and Value to MemCache
$ Mem-> set ($ key, $ result, 0, $ memcachelife );
$ D_value = $ microtime2-$ microtime1; // return a time difference
}
Else
{
Echo 22 .'
';
$ Microtime3 = microtime (); // you can specify a time range from memcache.
$ Value = $ mem-> get ($ key );
$ Microtime4 = microtime (); // sets the time after the value is obtained from memcache.
$ D_value = $ microtime4-$ microtime3; // return a time difference
}

Echo $ d_value;
Return $ value;
}

Function query ($ SQL)
{
If (''= $ SQL)
{
Echo 'SQL statement error: SQL statement is empty ';
}
$ Query = mysql_query ($ SQL );
If (! $ Query)
{
Echo 'wrong SQL statement ';
}
Return $ query;
}

Foreach (query_memcache ("select * from test. memcache where value = 'cs1336042255o78 '") as $ row)
{
// Echo $ row [0]. '-'. $ row [1].'

';
}

// $ Mem-> flush (); // refresh the cache

V. test with time difference
Time difference test passed
Two ways: 1. directly retrieve data from the database, set a timestamp before getting data, set a timestamp after getting data, subtract two timestamps, and return a time difference
2. retrieve data from memcache memory, set a timestamp before getting data from memory, set a timestamp after getting data, subtract the two timestamps, and return a time difference

The time difference returned when the program is run for the first time is the time used to retrieve data from the database.

The time difference obtained by running the program later is the time used to extract data from the memory.

It can be clearly seen that the average time used to retrieve data from the database is about 1000 times the time used to retrieve data from 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.