Install, configure, and use memcached in windows

Source: Internet
Author: User
Tags flush memcached memory usage stmt iptables mysql database port number firewall

Memcached software download and installation tutorial

1. Download the memcached compressed package, which contains 32-bit and 64-bit versions. Select the corresponding version of your system. Decompress the package to a disk, for example, in the D: wampbin directory.

Download memcached software: memcached.zip

2. Enter the following command on the terminal (CMD command line:


① Input d: Switch to disk d;
② Enter cd wampbin and switch to the D: wampbin directory;
③ Enter dir to check whether memcached software exists in the current directory.
④ Input:
Install memcached.exe-d install
Memcached.exe-d uninstall
Memcached.exe-d stop
Memcached.exe-h for all help
3. Connect after startup:
D: wampbin> telnet 127.0.0.1 11211-connect to memcache port 11211 and exit with quit.

Memcached server CMD setting command

Common Memcache startup parameters
-P: Set the port number (default value: 11211)
-U udp listening port (default: 11211, disabled when 0)
-L binding address (default: all IP addresses are allowed, regardless of internal and external networks or the local machine IP address replacement, there is a security risk, if set to 127.0.0.1, only local access is allowed)
-D independent process running:
-D start: start the memcached service.
-D restart: restart the memcached service.
-D stop | shutdown the running memcached service
-D install the memcached service
-D uninstall memcached service
-U binding is used to run processes
-M: maximum memory usage, in MB (default: 64 MB)
-P writes the PID to the file, so that the process can be terminated quickly later. It must be used with-d.
-An error is returned when M memory is used up, instead of deleting items.
-C: maximum number of simultaneous connections. The default value is 1024.
-F block size growth factor, default value: 1.25
-N: Minimum allocation space. The default value of key + value + flags is 48.
-H Show Help

Common Memcache Operation Commands

• Five common commands
-Stats: status information of all currently running memcached servers
-Add: add a data entry to the server.
-Set: replace an existing data. If the data does not exist, it is the same as the add command.
-Get: extract the specified data from the server.
-Delete: deletes a specified single data. To clear all data, use the flush_all command.
• Memcache protocol errors mainly involve three error prompts:
-ERROR-common ERROR messages, such as command errors
-CLIENT_ERROR <error message>-Client Error
-SERVER_ERROR <error message>-server errors
 
Data management commands
• Format: <Command> <key> <tag> <validity period> <data length>
Where:
-Command: add, set, delete, get)
-<Key>-key: the key content of the sent command.
-<Flag>-flags indicates the flags used to call the set command to save data.
-Validity period: the validity period of data on the server. If it is 0, the data will always be valid, in seconds.
-Data length: the length of block data blocks. Generally, after the length ends, the next line follows the block data content,
-After the data is sent, the client generally waits for the return from the server. The return from the server:
-STORED data is successfully saved.
-The NOT_STORED data fails to be saved because the data key already exists on the server.

PHP memcache function extension module installation

① Place the downloaded php_memcache.dll file in the PHP Extension Library Directory (under the php ext directory ):

② Add "extension = php_memcache.dll" to the PHP. Ini file to open the memcache. dll extension Library:
③ Restart the appache service.
Use the phpinfo () function to check whether memcache. dll is successfully installed:

PHP method for MemCache operations


Memcache class
Memcache: add ---- add an entry to the cache server
Memcache: addServer ---- add a memcache server to the connection pool
Memcache: close ---- close memcache connection
Memcache: connect ---- open a memcached server connection
Memcache: decrement ---- reduce the element value
Memcache: delete ---- delete an element from the server
Memcache: flush ---- clean (delete) all elements that have been stored
Memcache: get ---- check an element from the server
Memcache: getExtendedStats ---- statistics of all servers in the cache server pool
Memcache: getServerStatus ---- get the online/offline status of a server
Memcache: getStats ---- get server statistics
Memcache: getVersion ---- Return the server version information
Memcache: increment ---- add the value of an element
Memcache: pconnect ---- open a persistent connection to the server
Memcache: replace ---- replace the value of an existing element
Memcache: set ---- Store data at the server
Memcache: setCompressThreshold ----- enable automatic compaction
Memcache: setServerParams ---- modify server parameters and status during runtime
Memcache function memcache_debug ---- switch debug output on/off

MemCache instance application

The code is as follows: Copy code

<? Php
// 1. Create an object
$ Mem = newMemcache ();
// 2. Add a server
$ Mem-> addServer ("192.168.150.250", 11211 );
$ Mem-> addServer ("192.168.150.138", 11211 );
$ Mem-> addServer ("192.168.112.128", 11211 );
// 3. Placement information
$ Mem-> add ("mystr", "hello memcache! ", MEMCACHE_COMPRESSED, 0 );
$ Mem-> add ("myarray", array (10, 20, 30, 40), MEMCACHE_COMPRESSED, 0 );
$ Mem-> add ("myob", newStu (), MEMCACHE_COMPRESSED, 0 );
// 4. Obtain information
Echo $ mem-> get ("mystr"). "<br/> ";
Var_dump ($ mem-> get ('myarray '));
Echo "<br/> ";
$ Mem-> get ("myob")-> getinfo ();
?>

MemCache instance application

<? Php
// Create a memcache object
$ Mem = new Memcache;
 
// Connect to the memcache server
$ Mem-> connect ("localhost", 11211 );
 
Class Test {
Public $ a = 1;
Public $ B = 2;
Public $ c = 3;
    }
 
$ Mem-> add ("one", "this is memecache test! ", MEMCACHE_COMPRESSED, time () + 60*60*24*31 );
$ Mem-> add ("two", array ("111", "222", "3333"), MEMCACHE_COMPRESSED, 0 );
$ Mem-> add ("three", new Test (), MEMCACHE_COMPRESSED, 0 );
$ Mem-> add ("four", 100, MEMCACHE_COMPRESSED, 0 );
 
$ Mem-> set ("five", "this is a demo! ", MEMCACHE_COMPRESSED, 1000 );
 
Var_dump ($ mem-> get ("one "));
Echo '<br> ';
 
Var_dump ($ mem-> get ("two "));
Echo '<br> ';
 
Var_dump ($ mem-> get ("three "));
Echo '<br> ';
 
Var_dump ($ mem-> get ("five "));
Echo '<br> ';
 
Var_dump ($ mem-> get ("four "));
Echo '<br> ';
 
Var_dump ($ mem-> get (array ("one", "five ")));
Echo '<br> ';
 
$ Mem-> delete ("one ");
$ Mem-> delete ("two", 0 );
$ Mem-> flush ();
 
$ Mem-> close ();
?>

Security protection for memcached servers


Access to the mysql database server must pass user authentication before access, and access to the memcached server is directly connected through the client, without any verification process. It is very dangerous for a server to leak directly on the internet. If it is light on data leaks, the server will be infiltrated, and there may be some unknown situations, so the risks are foreseeable. To ensure security, you can use either of the following methods:
1. Intranet access
Intranet access between networks can effectively prevent other illegal access. To allow multiple distributed memcached servers to access the memcached server only in the internal LAN, you need to set a network card in the Web server to access the memcached server over the intranet, and another network card of the Web server to the Internet. When the memcached server is started, it listens to the intranet IP address and port. The memcached startup options are as follows:
Memcached-d-m 1024-u root-l 192.168.0.10-p 11211-c 1024 start
This command sets the memcached server to listen to the intranet IP address 192.168.0.10 after it is started. The listening port is 11211, which occupies 1024 MB of memory and allows a maximum of concurrent connections.
2. Set firewall
Setting a firewall is simple and effective. If memcached and Web Server are on the same machine, or, if you access memcached through an Internet IP address, you need to use a firewall or proxy program to filter out illegal access. In Linux, iptables is usually used to specify rules to prevent unauthorized access. For example, you can only allow your Web server to access our memcached server and organize other accesses. The rules of the firewall iptables are as follows:
Iptables-F
Iptables-P INPUT DROP
Iptables-a input-p tcp-s 192.168.0.10 -- dport 11211-j ACCEPT
Iptables-a input-p udp-s 192.168.0.10 -- dport 11211-j ACCEPT
The above iptables rule only allows access from the Web server 192.168.0.10 to the memcached server, which can block some illegal access. Of course, you can also add other rules to enhance security. You need to set them according to your own needs.


Memcache instance application-cache database data

The most common MemCache application in a project is to cache the data results queried from the database and save session control information ). Use the memcached server to cache the database query results to reduce the pressure on the database caused by frequent database connections and a large number of queries. The design principle is that as long as the records in the database are not changed, you do not need to reconnect to the database and execute repeated query statements repeatedly. The same query results should be obtained from the cache server. An example of memcache is as follows:

The code is as follows: Copy code

<? Php
// Create a memcache object
$ Mem = new Memcache;
 
// Connect to the memcache server
$ Mem-> addServer ("localhost", 11211 );
$ Mem-> addServer ("192.168.1.20.", 11211 );
 
// Database connection and operations
$ SQL = "select id, name, pass, age, sex, email from users order by id ";
 
$ Key = md5 ($ SQL );
// Data directly from memory memcahce
$ Data = $ mem-> get ($ key );
 
// If the data is used, the data is returned from the content. If the data is not connected to the database, the SQL statement is executed.
If (empty ($ data )){
 
// Connection
Try {
$ Pdo = new PDO ("mysql: host = localhost; dbname = xsphp", "root", "123456 ");
} Catch (PDOException $ e ){
Echo "database connection failed:". $ e-> getMessage ();
        }
 
// Obtain the data and execute the query statement
$ Stmt = $ pdo-> prepare ($ SQL );
 
$ Stmt-> execute ();
 
$ Data = $ stmt-> fetchAll (PDO: FETCH_ASSOC );
 
$ Mem-> set ($ key, $ data, MEMCACHE_COMPRESSED, 10 );
 
Echo "this is the first time you access the data obtained from the database and put it in the memory! <Br> ";
    }
 
Echo '<pre> ';
Print_r ($ data );
Echo '</pre> ';
 
// Close the connection
$ Mem-> close ();
 
?>

 

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.