123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187 |
- <?php
- function wp_cache_add( $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->add( $key, $value, $group, $expiration );
- }
- function wp_cache_add_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->addByKey( $server_key, $key, $value, $group, $expiration );
- }
- function wp_cache_add_server( $host, $port, $weight = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->addServer( $host, $port, $weight );
- }
- function wp_cache_add_servers( $servers ) {
- global $wp_object_cache;
- return $wp_object_cache->addServers( $servers );
- }
- function wp_cache_append( $key, $value, $group = '' ) {
- global $wp_object_cache;
- return $wp_object_cache->append( $key, $value, $group );
- }
- function wp_cache_append_by_key( $server_key, $key, $value, $group = '' ) {
- global $wp_object_cache;
- return $wp_object_cache->appendByKey( $server_key, $key, $value, $group );
- }
- function wp_cache_cas( $cas_token, $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->cas( $cas_token, $key, $value, $group, $expiration );
- }
- function wp_cache_cas_by_key( $cas_token, $server_key, $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->casByKey( $cas_token, $server_key, $key, $value, $group, $expiration );
- }
- function wp_cache_close() {
- return true;
- }
- function wp_cache_decrement( $key, $offset = 1, $group = '' ) {
- global $wp_object_cache;
- return $wp_object_cache->decrement( $key, $offset, $group );
- }
- function wp_cache_decr( $key, $offset = 1, $group = '' ) {
- return wp_cache_decrement( $key, $offset, $group );
- }
- function wp_cache_delete( $key, $group = '', $time = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->delete( $key, $group, $time );
- }
- function wp_cache_delete_by_key( $server_key, $key, $group = '', $time = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->deleteByKey( $server_key, $key, $group, $time );
- }
- function wp_cache_fetch() {
- global $wp_object_cache;
- return $wp_object_cache->fetch();
- }
- function wp_cache_fetch_all() {
- global $wp_object_cache;
- return $wp_object_cache->fetchAll();
- }
- function wp_cache_flush( $delay = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->flush( $delay );
- }
- function wp_cache_get( $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
- global $wp_object_cache;
- if ( func_num_args() > 4 ) {
- return $wp_object_cache->get( $key, $group, $force, $found, '', false, $cache_cb, $cas_token );
- } else {
- return $wp_object_cache->get( $key, $group, $force, $found );
- }
- }
- function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
- global $wp_object_cache;
- if ( func_num_args() > 5 ) {
- return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found, $cache_cb, $cas_token );
- } else {
- return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found );
- }
- }
- function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = null ) {
- global $wp_object_cache;
- return $wp_object_cache->getDelayed( $keys, $groups, $with_cas, $value_cb );
- }
- function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = null ) {
- global $wp_object_cache;
- return $wp_object_cache->getDelayedByKey( $server_key, $keys, $groups, $with_cas, $value_cb );
- }
- function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
- global $wp_object_cache;
- if ( func_num_args() > 2 ) {
- return $wp_object_cache->getMulti( $keys, $groups, '', $cas_tokens, $flags );
- } else {
- return $wp_object_cache->getMulti( $keys, $groups );
- }
- }
- function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
- global $wp_object_cache;
- if ( func_num_args() > 3 ) {
- return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups, $cas_tokens, $flags );
- } else {
- return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups );
- }
- }
- function wp_cache_get_option( $option ) {
- global $wp_object_cache;
- return $wp_object_cache->getOption( $option );
- }
- function wp_cache_get_result_code() {
- global $wp_object_cache;
- return $wp_object_cache->getResultCode();
- }
- function wp_cache_get_result_message() {
- global $wp_object_cache;
- return $wp_object_cache->getResultMessage();
- }
- function wp_cache_get_server_by_key( $server_key ) {
- global $wp_object_cache;
- return $wp_object_cache->getServerByKey( $server_key );
- }
- function wp_cache_get_server_list() {
- global $wp_object_cache;
- return $wp_object_cache->getServerList();
- }
- function wp_cache_get_stats() {
- global $wp_object_cache;
- return $wp_object_cache->getStats();
- }
- function wp_cache_get_version() {
- global $wp_object_cache;
- return $wp_object_cache->getVersion();
- }
- function wp_cache_increment( $key, $offset = 1, $group = '' ) {
- global $wp_object_cache;
- return $wp_object_cache->increment( $key, $offset, $group );
- }
- function wp_cache_incr( $key, $offset = 1, $group = '' ) {
- return wp_cache_increment( $key, $offset, $group );
- }
- function wp_cache_prepend( $key, $value, $group = '' ) {
- global $wp_object_cache;
- return $wp_object_cache->prepend( $key, $value, $group );
- }
- function wp_cache_prepend_by_key( $server_key, $key, $value, $group = '' ) {
- global $wp_object_cache;
- return $wp_object_cache->prependByKey( $server_key, $key, $value, $group );
- }
- function wp_cache_replace( $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->replace( $key, $value, $group, $expiration );
- }
- function wp_cache_replace_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->replaceByKey( $server_key, $key, $value, $group, $expiration );
- }
- function wp_cache_set( $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->set( $key, $value, $group, $expiration );
- }
- function wp_cache_set_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->setByKey( $server_key, $key, $value, $group, $expiration );
- }
- function wp_cache_set_multi( $items, $groups = '', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->setMulti( $items, $groups, $expiration );
- }
- function wp_cache_set_multi_by_key( $server_key, $items, $groups = 'default', $expiration = 0 ) {
- global $wp_object_cache;
- return $wp_object_cache->setMultiByKey( $server_key, $items, $groups, $expiration );
- }
- function wp_cache_set_option( $option, $value ) {
- global $wp_object_cache;
- return $wp_object_cache->setOption( $option, $value );
- }
- function wp_cache_switch_to_blog( $blog_id ) {
- global $wp_object_cache;
- return $wp_object_cache->switch_to_blog( $blog_id );
- }
- function wp_cache_init() {
- global $wp_object_cache;
- $wp_object_cache = new WP_Object_Cache();
- }
- function wp_cache_add_global_groups( $groups ) {
- global $wp_object_cache;
- $wp_object_cache->add_global_groups( $groups );
- }
- function wp_cache_add_non_persistent_groups( $groups ) {
- global $wp_object_cache;
- $wp_object_cache->add_non_persistent_groups( $groups );
- }
- class WP_Object_Cache {
-
- public $m;
-
- public $servers;
-
- public $cache = array();
-
- public $global_groups = array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss' );
-
- public $no_mc_groups = array( 'comment', 'counts' );
-
- public $global_prefix = '';
-
- public $blog_prefix = '';
-
- public function __construct( $persistent_id = null ) {
- global $memcached_servers, $blog_id, $table_prefix;
- if ( is_null( $persistent_id ) || ! is_string( $persistent_id ) ) {
- $this->m = new Memcached();
- } else {
- $this->m = new Memcached( $persistent_id );
- }
- if ( isset( $memcached_servers ) ) {
- $this->servers = $memcached_servers;
- } else {
- $this->servers = array( array( 'memcached', 11211 ) );
- }
- $this->addServers( $this->servers );
-
- if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {
- define( 'WP_CACHE_KEY_SALT', '' );
- }
-
- if ( function_exists( 'is_multisite' ) ) {
- $this->global_prefix = ( is_multisite() || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) ? '' : $table_prefix;
- $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
- }
-
- $this->thirty_days = 60 * 60 * 24 * 30;
- $this->now = time();
- }
-
- public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
-
- if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) {
- return false;
- }
- $derived_key = $this->buildKey( $key, $group );
- $expiration = $this->sanitize_expiration( $expiration );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
-
- if ( isset( $this->cache[ $derived_key ] ) ) {
- return false;
- }
- $this->add_to_internal_cache( $derived_key, $value );
- return true;
- }
-
- if ( $by_key ) {
- $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration );
- } else {
- $result = $this->m->add( $derived_key, $value, $expiration );
- }
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->add_to_internal_cache( $derived_key, $value );
- }
- return $result;
- }
-
- public function addByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
- return $this->add( $key, $value, $group, $expiration, $server_key, true );
- }
-
- public function addServer( $host, $port, $weight = 0 ) {
- $host = is_string( $host ) ? $host : '127.0.0.1';
- $port = is_numeric( $port ) && $port > 0 ? $port : 11211;
- $weight = is_numeric( $weight ) && $weight > 0 ? $weight : 1;
- return $this->m->addServer( $host, $port, $weight );
- }
-
- public function addServers( $servers ) {
- if ( ! is_object( $this->m ) ) {
- return false;
- }
- return $this->m->addServers( $servers );
- }
-
- public function append( $key, $value, $group = 'default', $server_key = '', $by_key = false ) {
- if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) {
- return false;
- }
- $derived_key = $this->buildKey( $key, $group );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
- if ( ! isset( $this->cache[ $derived_key ] ) ) {
- return false;
- }
- $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' );
- $this->add_to_internal_cache( $derived_key, $combined );
- return true;
- }
-
- if ( $by_key ) {
- $result = $this->m->appendByKey( $server_key, $derived_key, $value );
- } else {
- $result = $this->m->append( $derived_key, $value );
- }
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' );
- $this->add_to_internal_cache( $derived_key, $combined );
- }
- return $result;
- }
-
- public function appendByKey( $server_key, $key, $value, $group = 'default' ) {
- return $this->append( $key, $value, $group, $server_key, true );
- }
-
- public function cas( $cas_token, $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
- $derived_key = $this->buildKey( $key, $group );
- $expiration = $this->sanitize_expiration( $expiration );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
- $this->add_to_internal_cache( $derived_key, $value );
- return true;
- }
-
- if ( $by_key ) {
- $result = $this->m->casByKey( $cas_token, $server_key, $derived_key, $value, $expiration );
- } else {
- $result = $this->m->cas( $cas_token, $derived_key, $value, $expiration );
- }
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->add_to_internal_cache( $derived_key, $value );
- }
- return $result;
- }
-
- public function casByKey( $cas_token, $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
- return $this->cas( $cas_token, $key, $value, $group, $expiration, $server_key, true );
- }
-
- public function decrement( $key, $offset = 1, $group = 'default' ) {
- $derived_key = $this->buildKey( $key, $group );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
-
- if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) {
-
- if ( is_numeric( $this->cache[ $derived_key ] ) ) {
- $this->cache[ $derived_key ] -= (int) $offset;
- } else {
- $this->cache[ $derived_key ] = 0;
- }
-
- if ( $this->cache[ $derived_key ] < 0 ) {
- $this->cache[ $derived_key ] = 0;
- }
- return $this->cache[ $derived_key ];
- } else {
- return false;
- }
- }
- $result = $this->m->decrement( $derived_key, $offset );
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->add_to_internal_cache( $derived_key, $result );
- }
- return $result;
- }
-
- public function decr( $key, $offset = 1, $group = 'default' ) {
- return $this->decrement( $key, $offset, $group );
- }
-
- public function delete( $key, $group = 'default', $time = 0, $server_key = '', $by_key = false ) {
- $derived_key = $this->buildKey( $key, $group );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
- if ( isset( $this->cache[ $derived_key ] ) ) {
- unset( $this->cache[ $derived_key ] );
- }
- return true;
- }
- if ( $by_key ) {
- $result = $this->m->deleteByKey( $server_key, $derived_key, $time );
- } else {
- $result = $this->m->delete( $derived_key, $time );
- }
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- unset( $this->cache[ $derived_key ] );
- }
- return $result;
- }
-
- public function deleteByKey( $server_key, $key, $group = 'default', $time = 0 ) {
- return $this->delete( $key, $group, $time, $server_key, true );
- }
-
- public function fetch() {
- return $this->m->fetch();
- }
-
- public function fetchAll() {
- return $this->m->fetchAll();
- }
-
- public function flush( $delay = 0 ) {
- $result = $this->m->flush( $delay );
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->cache = array();
- }
- return $result;
- }
-
- public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $by_key = false, $cache_cb = null, &$cas_token = null ) {
- $derived_key = $this->buildKey( $key, $group );
-
- $found = false;
-
- if ( func_num_args() > 6 && ! in_array( $group, $this->no_mc_groups, true ) ) {
- if ( $by_key ) {
- $value = $this->m->getByKey( $server_key, $derived_key, $cache_cb, $cas_token );
- } else {
- $value = $this->m->get( $derived_key, $cache_cb, $cas_token );
- }
- } else {
- if ( isset( $this->cache[ $derived_key ] ) ) {
- $found = true;
- return is_object( $this->cache[ $derived_key ] ) ? clone $this->cache[ $derived_key ] : $this->cache[ $derived_key ];
- } elseif ( in_array( $group, $this->no_mc_groups, true ) ) {
- return false;
- } else {
- if ( $by_key ) {
- $value = $this->m->getByKey( $server_key, $derived_key );
- } else {
- $value = $this->m->get( $derived_key );
- }
- }
- }
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->add_to_internal_cache( $derived_key, $value );
- $found = true;
- }
- return is_object( $value ) ? clone $value : $value;
- }
-
- public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
-
- if ( func_num_args() > 5 ) {
- return $this->get( $key, $group, $force, $found, $server_key, true, $cache_cb, $cas_token );
- } else {
- return $this->get( $key, $group, $force, $found, $server_key, true );
- }
- }
-
- public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = null ) {
- $derived_keys = $this->buildKeys( $keys, $groups );
- return $this->m->getDelayed( $derived_keys, $with_cas, $value_cb );
- }
-
- public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = null ) {
- $derived_keys = $this->buildKeys( $keys, $groups );
- return $this->m->getDelayedByKey( $server_key, $derived_keys, $with_cas, $value_cb );
- }
-
- public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = null, $flags = null ) {
- $derived_keys = $this->buildKeys( $keys, $groups );
-
- if ( func_num_args() > 3 && ! $this->contains_no_mc_group( $groups ) ) {
- if ( ! empty( $server_key ) ) {
- $values = $this->m->getMultiByKey( $server_key, $derived_keys, $cas_tokens, $flags );
- } else {
- $values = $this->m->getMulti( $derived_keys, $cas_tokens, $flags );
- }
- } else {
- $values = array();
- $need_to_get = array();
-
- foreach ( $derived_keys as $key ) {
- if ( isset( $this->cache[ $key ] ) ) {
- $values[ $key ] = $this->cache[ $key ];
- } else {
- $need_to_get[ $key ] = $key;
- }
- }
-
- if ( ! empty( $need_to_get ) ) {
- if ( ! empty( $server_key ) ) {
- $result = $this->m->getMultiByKey( $server_key, array_keys( $need_to_get ) );
- } else {
- $result = $this->m->getMulti( array_keys( $need_to_get ) );
- }
- }
-
- if ( isset( $result ) && Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $values = array_merge( $values, $result );
- }
-
- if ( ! empty( $need_to_get ) && Memcached::GET_PRESERVE_ORDER === $flags ) {
- $ordered_values = array();
- foreach ( $derived_keys as $key ) {
- if ( isset( $values[ $key ] ) ) {
- $ordered_values[ $key ] = $values[ $key ];
- }
- }
- $values = $ordered_values;
- unset( $ordered_values );
- }
- }
-
- $this->cache = array_merge( $this->cache, $values );
- return $values;
- }
-
- public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = null, $flags = null ) {
-
- if ( func_num_args() > 3 ) {
- return $this->getMulti( $keys, $groups, $server_key, $cas_tokens, $flags );
- } else {
- return $this->getMulti( $keys, $groups, $server_key );
- }
- }
-
- public function getOption( $option ) {
- return $this->m->getOption( $option );
- }
-
- public function getResultCode() {
- return $this->m->getResultCode();
- }
-
- public function getResultMessage() {
- return $this->m->getResultMessage();
- }
-
- public function getServerByKey( $server_key ) {
- return $this->m->getServerByKey( $server_key );
- }
-
- public function getServerList() {
- return $this->m->getServerList();
- }
-
- public function getStats() {
- return $this->m->getStats();
- }
-
- public function getVersion() {
- return $this->m->getVersion();
- }
-
- public function increment( $key, $offset = 1, $group = 'default' ) {
- $derived_key = $this->buildKey( $key, $group );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
-
- if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) {
-
- if ( is_numeric( $this->cache[ $derived_key ] ) ) {
- $this->cache[ $derived_key ] += (int) $offset;
- } else {
- $this->cache[ $derived_key ] = 0;
- }
-
- if ( $this->cache[ $derived_key ] < 0 ) {
- $this->cache[ $derived_key ] = 0;
- }
- return $this->cache[ $derived_key ];
- } else {
- return false;
- }
- }
- $result = $this->m->increment( $derived_key, $offset );
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->add_to_internal_cache( $derived_key, $result );
- }
- return $result;
- }
-
- public function incr( $key, $offset = 1, $group = 'default' ) {
- return $this->increment( $key, $offset, $group );
- }
-
- public function prepend( $key, $value, $group = 'default', $server_key = '', $by_key = false ) {
- if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) {
- return false;
- }
- $derived_key = $this->buildKey( $key, $group );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
- if ( ! isset( $this->cache[ $derived_key ] ) ) {
- return false;
- }
- $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' );
- $this->add_to_internal_cache( $derived_key, $combined );
- return true;
- }
-
- if ( $by_key ) {
- $result = $this->m->prependByKey( $server_key, $derived_key, $value );
- } else {
- $result = $this->m->prepend( $derived_key, $value );
- }
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' );
- $this->add_to_internal_cache( $derived_key, $combined );
- }
- return $result;
- }
-
- public function prependByKey( $server_key, $key, $value, $group = 'default' ) {
- return $this->prepend( $key, $value, $group, $server_key, true );
- }
-
- public function replace( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
- $derived_key = $this->buildKey( $key, $group );
- $expiration = $this->sanitize_expiration( $expiration );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
-
- if ( ! isset( $this->cache[ $derived_key ] ) ) {
- return false;
- }
- $this->cache[ $derived_key ] = $value;
- return true;
- }
-
- if ( $by_key ) {
- $result = $this->m->replaceByKey( $server_key, $derived_key, $value, $expiration );
- } else {
- $result = $this->m->replace( $derived_key, $value, $expiration );
- }
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->add_to_internal_cache( $derived_key, $value );
- }
- return $result;
- }
-
- public function replaceByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
- return $this->replace( $key, $value, $group, $expiration, $server_key, true );
- }
-
- public function set( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
- $derived_key = $this->buildKey( $key, $group );
- $expiration = $this->sanitize_expiration( $expiration );
-
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
- $this->add_to_internal_cache( $derived_key, $value );
- return true;
- }
-
- if ( $by_key ) {
- $result = $this->m->setByKey( $server_key, $derived_key, $value, $expiration );
- } else {
- $result = $this->m->set( $derived_key, $value, $expiration );
- }
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->add_to_internal_cache( $derived_key, $value );
- }
- return $result;
- }
-
- public function setByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
- return $this->set( $key, $value, $group, $expiration, $server_key, true );
- }
-
- public function setMulti( $items, $groups = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
-
- $derived_keys = $this->buildKeys( array_keys( $items ), $groups );
- $expiration = $this->sanitize_expiration( $expiration );
- $derived_items = array_combine( $derived_keys, $items );
-
- foreach ( $derived_items as $derived_key => $value ) {
-
- $key_pieces = explode( ':', $derived_key );
-
- if ( in_array( $key_pieces[1], $this->no_mc_groups, true ) ) {
- $this->add_to_internal_cache( $derived_key, $value );
- unset( $derived_items[ $derived_key ] );
- }
- }
-
- if ( $by_key ) {
- $result = $this->m->setMultiByKey( $server_key, $derived_items, $expiration );
- } else {
- $result = $this->m->setMulti( $derived_items, $expiration );
- }
-
- if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
- $this->cache = array_merge( $this->cache, $derived_items );
- }
- return $result;
- }
-
- public function setMultiByKey( $server_key, $items, $groups = 'default', $expiration = 0 ) {
- return $this->setMulti( $items, $groups, $expiration, $server_key, true );
- }
-
- public function setOption( $option, $value ) {
- return $this->m->setOption( $option, $value );
- }
-
- public function buildKey( $key, $group = 'default' ) {
- if ( empty( $group ) ) {
- $group = 'default';
- }
- if ( false !== array_search( $group, $this->global_groups, true ) ) {
- $prefix = $this->global_prefix;
- } else {
- $prefix = $this->blog_prefix;
- }
- return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key" );
- }
-
- public function buildKeys( $keys, $groups = 'default' ) {
- $derived_keys = array();
-
- if ( ! is_array( $groups ) ) {
- $groups = (array) $groups;
- }
- if ( ! is_array( $keys ) ) {
- $keys = (array) $keys;
- }
-
- if ( count( $keys ) === count( $groups ) ) {
- for ( $i = 0; $i < count( $keys ); $i++ ) {
- $derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] );
- }
-
-
- } elseif ( count( $keys ) > count( $groups ) ) {
- for ( $i = 0; $i < count( $keys ); $i++ ) {
- if ( isset( $groups[ $i ] ) ) {
- $derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] );
- } elseif ( count( $groups ) === 1 ) {
- $derived_keys[] = $this->buildKey( $keys[ $i ], $groups[0] );
- } else {
- $derived_keys[] = $this->buildKey( $keys[ $i ], 'default' );
- }
- }
- }
- return $derived_keys;
- }
-
- public function sanitize_expiration( $expiration ) {
- if ( $expiration > $this->thirty_days && $expiration <= $this->now ) {
- $expiration = $expiration + $this->now;
- }
- return $expiration;
- }
-
- public function combine_values( $original, $pended, $direction ) {
- $type = gettype( $original );
-
- if ( 'pre' === $direction ) {
- $combined = $pended . $original;
- } else {
- $combined = $original . $pended;
- }
-
- settype( $combined, $type );
- return $combined;
- }
-
- public function add_to_internal_cache( $derived_key, $value ) {
- if ( is_object( $value ) ) {
- $value = clone $value;
- }
- $this->cache[ $derived_key ] = $value;
- }
-
- public function contains_no_mc_group( $groups ) {
- if ( is_scalar( $groups ) ) {
- return in_array( $groups, $this->no_mc_groups, true );
- }
- if ( ! is_array( $groups ) ) {
- return false;
- }
- foreach ( $groups as $group ) {
- if ( in_array( $group, $this->no_mc_groups, true ) ) {
- return true;
- }
- }
- return false;
- }
-
- public function add_global_groups( $groups ) {
- if ( ! is_array( $groups ) ) {
- $groups = (array) $groups;
- }
- $this->global_groups = array_merge( $this->global_groups, $groups );
- $this->global_groups = array_unique( $this->global_groups );
- }
-
- public function add_non_persistent_groups( $groups ) {
- if ( ! is_array( $groups ) ) {
- $groups = (array) $groups;
- }
- $this->no_mc_groups = array_merge( $this->no_mc_groups, $groups );
- $this->no_mc_groups = array_unique( $this->no_mc_groups );
- }
-
- public function get_from_runtime_cache( $key, $group ) {
- $derived_key = $this->buildKey( $key, $group );
- if ( isset( $this->cache[ $derived_key ] ) ) {
- return $this->cache[ $derived_key ];
- }
- return false;
- }
-
- public function switch_to_blog( $blog_id ) {
- global $table_prefix;
- $blog_id = (int) $blog_id;
- $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
- }
- }
|