: This article describes how to use php to call memcache to store Sessions. if you are interested in the PHP Tutorial, refer to it. When php calls memcache to store sessions in the following way, remember
In/usr/local/php/etc/php. ini, set session. auto_start = 0 to 0. Otherwise, the memcache storage session is called and does not take effect.
The reason is that the session is automatically started when php executes the first line, and the default value is files. Therefore, the session is saved through the file by default, the ini_set configured later is useless.
// Session settings
Method 1:
This is the way the production environment is used.
Add the initialization command at the beginning of the line of the php code file.
Ini_set ("session. save_handler", "memcache ");
# If you need to configure multiple memcache addresses, separate them by commas.
# Ini_set ("session. save_path", "tcp: // 127.0.0.1: 11211, tcp: // 127.0.0.1: 11212 ");
Ini_set ("session. save_path", "tcp: // 127.0.0.1: 11211 ");
Session_start ();
Method 2:
This method writes a memcache node, which has been used in the production environment. the ip addresses configured with the two memcache instances have not been verified.
Directly set/usr/local/php/etc/php. ini
Session. save_handler = memcache
Session. save_path = "tcp: // fig: 11211"
Set_session.php
######################################## ###################
Ini_set ("session. save_handler", "memcache ");
Ini_set ("session. save_path", "tcp: // 10.12.4.25: 11211, tcp: // 10.12.4.25: 11212 ");
Session_start ();
$ _ SESSION ['test3'] = time ();
Print $ _ SESSION ['test3'];
Print session_id ();
?>
After the browser accesses set_session.php, we can telnet 10.12.4.25 11211 to check whether the session value actually exists and prove that it is saved only on one of the memcache nodes.
Get rtv10q183u28kmmtfpi0bd5nq6
######################################## ####################
Test whether multiple memcache nodes are distributed and stored on multiple memcache nodes.
######################################## #####################################
1. memcache multi-node configuration, and then assigned to them, will find that they are distributed and stored in multiple nodes
$ Memcache = new Memcache;
$ Memcache-> addServer ('10. 12.4.25 ', 11211 );
$ Memcache-> addServer ('10. 12.4.25 ', 11212 );
$ Memcache-> addServer ('10. 12.4.25 ', 11213 );
For ($ I = 0; $ I <20; $ I ++)
{
$ Memcache-> set ($ I, $ I. "hehe", 0, 1000 );
}
For ($ I = 0; $ I <20; $ I ++)
{
$ Val = $ memcache-> get ("$ I ");
Echo "Get $ I key1 value:". $ val ."
\ N ";
}
?>
Through get_memcache.php, we can see that the data is indeed scattered across multiple nodes.
Echo "10. 12.4.25 11211"
';
$ Memcache = new Memcache;
$ Memcache-> addServer ('10. 12.4.25 ', 11211 );
# $ Memcache-> addServer ('10. 12.4.25 ', 11213 );
For ($ I = 0; $ I <20; $ I ++)
{
$ Val = $ memcache-> get ("$ I ");
Echo "Get $ I key1 value:". $ val ."
\ N ";
}
Echo "10. 12.4.25 11212"
';
$ Memcache = new Memcache;
$ Memcache-> addServer ('10. 12.4.25 ', 11212 );
For ($ I = 0; $ I <20; $ I ++)
{
$ Val = $ memcache-> get ("$ I ");
Echo "Get $ I key1 value:". $ val ."
\ N ";
}
Echo "10. 12.4.25 11213"
';
$ Memcache = new Memcache;
$ Memcache-> addServer ('10. 12.4.25 ', 11213 );
For ($ I = 0; $ I <20; $ I ++)
{
$ Val = $ memcache-> get ("$ I ");
Echo "Get $ I key1 value:". $ val ."
\ N ";
}
?>
Clean_memcache.php is used to clear the memcache value, clear all the values, and then assign values again to check whether there is any problem. it is found that the storage is actually distributed.
Clean_memcache.php
$ Memcache = new Memcache;
$ Memcache-> connect ('10. 12.4.25', 11211 );
$ Memcache-> addServer ('10. 12.4.25 ', 11212 );
$ Memcache-> addServer ('10. 12.4.25 ', 11213 );
# $ Memcache-> addServer ('10. 12.4.25 ', 11214 );
$ Memcache-> flush ();
?>
When php calls memcache to store sessions in the following way, remember
In/usr/local/php/etc/php. ini, set session. auto_start = 0 to 0. Otherwise, the memcache storage session is called and does not take effect.
// Session settings
Ini_set ("session. save_handler", "memcache ");
# Ini_set ("session. save_path", "tcp: // 127.0.0.1: 11211, tcp: // 127.0.0.1: 11212 ");
Ini_set ("session. save_path", "tcp: // 127.0.0.1: 11211 ");
Session_start ();
The above describes how to use php to call memcache to store sessions, including some content. I hope to help my friends who are interested in PHP tutorials.