Learning PHP MemCache at the front end and learning PHP MemCache
* Directory [1] function [2] install [3] manage [4] commands
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. Simply put, the data is called to the memory and then read from the memory, which greatly improves the reading speed. This article describes the content of MemCache in detail.
Function
Memcache is a danga project that was first developed by LiveJour MemCachenal to accelerate LiveJournal access. Later, Memcache was used by many large websites.
Memcache runs on one or more servers as a daemon and receives client connections and operations at any time.
The primary purpose of the MemCache cache system is to increase the dynamic web page application and relieve the Database Retrieval pressure. Memcache can be used to relieve database pressure when the website traffic is large. The main focus is on the following two aspects: 1. use MemCache as the intermediate cache layer to reduce database pressure and 2. distributed MemCache applications
Install
1. download the software and decompress it. A total of three files are listed below.
2. install the service on windows, open the cmd command line, enter the memcached directory, execute the memcached-d install command, and install the service.
[Note] If the "failed to install service or service already installed" error occurs without installation, it can be that cmd.exe needs to run as an administrator.
3. start the service and execute memcached.exe-d start.
Finally, choose computer> Management> services to find the memcached service.
Management
The port number of memcache is 11211, which is connected after the memcache service is started.
[Note] telnet is disabled by default in windows, so it must be enabled manually.
telnet 127.0.0.1 11211
After entering the memcache service, enter the stats Command, as shown in the following figure.
The stats command is used to dump the current statistics of the connected memcached instance. In the following example, the stats command displays information about the current memcached instance.
STAT pid 22459 process id stat uptime 1027046 server running seconds STAT time 1273043062 server current unix timestamp STAT version 1.4.4 server version STAT pointer_size 64 operating system word size (this server is 64-bit) STAT rusage_user 0.040000 process cumulative user time STAT rusage_system 0.260000 process cumulative system time STAT curr_connections 10 number of currently open connections STAT total_connections 82 number of previously opened connections STAT connection_structures 13 Number of connection structures allocated by the server STAT cmd_get 54 total number of get commands executed STAT cmd_set 34 Total number of set commands executed STAT cmd_flush 3 Total number of times flush_all commands STAT get_hits 9 get hits STAT get_misses 45 get misses STAT delete_misses 5 delete misses STAT delete_hits 1 delete hits STAT incr_misses 0 incr misses STAT incr_hits 0 incr hits STAT decr_misses 0 decr misses STAT decr_hits 0 decr hits STAT cas_misses 0 cas misses STAT cas_hits 0 cas hit count STAT cas_badval 0 use wipe count STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 15785 read bytes total STAT bytes_written 15222 write bytes total STAT limit_maxbytes 1048576 allocated memory (bytes) STAT accepting_conns 1 currently accepts connections STAT listen_disabled_num 0 STAT threads 4 threads STAT conn_yields 0 STAT bytes 0 storage item Byte Count STAT curr_items 0 item count STAT total_items 34 Total item count STAT evictions 0 is obtains the total number of items deleted from a space.
Use the quit command to exit the memcache operation interface.
Command
You can use memcache.exe-h to view commands supported by memcache.
-P <num> set the port number (default value: 11211)-U <num> UDP listening port (default value: 11211, disabled when 0) -l <ip_addr> binding address (default: All IP addresses are allowed, regardless of Intranet and Internet addresses or local IP addresses. If this parameter is set to 127.0.0.1, only local access is allowed) -d independent process run-d start memcached service-d restart memcached service-d stop | shutdown disable the running memcached service-d install memcached service-d uninstall memcached Service -u <username> bind to use the specified <username>-m <num> for running processes to allow maximum memory usage, unit: M (default: 64 MB)-P <file> writes the PID to a file <file>, which can cause fast process termination, an error is returned when you need to use-M memory with-d, instead of deleting item-c. The default value is 1024-f, the default value is 1.25-n, and the minimum space is allocated. The key + value + flags value is 48-h by default.
Generally, memcache uses the following five common commands:
Stats: The status information of all current memcached servers running add: add a data to the server set: replace an existing data. If the data does not exist, it is the same as the add command. Get: extract specified data from the server. Delete: delete a specified data. to clear all data, run the flush_all command.
The memcache error message mainly includes the following three commands:
ERROR -- common ERROR information, such as command ERROR CLIENT_ERROR <ERROR message> -- client ERROR SERVER_ERROR <ERROR message> -- Server ERROR
Command Format
Format: <command> <key> <tag> <validity period> <data length>
Command: add, set, delete, get)
<Key>-key: key content of the sent command
<Flag>-flags: flags when the set command is called 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.
Return Value
After data is sent, the client generally waits for the server to return data. The return values of the server include the following:
STORED data is successfully saved. NOT_STORED Data fails to be saved because the data key already exists on the server.
The following uses add one 1 0 5 as an example. add indicates adding data, one indicates the key name, 1 indicates the mark, 0 indicates the validity period is permanent, and 5 indicates the length is 5.
Press enter, enter 12345, and then press enter, which indicates that the key value is 12345 and is saved successfully.
You can use get one to find information about the key name one and its key value.
Then use set one to change the tag to 2.
Then, you can use get one to find the information and key value of the key name one.
Delete one by using delete one, and then read the information of one by using get one.
Generally, traversal operations are not often used when memcache is used, but traversal behaviors can be simulated. First, store 5 data records.
Then, execute the stats items command, and you can see many items lines. Execute the stats cachedump 1 0 command. 1 indicates the number following the items in the figure above. 0 indicates that all data is displayed. If it is 1, only one item is displayed.