Basic commands for memcached installation, configuration, etc.

Source: Internet
Author: User
Tags md5 memcached memory usage

Basic commands for memcached (installation, uninstall, startup, configuration-related)

-P Listening Port
The IP address of the-l connection, the default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown shut down the running memcached service
-D Install installation memcached service
-d Uninstall Uninstall memcached service
-U Run as (only valid when run as root)
-m maximum memory usage, in MB. Default 64MB

-Returns an error when M memory is exhausted instead of deleting the item
-C Maximum Simultaneous connection number, default is 1024
-F Block size growth factor, default is 1.25
-N Minimum allocation space, key+value+flags default is 48
-H Display Help

memcached the basic command ( when memcached is activated for memcached Managed data and its own running state-related commands ):

Command Description Example
Get Reads a value Get MyKey
Set Set a key unconditionally Set MyKey 0 60 5
Add ADD a new key Add Newkey 0 60 5
Replace Overwrite existing Key Replace key 0 60 5
Append Append data to existing key Append key 0 60 15
Prepend Prepend data to existing key Prepend Key 0 60 15
incr Increments numerical key value by given number INCR MyKey 2
Decr Decrements numerical key value by given number DECR MyKey 5
Delete Deletes an existing key Delete MyKey
Flush_all Invalidate specific items immediately Flush_all
Invalidate all items in n seconds Flush_all 900
Stats Prints General statistics Stats
Prints Memory statistics Stats Slabs
Prints Memory statistics Stats malloc
Print Higher level allocation statistics Stats items
Stats Detail
Stats sizes
Resets statistics Stats Reset
Version Prints server version. Version
Verbosity Increases log level Verbosity
Quit Terminate Telnet session Quit
Keywords for viewing information in Chinese and English tables

Pid
Memcache The process ID of the server
Uptime
Number of seconds the server has been running
Time
Server's current UNIX timestamp
Version
Memcache version
Pointer_size
The current operating system pointer size (32-bit system is typically 32bit)
Rusage_user
Cumulative user time for the process
Rusage_system
Cumulative system time of the process
Curr_items
The number of items currently stored by the server
Total_items
Total number of items stored since server startup
bytes
The number of bytes consumed by the current server storage items
Curr_connections
The number of connections currently open
Total_connections
Number of connections that have been opened since the server was started
Connection_structures
Number of connection constructs that the server assigns
Cmd_get
Get command (gets) the total number of requests
Cmd_set
Set command (save) number of total requests
Get_hits
Total Hit Count
Get_misses
Total missed Hits
Evictions
Number of items deleted to get free memory (the space allocated to memcache needs to be removed from the old items to get space allocated to new items)
Bytes_read
Total Read bytes (number of bytes requested)
Bytes_written
Total Bytes sent (result bytes)
Limit_maxbytes
Size of memory allocated to Memcache (bytes)
Threads
Current number of threads

The following is a simple test code that accesses the object data with the identifier ' MyKey ' in the code:

The code is as follows Copy Code
<?php
Contains memcached class files
Require_once (' memcached-client.php ');
Option settings
$options = Array (
' Servers ' => array (' 192.168.1.1:11211 '),//memcached service address, port, multiple array elements can be used to represent more than one memcached service
' Debug ' => true,//whether to turn on debug
' Compress_threshold ' => 10240,//compressed with more than the number of bytes of data
' 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 ';
Writing objects to the memcached
$MC->add ($key, ' some random strings ');
$val = $MC->get ($key);
echo "n". Str_pad (' $MC->add () ', 60, ' _ '). " n ";
Var_dump ($val);
Replace the object data value that has been written
$MC->replace ($key, Array (' some ' => ' haha ', ' 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 not very simple, in practical applications, the result set of the database query is usually saved to the memcached, the next visit directly from the memcached, and no longer do the database query operations, which can greatly reduce the burden of the database. The value after the SQL statement MD5 () is typically used as the unique identifier key. Below is an example of caching a database query result set using memcached (this code fragment immediately follows the example code above):

  code is as follows copy code


<?php
$sql = ' SELECT * from users ';
$key = MD5 ($sql);//memcached object identifier
if (!) ( $datas = $MC->get ($key))) {
//cached data is not obtained in memcached, then a database query is used to get the recordset.
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 in the database to memcached for use on the next visit.
$MC->add ($key, $datas);
} else {
echo "n". Str_pad (' Read datas from memcached. ', 60, ' _ '). " n ";
}
Var_dump ($datas);
?

It can be seen that the use of memcached, you can reduce the database connection, query operations, the database load down, the speed of the script also improved.

OK, so memcached basic erection completed, if there is any problem or what I said wrong welcome to contact me

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.