General memcache classes include consistent hash allocation
1. class. php
Config = array ('host' => '2017. 0.0.1 ', 'port' => 11211, 'persistent' => false, 'weight' => 1, // timeout time 'timeout' => 1, // retry interval: 'retry _ interval '=> 15, 'status' => false, // ==================================/// whether to enable the compression 'threshold' => 1, // compression ratio 'min _ savings '=> 0.2, // maximum number of retries 'max _ connect_retries' => 5); // if (! Isset ($ config ['memcache _ deploy']) |! Is_callable ($ config ['memcache _ deploy ']) {$ config ['memcache _ deploy'] = array (_ CLASS __, 'default _ memcache_deploy ');} // if (! Isset ($ config ['memcache _ hdeploy ']) |! Is_callable ($ config ['memcache _ hdeploy ']) {$ config ['memcache _ hdeploy'] = array (_ CLASS __, 'default _ memcache_hdeploy ');} // error handling if (! Isset ($ config ['failure _ callback']) |! Is_callable ($ config ['failure _ callback']) {$ config ['failure _ callback'] = array (_ CLASS __, 'default _ error_hander ');} $ this-> config = array_merge ($ this-> config, $ config); $ this-> memcache_obj = new \ Memcache;} public function host ($ host_list = '') {$ this-> host_list = $ host_list; return $ this;} // default error handling static public function default_error_hander ($ host = array () // var_dump ($ host ); {} static pu Blic function default_memcache_hdeploy ($ host, $ hash = '') {static $ node_object; if (! ($ Node_object instanceof yhash) {$ config = array (// 20 'replicas' for virtual nodes => 20); $ node_object = new yhash ($ config ); // add node $ node_object-> addTargets (array_keys ($ host);} return $ node_object-> lookup ($ hash);} static public function default_memcache_deploy ($ host) {$ keys = array_keys ($ host); $ key = array_rand ($ keys, 1); return $ keys [$ key];} public function alink () {$ default_host = array ('host' => $ This-> config ['host'], 'port' => $ this-> config ['port'], 'persistent' => $ this-> config ['persistent'], 'weight' => $ this-> config ['weight'], 'timeout' => $ this-> config ['timeout'], 'retry _ interval '=> $ this-> config ['retry _ interval'], 'status' => $ this-> config ['status'], 'failure _ callback' => $ this-> config ['failure _ callback'], 'threshold '=> $ this-> config ['threshold'], 'min _ savings '=> $ this-> config [' Min_savings ']); $ failure_callback = ''; if (! Empty ($ this-> host_list) {foreach ($ this-> host_list as $ _ host) {$ _ host = array_merge ($ default_host, $ _ host ); $ failure_callback = function ($ host = '', $ port = 11211) use ($ _ host) {return call_user_func_array ($ _ host ['failure _ callback'], array ($ _ host) ;}; $ this-> memcache_obj-> addServer ($ _ host ['host'], $ _ host ['port'], $ _ host ['persistent'], $ _ host ['weight'], $ _ host ['timeout'], $ _ host ['retry _ interv Al '], $ _ host ['status'], $ failure_callback); if ($ _ host ['threshold']) {$ this-> memcache_obj-> setCompressThreshold ($ _ host ['threshopld '], $ _ host ['min _ savings']);} else {$ failure_callback = function ($ host = '', $ port = 11211) use ($ default_host) {return call_user_func_array ($ default_host ['failure _ callback'], array ($ default_host) ;}; $ this-> memcache_obj-> addServer ($ default_host ['host'], $ defau Lt_host ['port'], $ default_host ['persistent'], $ default_host ['weight'], $ default_host ['timeout'], $ default_host ['retry _ interval '], $ default_host ['status'], $ failure_callback); if ($ default_host ['threshold']) {$ this-> memcache_obj-> setCompressThreshold ($ default_host ['threshopld '], $ default_host ['min _ savings']); }}} public function link () {$ default_host = ''; $ host_no =-1; $ return = false; $ _ host = Array (); if (! Empty ($ this-> host_list) {$ host_no = call_user_func_array ($ this-> config ['memcache _ deploy '], array ($ this-> host_list )); $ _ host = $ this-> host_list [$ host_no];} $ default_host = array ('host' => $ this-> config ['host'], 'port' => $ this-> config ['port'], 'persistent' => $ this-> config ['persistent'], // 'weight' => $ this-> config ['weight'], 'timeout' => $ this-> config ['timeout'], 'retry _ interval' => $ this-> conf Ig ['retry _ interval'], // 'status' => $ this-> config ['status'], 'failure _ callback' => $ this-> config ['failure _ callback'], 'threshold '=> $ this-> config ['threshold'], 'min _ savings '=> $ this-> config ['min _ savings']); $ _ host = array_merge ($ default_host, $ _ host ); if ($ _ host ['persistent']) {$ return =@ $ this-> memcache_obj-> pconnect ($ _ host ['host'], $ _ host ['port'], $ _ host ['timeout']);} else {$ return = @ $ this-> Memcache_obj-> connect ($ _ host ['host'], $ _ host ['port'], $ _ host ['timeout']);} if (! $ Return) {$ this-> connectFailures ++; if ($ this-> connectFailures <= $ this-> config ['max _ connect_retries ']) {if (count ($ this-> host_list)> 1) {call_user_func_array ($ _ host ['failure _ callback'], array ($ _ host )); usleep ($ _ host ['interval _ time']); unset ($ this-> host_list [$ host_no]); $ this-> link ();} else {$ this-> connectFailures = 0; call_user_func_array ($ _ host ['failure _ callback'], array ($ _ host);} e Lse {$ this-> connectFailures = 0; call_user_func_array ($ _ host ['failure _ callback'], array ($ _ host ));}} else {$ this-> connectFailures = 0; if ($ _ host ['threshold ']) {$ this-> memcache_obj-> setCompressThreshold ($ _ host ['threshold'], $ _ host ['min _ savings ']) ;}} public function hlink ($ hash = '') {$ default_host =''; $ host_no =-1; $ return = false; $ _ host = array (); if (! Empty ($ this-> host_list) {$ host_no = call_user_func_array ($ this-> config ['memcache _ hdeploy '], array ($ this-> host_list, $ hash); $ _ host = $ this-> host_list [$ host_no];} $ default_host = array ('host' => $ this-> config ['host'], 'port' => $ this-> config ['port'], 'persistent' => $ this-> config ['persistent'], // 'weight' => $ this-> config ['weight'], 'timeout' => $ this-> config ['timeout'], 'retry _ interval' => $ th Is-> config ['retry _ interval '], // 'status' => $ this-> config ['status'], 'failure _ callback' => $ this-> config ['failure _ callback'], 'threshold '=> $ this-> config ['threshold'], 'min _ savings '=> $ this-> config ['min _ savings'], 'hash' => $ hash); $ _ host = array_merge ($ default_host, $ _ host); if ($ _ host ['persistent']) {$ return = $ this-> memcache_obj-> pconnect ($ _ host ['host'], $ _ host ['port'], $ _ host ['timeout']);} Else {$ return = $ this-> memcache_obj-> connect ($ _ host ['host'], $ _ host ['port'], $ _ host ['timeout']);} if (! $ Return) {$ this-> connectFailures = 0; call_user_func_array ($ _ host ['failure _ callback'], array ($ _ host ));} else {if ($ _ host ['threshold ']) {$ this-> memcache_obj-> setCompressThreshold ($ _ host ['threshold'], $ _ host ['min _ savings ']); }}/ * type value reset, malloc, maps, cachedump, slabs, items, maximum number of items returned by id limit of sizes slabid slab */public function _ server ($ type = '', $ slabid = 1, $ limit = 1000000 ){// $ This-> memcache_obj-> close (); if ($ type = '') return $ this-> memcache_obj-> getStats (); if ($ type! = 'Cachedump ') return $ this-> memcache_obj-> getStats ($ type); return $ this-> memcache_obj-> getStats ($ type, $ slabid, $ limit );} public function _ allserver ($ type = '', $ slabid = 1, $ limit = 1000000) {if ($ type = '') return $ this-> memcache_obj-> getExtendedStats (); if ($ type! = 'Cachedump ') return $ this-> memcache_obj-> getExtendedStats ($ type); return $ this-> memcache_obj-> getExtendedStats ($ type, $ slabid, $ limit );} public function close () {return $ this-> memcache_obj-> close ();} public function slabid () {$ ids = $ id = array (); $ ids = $ this-> memcache_obj-> getStats ('slabs '); foreach ($ ids as $ k = >$ v) {if (is_int ($ k )) {$ id [] = $ k ;}} return $ id ;}//!!!!!!!!!!! ========================================================== === Public function getallkey ($ limit = 1000000) {$ ids = $ this-> slabid (); $ arr = array (); foreach ($ ids as $ id) {$ arr = array_merge (array_keys ($ this-> memcache_obj-> getStats ('cachedump ', $ id, $ limit), $ arr);} return $ arr ;} public function set ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) {return $ this-> memcache_obj-> set ($ key, $ var, $ flag, $ expire);} public function add ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) {return $ this-> memcache_obj-> add ($ key, $ var, $ flag, $ expire);} public function get ($ key, & $ flag = 0) {$ gflag = 0; $ return = $ this-> memcache_obj-> get ($ key, $ gflag); $ flag = $ gflag; return $ return;} public function delete ($ key, $ timeout = 0) {if ($ key = 'all') {return $ this-> memcache_obj-> flush ();} return $ this-> memcache_obj-> delete ($ key, $ timeout);} public function replace ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) {return $ this-> memcache_obj-> replace ($ key, $ var, $ flag, $ expire);} public function increment ($ key, $ var) {return $ this-> memcache_obj-> increment ($ key, (int) $ var);} public function decrement ($ key, $ var) {return $ this-> memcache_obj-> decrement ($ key, (int) $ var);} public function append ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) {$ return = $ this-> memcache_obj-> get ($ key); if (empty ($ return) return false; return $ this-> memcache_obj-> replace ($ key, $ return. $ var, $ flag, $ expire);} public function prepend ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) {$ return = $ this-> memcache_obj-> get ($ key); if (empty ($ return) return false; return $ this-> memcache_obj-> replace ($ key, $ var. $ return, $ flag, $ expire);} public function cas ($ key, $ key2) {$ return1 = $ this-> memcache_obj-> get ($ key ); if (empty ($ return1) return false; $ return2 = $ this-> memcache_obj-> get ($ key2); if (empty ($ return2) return false; return ($ this-> memcache_obj-> replace ($ key, $ return2) & ($ this-> memcache_obj-> replace ($ key2, $ return ));}}
2.Memcache. php
'2017. 0.0.1 ', // port 'port' => 11211, // whether persistent connection 'persistent' => false, // The proportion of 'weight' => 1, // timeout time 'timeout' => 1, // retry interval 'retry _ interval' => 15, // status 'status' => false, // ==================================/// whether to enable the compression 'threshold' => 1, // compression ratio 'min _ savings '=> 0.2, // maximum number of retries 'max _ connect_retries' => 5, // without consistent hash allocation scheme // callback function // parameter host array 'memcache _ deploy '=> 'default _ memcache_deploy ', // consistent hash allocation scheme // callback function // parameter host array hash value 'memcache _ hdeploy '=> 'default _ memcache_hdeploy ', // error handling // callback function // parameter host array 'failure _ callback' => 'default _ error_hander '); // ================================================ ========================================================== ========/// connection/* memcahe three connection methods 1. connection pool addserver2. normal connect/pconnect3. connect/pconnect with hash * // 1. connection pool $ memcache = new memcache (); $ host = array ('host' => 'localhost', 'port' => '123 '), array ('host' => '127. 0.0.1 ', 'port' => '123'), array ('host' => 'localhost', 'port' => '123 '), array ('host' => '127. 0.0.1 ', 'port' => '123'); $ memcache-> host ($ host); $ memcache-> alink (); // print_r ($ memcache-> _ allserver (); // ------------------------------------------------------------- // 2. normal connect/pconnect $ memcache2 = new memcache (); $ host = array ('host' => 'localhost', 'port' => '123 '), array ('host' => '127. 0.0.1 ', 'port' => '123'), array ('host' => 'localhost', 'port' => '123 '), array ('host' => '127. 0.0.1 ', 'port' => '123'); $ memcache2-> host ($ host); $ memcache2-> link (); // print_r ($ memcache2-> _ allserver (); // -------------------------------------------------------------- // 3. connect/pconnect // Import algorithm class include ('.. /lib/hash/class. php '); $ memcache3 = new memcache (); $ host = array ('host' => 'localhost', 'port' => '123 '), array ('host' => '127. 0.0.1 ', 'port' => '123'), array ('host' => 'localhost', 'port' => '123 '), array ('host' => '127. 0.0.1 ', 'port' => '123'); $ memcache3-> host ($ host); // $ memcache3-> hlink ('233ybl '); // print_r ($ memcache3-> _ allserver ()); // ================================================ ========================================================== ====/// built-in functions // set ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) // add ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) // get ($ key, & $ flag = 0) // delete ($ key, $ timeout = 0) // replace ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) // increment ($ key, $ var) // decrement ($ key, $ var) // append ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) // prepend ($ key, $ var, $ expire = 0, $ flag = MEMCACHE_COMPRESSED) // cas ($ key, $ key2) $ memcache3-> hlink ('32ybl'); $ memcache3-> set ('32ybl', 55); // $ memcache3-> increment ('32ybl', 333 ); echo $ memcache3-> get ('32ybl '); // ================================================ ====================================================/// system function // get the current connection server information // _ server ($ type = '', $ slabid = 1, $ limit = 1000000) // Obtain all connection server information // _ allserver ($ type = '', $ slabid = 1, $ limit = 1000000) // Obtain all key names // getallkey ($ limit = 1000000) var_dump ($ memcache-> getallkey ());