Install memcached in Windows

Source: Internet
Author: User
Tags memcached windows php memcached

Official memcached website
Http://www.danga.com/memcached/
Memcached windows Official Website
Http://jehiah.cz/projects/memcached-win32/

Memcached 1.2.0 for Win32 is the latest version. libevent 1.2 is required.

Unzip the binaries in your desired directory (eg. c: \ memcached)
Install the service using the command: 'C: \ memcached \ memcached.exe-D install 'from either the command line
Start the server from the Microsoft Management Console or by running the following command: 'C: \ memcached \ memcached.exe-D start'
Use the server, by default listening to port 11211
Use 'memcached.exe-H' for extra help and command line server

In the future, memcached will be used as a Windows service to automatically start every time it is started.
Remove the comment before 'extension = php_memcache.dll 'in PHP. ini.
Download the memcache module package of PECL to the ext directory.
Note: PHP and PECL versions must be consistent.

  1. <? PHP
  2. $ Memcache_obj=New Memcache;
  3. /* Connect to memcached server */
  4. $ Memcache_obj->Connect('Localhost',11211);
  5. /*
  6. Set Value of item with key 'var _ key', using on-the-fly Compression
  7. Expire time is 50 seconds
  8. */
  9. $ Memcache_obj->Set('Var_key','Some really big variable',Memcache_compressed,50);
  10. Echo $ Memcache_obj->Get('Var_key');
  11. ?>

Displaying "some really big variable" is successful.

Memcached for Win32

This is a port of memcached to the Win32 architecture by kronuz

The Win32 version of memcached can be run both as a NT Service or from the command line.
To install memcached as a service, follow the next steps:

    1. Unzip the binaries in your desired directory (eg. c: \ memcached)
    2. Install the service using the command: 'C: \ memcached \ memcached.exe-D install 'from either the command line
    3. Start the server from the Microsoft Management Console or by running the following command: 'C: \ memcached \ memcached.exe-D start'
    4. Use the server, by default listening to port 11211

Use 'memcached.exe-H' for extra help and command line server
You can check the changelog to see what's new.

PHP memcached application example

First download the memcached-client.php, after downloading the memcached-client.php, you can operate the memcached service through the class "memcached" in this file. ActuallyCodeCalling is very simple. The main methods used include add (), get (), replace (), and delete (). The methods are described as follows:

Add ($ key, $ Val, $ exp = 0)
Write an object to memcached. $ key is the unique identifier of the object. $ Val is the data of the written object. $ exp is the expiration time, in seconds. The default value is unlimited;

Get ($ key)
Get object data from memcached and get it through the unique identifier $ key of the object;

Replace ($ key, $ value, $ exp = 0)
Replace $ value with the content of the $ key object in memcached. The parameter works only when the $ key object exists like the add () method;

Delete ($ key, $ time = 0)
Delete an object with the identifier $ key in memcached. $ time is an optional parameter, indicating how long it will take before deletion.

The following is a simple test code that allows you to access the object data with the identifier 'mykey:

The following is a reference clip:
<? PHP
// Contains the memcached class file
Require_once ('memcached-client. php ');
// Option settings
$ Options = array (
'Servers' => array ('192. 168.1.1: 100'), // address and port of the memcached service. Multiple array elements can be used to represent multiple memcached services.
'Debug' => true, // whether to enable debug
'Compress _ threshold '=> 10240, // when the number of bytes of data exceeds
'Persistant' => false // whether to use persistent connection
);
// Create a memcached object instance
$ MC = new memcached ($ options );
// Set the unique identifier used by this script
$ Key = 'mykey ';
// Write an object to memcached
$ Mc-> Add ($ key, 'some random string ');
$ Val = $ Mc-> get ($ key );
Echo "N". str_pad ('$ Mc-> Add ()', 60, '_'). "N ";
Var_dump ($ Val );
// Replace the written object data value
$ Mc-> Replace ($ key, array ('some' => 'hahaha', 'array' => 'xxx '));
$ Val = $ Mc-> get ($ key );
Echo "N". str_pad ('$ Mc-> Replace ()', 60, '_'). "N ";
Var_dump ($ Val );
// Delete an object in memcached
$ Mc-> Delete ($ key );
$ Val = $ Mc-> get ($ key );
Echo "N". str_pad ('$ Mc-> Delete ()', 60, '_'). "N ";
Var_dump ($ Val );
?>

Is it very easy? In actual applications, the database query result set is usually saved to memcached. The result set is obtained directly from memcached during the next visit, instead of performing database query operations, this can greatly reduce the burden on the database. Generally, the value after the MD5 () of the SQL statement is used as the unique identifier key. Below is an example of using memcached to cache the database query result set (this code snippet is followed by the above sample code ):

The following is a reference clip:
<? PHP
$ SQL = 'select * From users ';
$ Key = MD5 ($ SQL); // memcached object identifier
If (! ($ Datas = $ Mc-> get ($ key ))){
// If no cached data is obtained in memcached, use database query to obtain the record set.
Echo "N". str_pad ('read datas from mysql. ', 60,' _ '). "N ";
$ Conn = mysql_connect ('localhost', 'test', 'test ');
Mysql_select_db ('test ');
$ Result = mysql_query ($ SQL );
While ($ ROW = mysql_fetch_object ($ result ))
$ Datas [] = $ row;
// Save the result set data obtained from the database to memcached for the next access.
$ Mc-> Add ($ key, $ datas );
} Else {
Echo "N". str_pad ('read datas from memcached. ', 60,' _ '). "N ";
}
Var_dump ($ datas );
?>

Related Article

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.