Memcached is a caching technology that puts data in memory and thus speeds it up through memory access

A large hashtable is maintained in memcached

There are currently three ways to obtain data

  1. Query database
  2. Use true static (files)
  3. Get it directly from memory

The memcached service maintains an in-memory table (hashtable)

key value

  1. Key is usually a string and cannot be repeated
  2. Value can be (string, value, array, object, Boolean, binary, null)

(actually serialized string)

The installation

  1. Download memcached. Exe
  2. Command line memcached.exe-d install

Start the

  1. Starting in the service
  2. Command line memcached.exe-d start

    Netstat-an: port 1121 is listening, indicating success
  3. Special boot memcached.exe-p 11211(port number (0-65535) is represented in 2 bytes and is normally used until 1024 (named port))

stop

memcached.exe -d stop

Netstat -anb can see which program is listening on which port (and which IP is connected)

Cause of installation failure

  1. Have no legal power
  2. The installation path has Chinese whitespace and other special characters

Operating the memcached

  1. The memcache extension operates directly using socket programming operations
  2. Telnet tools

Telnet (crud)

  • Log on to Telnet and connect to the memcached service

Telnet 127.0.0.1 11211

  • increase
Add key1 0 30 5 add key1 0 30 5 add key1 0 30 5
  • To obtain

get key

  • Modify the
Set key 0 30 5 replace key 0 30 5 replace key 0 30 5
  • delete

delete key

Prepend key 0 30 5 append prepend key 0 30 5 append

Flush_all deletes all data

Stats views memcached usage

PHP Memcache extension operation

  1. Install the extension and modify the configuration file
  2. Specific case
<? php header('content-type:text/html; charset=utf-8'); $mem=new Memcache(); $mem=new Memcache(); / / connect $mem - > connect (' 127.0.0.1 ', '11211') or die (' the connect error '); / / add * * * * * * * * * * * * * * * * * * * * * * / / increase the string $mem - > add (' name ', 'xiaobai', '0', '60) or die (' add STR error'); $res=$mem->get('name'); echo $res; $arr=array(1,2,3,4); $mem->add('list',$arr,'0','60') or die('add array error'); $res=$mem->get('list'); echo '<pre>'; var_dump($res); Class Cat{public $name; public $age; public function __construct($name,$age=1){ $this->name=$name; $this->age=$age; } $cat=new cat ($cat=new cat ($cat)); $mem - > add (' cat '$cat, 0') or die (' add obj error "); $res=$mem->get('cat'); var_dump($res); $mem->add('bool',false,0,60) or die('add bool error'); $res=$mem->get('bool'); var_dump($res); / / modify * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * $mem - > replace (' name ', 'black', 0 ') or die (' update STR error "); $res=$mem->get('name'); var_dump($res); / / delete * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * $mem - > delete (' name ') or die (' delete STR error '); //$mem->get('name') or die('name is not fund'); $mem->add('name',' mash ',0,strtotime('+ 31 days')) or die('31 days expire error'); $res=$mem->get('name') or die('name is not fund in expire'); var_dump($res); $mem->close(); $mem->close(); ? >

PHP source code manipulation of the memcached service (without the memcache extension)

  • Find the source code online, copy, paste, use

Know the memcached mechanism

  1. C/s architecture
  2. Libevent-based event handling, using libevent mechanism to handle concurrency (MySQL is multithreaded, Apache is MPM)

    Libevent is a package of cross-platform event handling interfaces
  3. Memory storage mode (after the content reaches the maximum value, use LRU algorithm to delete the data that has not been accessed for a long time)
  4. Client based distribution

Distributed storage + values

<? php $mem=new Memcache(); // Distributed storage $mm-> addServer('127.0.0.1',11210) or die(' CONNECT1 error'); $mem - > addServer (127.0.0.1, 11211) or die (' connect2 error "); $mm-> set('name1','hello1',0,300) or die('add STR error'); $mm-> set('name1','hello1',0,300); $mem->set('name2','hello2',0,300) or die('add STR error'); $mem->set('name3','hello3',0,300) or die('add STR error'); ? >
<? php $mem=new Memcache(); $mm-> addServer('127.0.0.1',11210) or die(' CONNECT1 error'); $mem - > addServer (127.0.0.1, 11211) or die (' connect2 error "); echo $mem->get('name1'); echo $mem->get('name2'); echo $mem->get('name3'); ? >

Pay attention to

  1. The memcached service’s data is not synchronized; it is distributed
  2. What data is put into which memcached object is determined by the client memcache object
  3. Instead of immediately connecting to the memcached service when you execute addServer, you compute the hash to determine which memcached server to connect to.

So there is no extra overhead when you pool a lot of server connections

The life cycle

  1. When the data is placed in Memcache, it will be timed. When the time is up, it will be destroyed. The time is 0, which means it is not expired
  2. Restart the memcached service
  3. Manually delete delete/flush_all

The session in a memcache

  1. Modify the PHP ini

    Session. Save_handler = [user | files | memcache] user custom session. Save_path = 'TCP: / / 127.0.0.1:11211, TCP: / / 127.0.0.1:11210' with commas
  2. SessionID is stored in Memcache
<? PHP // alter ini_set('session.save_handler','memcache'); // alter ini_set('session.save_handler','memcache'); Ini_set (' session. Save_path ', 'TCP: / / 127.0.0.1:11211, TCP: / / 127.0.0.1:11210'); session_start(); //echo session_id(); $_SESSION['name']='okokook'; The $_SESSION [' city '] = 'xi 'an; echo $_SESSION['name']; ? >

Memcached compared to sessions

  • Memcached’s main purpose is to speed things up, so it is stateless data, meaning the data is not bound to the user
  • Session is suitable for users to bind to stateful data

Memcached security

  1. Enable firewall in WIN (only allow local or Intranet access)
  2. Firewalls can also be started under Linux

    Iptables -a input -p TCP-S 127.0.0.1 -dport 11211-j ACCEPT

Data suitable for Memcache

Frequent changes and unstable data do not need real-time warehousing (online status, online number)