Advertisement
Guest User

Untitled

a guest
Aug 24th, 2014
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 85.29 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Adds a value to cache.
  4.  *
  5.  * If the specified key already exists, the value is not stored and the function
  6.  * returns false.
  7.  *
  8.  * @link http://www.php.net/manual/en/memcached.add.php
  9.  *
  10.  * @param string    $key        The key under which to store the value.
  11.  * @param mixed     $value      The value to store.
  12.  * @param string    $group      The group value appended to the $key.
  13.  * @param int       $expiration The expiration time, defaults to 0.
  14.  * @return bool                 Returns TRUE on success or FALSE on failure.
  15.  */
  16. function wp_cache_add( $key, $value, $group = '', $expiration = 0 ) {
  17.     global $wp_object_cache;
  18.     return $wp_object_cache->add( $key, $value, $group, $expiration );
  19. }
  20.  
  21. /**
  22.  * Adds a value to cache on a specific server.
  23.  *
  24.  * Using a server_key value, the object can be stored on a specified server as opposed
  25.  * to a random server in the stack. Note that this method will add the key/value to the
  26.  * _cache object as part of the runtime cache. It will add it to an array for the
  27.  * specified server_key.
  28.  *
  29.  * @link http://www.php.net/manual/en/memcached.addbykey.php
  30.  *
  31.  * @param string    $server_key     The key identifying the server to store the value on.
  32.  * @param string    $key            The key under which to store the value.
  33.  * @param mixed     $value          The value to store.
  34.  * @param string    $group          The group value appended to the $key.
  35.  * @param int       $expiration     The expiration time, defaults to 0.
  36.  * @return bool                     Returns TRUE on success or FALSE on failure.
  37.  */
  38. function wp_cache_add_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
  39.     global $wp_object_cache;
  40.     return $wp_object_cache->addByKey( $server_key, $key, $value, $group, $expiration );
  41. }
  42.  
  43. /**
  44.  * Add a single server to the list of Memcached servers.
  45.  *
  46.  * @link http://www.php.net/manual/en/memcached.addserver.php
  47.  *
  48.  * @param string        $host   The hostname of the memcache server.
  49.  * @param int           $port   The port on which memcache is running.
  50.  * @param int           $weight The weight of the server relative to the total weight of all the servers in the pool.
  51.  * @return bool                 Returns TRUE on success or FALSE on failure.
  52.  */
  53. function wp_cache_add_server( $host, $port, $weight = 0 ) {
  54.     global $wp_object_cache;
  55.     return $wp_object_cache->addServer( $host, $port, $weight );
  56. }
  57.  
  58. /**
  59.  * Adds an array of servers to the pool.
  60.  *
  61.  * Each individual server in the array must include a domain and port, with an optional
  62.  * weight value: $servers = array( array( '127.0.0.1', 11211, 0 ) );
  63.  *
  64.  * @link http://www.php.net/manual/en/memcached.addservers.php
  65.  *
  66.  * @param array     $servers    Array of server to register.
  67.  * @return bool                 True on success; false on failure.
  68.  */
  69. function wp_cache_add_servers( $servers ) {
  70.     global $wp_object_cache;
  71.     return $wp_object_cache->addServers( $servers );
  72. }
  73.  
  74. /**
  75.  * Append data to an existing item.
  76.  *
  77.  * This method should throw an error if it is used with compressed data. This
  78.  * is an expected behavior. Memcached casts the value to be appended to the initial value to the
  79.  * type of the initial value. Be careful as this leads to unexpected behavior at times. Due to
  80.  * how memcached treats types, the behavior has been mimicked in the internal cache to produce
  81.  * similar results and improve consistency. It is recommend that appends only occur with data of
  82.  * the same type.
  83.  *
  84.  * @link http://www.php.net/manual/en/memcached.append.php
  85.  *
  86.  * @param string    $key    The key under which to store the value.
  87.  * @param mixed     $value  Must be string as appending mixed values is not well-defined
  88.  * @param string    $group  The group value appended to the $key.
  89.  * @return bool             Returns TRUE on success or FALSE on failure.
  90.  */
  91. function wp_cache_append( $key, $value, $group = '' ) {
  92.     global $wp_object_cache;
  93.     return $wp_object_cache->append( $key, $value, $group );
  94. }
  95.  
  96. /**
  97.  * Append data to an existing item by server key.
  98.  *
  99.  * This method should throw an error if it is used with compressed data. This
  100.  * is an expected behavior. Memcached casts the value to be appended to the initial value to the
  101.  * type of the initial value. Be careful as this leads to unexpected behavior at times. Due to
  102.  * how memcached treats types, the behavior has been mimicked in the internal cache to produce
  103.  * similar results and improve consistency. It is recommend that appends only occur with data of
  104.  * the same type.
  105.  *
  106.  * @link http://www.php.net/manual/en/memcached.appendbykey.php
  107.  *
  108.  * @param string    $server_key     The key identifying the server to store the value on.
  109.  * @param string    $key            The key under which to store the value.
  110.  * @param mixed     $value          Must be string as appending mixed values is not well-defined
  111.  * @param string    $group          The group value appended to the $key.
  112.  * @return bool                     Returns TRUE on success or FALSE on failure.
  113.  */
  114. function wp_cache_append_by_key( $server_key, $key, $value, $group = '' ) {
  115.     global $wp_object_cache;
  116.     return $wp_object_cache->appendByKey( $server_key, $key, $value, $group );
  117. }
  118.  
  119. /**
  120.  * Performs a "check and set" to store data.
  121.  *
  122.  * The set will be successful only if the no other request has updated the value since it was fetched by
  123.  * this request.
  124.  *
  125.  * @link http://www.php.net/manual/en/memcached.cas.php
  126.  *
  127.  * @param float     $cas_token  Unique value associated with the existing item. Generated by memcached.
  128.  * @param string    $key        The key under which to store the value.
  129.  * @param mixed     $value      The value to store.
  130.  * @param string    $group      The group value appended to the $key.
  131.  * @param int       $expiration The expiration time, defaults to 0.
  132.  * @return bool                 Returns TRUE on success or FALSE on failure.
  133.  */
  134. function wp_cache_cas( $cas_token, $key, $value, $group = '', $expiration = 0 ) {
  135.     global $wp_object_cache;
  136.     return $wp_object_cache->cas( $cas_token, $key, $value, $group, $expiration );
  137. }
  138.  
  139. /**
  140.  * Performs a "check and set" to store data with a server key.
  141.  *
  142.  * The set will be successful only if the no other request has updated the value since it was fetched by
  143.  * this request.
  144.  *
  145.  * @link http://www.php.net/manual/en/memcached.casbykey.php
  146.  *
  147.  * @param string    $server_key The key identifying the server to store the value on.
  148.  * @param float     $cas_token  Unique value associated with the existing item. Generated by memcached.
  149.  * @param string    $key        The key under which to store the value.
  150.  * @param mixed     $value      The value to store.
  151.  * @param string    $group      The group value appended to the $key.
  152.  * @param int       $expiration The expiration time, defaults to 0.
  153.  * @return bool                 Returns TRUE on success or FALSE on failure.
  154.  */
  155. function wp_cache_cas_by_key( $cas_token, $server_key, $key, $value, $group = '', $expiration = 0 ) {
  156.     global $wp_object_cache;
  157.     return $wp_object_cache->casByKey( $cas_token, $server_key, $key, $value, $group, $expiration );
  158. }
  159.  
  160. /**
  161.  * Closes the cache.
  162.  *
  163.  * This function has ceased to do anything since WordPress 2.5. The
  164.  * functionality was removed along with the rest of the persistent cache. This
  165.  * does not mean that plugins can't implement this function when they need to
  166.  * make sure that the cache is cleaned up after WordPress no longer needs it.
  167.  *
  168.  * @since 2.0.0
  169.  *
  170.  * @return  bool    Always returns True
  171.  */
  172. function wp_cache_close() {
  173.     return true;
  174. }
  175.  
  176. /**
  177.  * Decrement a numeric item's value.
  178.  *
  179.  * @link http://www.php.net/manual/en/memcached.decrement.php
  180.  *
  181.  * @param string    $key    The key under which to store the value.
  182.  * @param int       $offset The amount by which to decrement the item's value.
  183.  * @param string    $group  The group value appended to the $key.
  184.  * @return int|bool         Returns item's new value on success or FALSE on failure.
  185.  */
  186. function wp_cache_decrement( $key, $offset = 1, $group = '' ) {
  187.     global $wp_object_cache;
  188.     return $wp_object_cache->decrement( $key, $offset, $group );
  189. }
  190.  
  191. /**
  192.  * Decrement a numeric item's value.
  193.  *
  194.  * Same as wp_cache_decrement. Original WordPress caching backends use wp_cache_decr. I
  195.  * want both spellings to work.
  196.  *
  197.  * @link http://www.php.net/manual/en/memcached.decrement.php
  198.  *
  199.  * @param string    $key    The key under which to store the value.
  200.  * @param int       $offset The amount by which to decrement the item's value.
  201.  * @param string    $group  The group value appended to the $key.
  202.  * @return int|bool         Returns item's new value on success or FALSE on failure.
  203.  */
  204. function wp_cache_decr( $key, $offset = 1, $group = '' ) {
  205.     return wp_cache_decrement( $key, $offset, $group );
  206. }
  207.  
  208. /**
  209.  * Remove the item from the cache.
  210.  *
  211.  * Remove an item from memcached with identified by $key after $time seconds. The
  212.  * $time parameter allows an object to be queued for deletion without immediately
  213.  * deleting. Between the time that it is queued and the time it's deleted, add,
  214.  * replace, and get will fail, but set will succeed.
  215.  *
  216.  * @link http://www.php.net/manual/en/memcached.delete.php
  217.  *
  218.  * @param string    $key    The key under which to store the value.
  219.  * @param string    $group  The group value appended to the $key.
  220.  * @param int       $time   The amount of time the server will wait to delete the item in seconds.
  221.  * @return bool             Returns TRUE on success or FALSE on failure.
  222.  */
  223. function wp_cache_delete( $key, $group = '', $time = 0 ) {
  224.     global $wp_object_cache;
  225.     return $wp_object_cache->delete( $key, $group, $time );
  226. }
  227.  
  228. /**
  229.  * Remove the item from the cache by server key.
  230.  *
  231.  * Remove an item from memcached with identified by $key after $time seconds. The
  232.  * $time parameter allows an object to be queued for deletion without immediately
  233.  * deleting. Between the time that it is queued and the time it's deleted, add,
  234.  * replace, and get will fail, but set will succeed.
  235.  *
  236.  * @link http://www.php.net/manual/en/memcached.deletebykey.php
  237.  *
  238.  * @param string        $server_key The key identifying the server to store the value on.
  239.  * @param string        $key        The key under which to store the value.
  240.  * @param string        $group      The group value appended to the $key.
  241.  * @param int           $time       The amount of time the server will wait to delete the item in seconds.
  242.  * @return bool                     Returns TRUE on success or FALSE on failure.
  243.  */
  244. function wp_cache_delete_by_key( $server_key, $key, $group = '', $time = 0 ) {
  245.     global $wp_object_cache;
  246.     return $wp_object_cache->deleteByKey( $server_key, $key, $group, $time );
  247. }
  248.  
  249. /**
  250.  * Fetch the next result.
  251.  *
  252.  * @link http://www.php.net/manual/en/memcached.fetch.php
  253.  *
  254.  * @return  array|bool   Returns the next result or FALSE otherwise.
  255.  */
  256. function wp_cache_fetch() {
  257.     global $wp_object_cache;
  258.     return $wp_object_cache->fetch();
  259. }
  260.  
  261. /**
  262.  * Fetch all remaining results from the last request.
  263.  *
  264.  * @link http://www.php.net/manual/en/memcached.fetchall.php
  265.  *
  266.  * @return  array|bool  Returns the results or FALSE on failure.
  267.  */
  268. function wp_cache_fetch_all() {
  269.     global $wp_object_cache;
  270.     return $wp_object_cache->fetchAll();
  271. }
  272.  
  273. /**
  274.  * Invalidate all items in the cache.
  275.  *
  276.  * @link http://www.php.net/manual/en/memcached.flush.php
  277.  *
  278.  * @param int       $delay  Number of seconds to wait before invalidating the items.
  279.  * @return bool             Returns TRUE on success or FALSE on failure.
  280.  */
  281. function wp_cache_flush( $delay = 0 ) {
  282.     global $wp_object_cache;
  283.     return $wp_object_cache->flush( $delay );
  284. }
  285.  
  286. /**
  287.  * Retrieve object from cache.
  288.  *
  289.  * Gets an object from cache based on $key and $group. In order to fully support the $cache_cb and $cas_token
  290.  * parameters, the runtime cache is ignored by this function if either of those values are set. If either of
  291.  * those values are set, the request is made directly to the memcached server for proper handling of the
  292.  * callback and/or token.
  293.  *
  294.  * Note that the $deprecated and $found args are only here for compatibility with the native wp_cache_get function.
  295.  *
  296.  * @link http://www.php.net/manual/en/memcached.get.php
  297.  *
  298.  * @param string        $key        The key under which to store the value.
  299.  * @param string        $group      The group value appended to the $key.
  300.  * @param bool          $force      Whether or not to force a cache invalidation.
  301.  * @param null|bool     $found      Variable passed by reference to determine if the value was found or not.
  302.  * @param null|string   $cache_cb   Read-through caching callback.
  303.  * @param null|float    $cas_token  The variable to store the CAS token in.
  304.  * @return bool|mixed               Cached object value.
  305.  */
  306. function wp_cache_get( $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
  307.     global $wp_object_cache;
  308.  
  309.     if ( func_num_args() > 4 )
  310.         return $wp_object_cache->get( $key, $group, $force, $found, '', false, $cache_cb, $cas_token );
  311.     else
  312.         return $wp_object_cache->get( $key, $group, $force, $found );
  313. }
  314.  
  315. /**
  316.  * Retrieve object from cache from specified server.
  317.  *
  318.  * Gets an object from cache based on $key, $group and $server_key. In order to fully support the $cache_cb and $cas_token
  319.  * parameters, the runtime cache is ignored by this function if either of those values are set. If either of
  320.  * those values are set, the request is made directly to the memcached server for proper handling of the
  321.  * callback and/or token.
  322.  *
  323.  * @link http://www.php.net/manual/en/memcached.getbykey.php
  324.  *
  325.  * @param string        $server_key The key identifying the server to store the value on.
  326.  * @param string        $key        The key under which to store the value.
  327.  * @param string        $group      The group value appended to the $key.
  328.  * @param bool          $force      Whether or not to force a cache invalidation.
  329.  * @param null|bool     $found      Variable passed by reference to determine if the value was found or not.
  330.  * @param null|string   $cache_cb   Read-through caching callback.
  331.  * @param null|float    $cas_token  The variable to store the CAS token in.
  332.  * @return bool|mixed               Cached object value.
  333.  */
  334. function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$found = null, $cache_cb = NULL, &$cas_token = NULL ) {
  335.     global $wp_object_cache;
  336.  
  337.     if ( func_num_args() > 5 )
  338.         return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found, $cache_cb, $cas_token );
  339.     else
  340.         return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found );
  341. }
  342.  
  343. /**
  344.  * Request multiple keys without blocking.
  345.  *
  346.  * @link http://www.php.net/manual/en/memcached.getdelayed.php
  347.  *
  348.  * @param string|array  $keys       Array or string of key(s) to request.
  349.  * @param string|array  $groups     Array or string of group(s) for the key(s). See buildKeys for more on how these are handled.
  350.  * @param bool          $with_cas   Whether to request CAS token values also.
  351.  * @param null          $value_cb   The result callback or NULL.
  352.  * @return bool                     Returns TRUE on success or FALSE on failure.
  353.  */
  354. function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = NULL ) {
  355.     global $wp_object_cache;
  356.     return $wp_object_cache->getDelayed( $keys, $groups, $with_cas, $value_cb );
  357. }
  358.  
  359. /**
  360.  * Request multiple keys without blocking from a specified server.
  361.  *
  362.  * @link http://www.php.net/manual/en/memcached.getdelayed.php
  363.  *
  364.  * @param string        $server_key The key identifying the server to store the value on.
  365.  * @param string|array  $keys       Array or string of key(s) to request.
  366.  * @param string|array  $groups     Array or string of group(s) for the key(s). See buildKeys for more on how these are handled.
  367.  * @param bool          $with_cas   Whether to request CAS token values also.
  368.  * @param null          $value_cb   The result callback or NULL.
  369.  * @return bool                     Returns TRUE on success or FALSE on failure.
  370.  */
  371. function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = NULL ) {
  372.     global $wp_object_cache;
  373.     return $wp_object_cache->getDelayedByKey( $server_key, $keys, $groups, $with_cas, $value_cb );
  374. }
  375.  
  376. /**
  377.  * Gets multiple values from memcached in one request.
  378.  *
  379.  * See the buildKeys method definition to understand the $keys/$groups parameters.
  380.  *
  381.  * @link http://www.php.net/manual/en/memcached.getmulti.php
  382.  *
  383.  * @param array         $keys       Array of keys to retrieve.
  384.  * @param string|array  $groups     If string, used for all keys. If arrays, corresponds with the $keys array.
  385.  * @param null|array    $cas_tokens The variable to store the CAS tokens for the found items.
  386.  * @param int           $flags      The flags for the get operation.
  387.  * @return bool|array               Returns the array of found items or FALSE on failure.
  388.  */
  389. function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = NULL, $flags = NULL ) {
  390.     global $wp_object_cache;
  391.  
  392.     if ( func_num_args() > 2 )
  393.         return $wp_object_cache->getMulti( $keys, $groups, '', $cas_tokens, $flags );
  394.     else
  395.         return $wp_object_cache->getMulti( $keys, $groups );
  396. }
  397.  
  398. /**
  399.  * Gets multiple values from memcached in one request by specified server key.
  400.  *
  401.  * See the buildKeys method definition to understand the $keys/$groups parameters.
  402.  *
  403.  * @link http://www.php.net/manual/en/memcached.getmultibykey.php
  404.  *
  405.  * @param string        $server_key The key identifying the server to store the value on.
  406.  * @param array         $keys       Array of keys to retrieve.
  407.  * @param string|array  $groups     If string, used for all keys. If arrays, corresponds with the $keys array.
  408.  * @param null|array    $cas_tokens The variable to store the CAS tokens for the found items.
  409.  * @param int           $flags      The flags for the get operation.
  410.  * @return bool|array               Returns the array of found items or FALSE on failure.
  411.  */
  412. function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = NULL, $flags = NULL ) {
  413.     global $wp_object_cache;
  414.  
  415.     if ( func_num_args() > 3 )
  416.         return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups, $cas_tokens, $flags );
  417.     else
  418.         return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups );
  419. }
  420.  
  421. /**
  422.  * Retrieve a Memcached option value.
  423.  *
  424.  * @link http://www.php.net/manual/en/memcached.getoption.php
  425.  *
  426.  * @param int   $option One of the Memcached::OPT_* constants.
  427.  * @return mixed        Returns the value of the requested option, or FALSE on error.
  428.  */
  429. function wp_cache_get_option( $option ) {
  430.     global $wp_object_cache;
  431.     return $wp_object_cache->getOption( $option );
  432. }
  433.  
  434. /**
  435.  * Return the result code of the last option.
  436.  *
  437.  * @link http://www.php.net/manual/en/memcached.getresultcode.php
  438.  *
  439.  * @return int  Result code of the last Memcached operation.
  440.  */
  441. function wp_cache_get_result_code() {
  442.     global $wp_object_cache;
  443.     return $wp_object_cache->getResultCode();
  444. }
  445.  
  446. /**
  447.  * Return the message describing the result of the last operation.
  448.  *
  449.  * @link http://www.php.net/manual/en/memcached.getresultmessage.php
  450.  *
  451.  * @return string   Message describing the result of the last Memcached operation.
  452.  */
  453. function wp_cache_get_result_message() {
  454.     global $wp_object_cache;
  455.     return $wp_object_cache->getResultMessage();
  456. }
  457.  
  458. /**
  459.  * Get server information by key.
  460.  *
  461.  * @link http://www.php.net/manual/en/memcached.getserverbykey.php
  462.  *
  463.  * @param string    $server_key The key identifying the server to store the value on.
  464.  * @return array                Array with host, post, and weight on success, FALSE on failure.
  465.  */
  466. function wp_cache_get_server_by_key( $server_key ) {
  467.     global $wp_object_cache;
  468.     return $wp_object_cache->getServerByKey( $server_key );
  469. }
  470.  
  471. /**
  472.  * Get the list of servers in the pool.
  473.  *
  474.  * @link http://www.php.net/manual/en/memcached.getserverlist.php
  475.  *
  476.  * @return array    The list of all servers in the server pool.
  477.  */
  478. function wp_cache_get_server_list() {
  479.     global $wp_object_cache;
  480.     return $wp_object_cache->getServerList();
  481. }
  482.  
  483. /**
  484.  * Get server pool statistics.
  485.  *
  486.  * @link http://www.php.net/manual/en/memcached.getstats.php
  487.  *
  488.  * @return array    Array of server statistics, one entry per server.
  489.  */
  490. function wp_cache_get_stats() {
  491.     global $wp_object_cache;
  492.     return $wp_object_cache->getStats();
  493. }
  494.  
  495. /**
  496.  * Get server pool memcached version information.
  497.  *
  498.  * @link http://www.php.net/manual/en/memcached.getversion.php
  499.  *
  500.  * @return array    Array of server versions, one entry per server.
  501.  */
  502. function wp_cache_get_version() {
  503.     global $wp_object_cache;
  504.     return $wp_object_cache->getVersion();
  505. }
  506.  
  507. /**
  508.  * Increment a numeric item's value.
  509.  *
  510.  * @link http://www.php.net/manual/en/memcached.increment.php
  511.  *
  512.  * @param string    $key    The key under which to store the value.
  513.  * @param int       $offset The amount by which to increment the item's value.
  514.  * @param string    $group  The group value appended to the $key.
  515.  * @return int|bool         Returns item's new value on success or FALSE on failure.
  516.  */
  517. function wp_cache_increment( $key, $offset = 1, $group = '' ) {
  518.     global $wp_object_cache;
  519.     return $wp_object_cache->increment( $key, $offset, $group );
  520. }
  521.  
  522. /**
  523.  * Increment a numeric item's value.
  524.  *
  525.  * This is the same as wp_cache_increment, but kept for back compatibility. The original
  526.  * WordPress caching backends use wp_cache_incr. I want both to work.
  527.  *
  528.  * @link http://www.php.net/manual/en/memcached.increment.php
  529.  *
  530.  * @param string    $key    The key under which to store the value.
  531.  * @param int       $offset The amount by which to increment the item's value.
  532.  * @param string    $group  The group value appended to the $key.
  533.  * @return int|bool         Returns item's new value on success or FALSE on failure.
  534.  */
  535. function wp_cache_incr( $key, $offset = 1, $group = '' ) {
  536.     return wp_cache_increment( $key, $offset, $group );
  537. }
  538.  
  539. /**
  540.  * Prepend data to an existing item.
  541.  *
  542.  * This method should throw an error if it is used with compressed data. This is an expected behavior.
  543.  * Memcached casts the value to be prepended to the initial value to the type of the initial value. Be
  544.  * careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23 to
  545.  * (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
  546.  * (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
  547.  * mimicked in the internal cache to produce similar results and improve consistency. It is recommend
  548.  * that prepends only occur with data of the same type.
  549.  *
  550.  * @link http://www.php.net/manual/en/memcached.prepend.php
  551.  *
  552.  * @param string    $key    The key under which to store the value.
  553.  * @param string    $value  Must be string as prepending mixed values is not well-defined.
  554.  * @param string    $group  The group value prepended to the $key.
  555.  * @return bool             Returns TRUE on success or FALSE on failure.
  556.  */
  557. function wp_cache_prepend( $key, $value, $group = '' ) {
  558.     global $wp_object_cache;
  559.     return $wp_object_cache->prepend( $key, $value, $group );
  560. }
  561.  
  562. /**
  563.  * Append data to an existing item by server key.
  564.  *
  565.  * This method should throw an error if it is used with compressed data. This is an expected behavior.
  566.  * Memcached casts the value to be prepended to the initial value to the type of the initial value. Be
  567.  * careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23 to
  568.  * (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
  569.  * (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
  570.  * mimicked in the internal cache to produce similar results and improve consistency. It is recommend
  571.  * that prepends only occur with data of the same type.
  572.  *
  573.  * @link http://www.php.net/manual/en/memcached.prependbykey.php
  574.  *
  575.  * @param string    $server_key     The key identifying the server to store the value on.
  576.  * @param string    $key            The key under which to store the value.
  577.  * @param string    $value          Must be string as prepending mixed values is not well-defined.
  578.  * @param string    $group          The group value prepended to the $key.
  579.  * @return bool                     Returns TRUE on success or FALSE on failure.
  580.  */
  581. function wp_cache_prepend_by_key( $server_key, $key, $value, $group = '' ) {
  582.     global $wp_object_cache;
  583.     return $wp_object_cache->prependByKey( $server_key, $key, $value, $group );
  584. }
  585.  
  586. /**
  587.  * Replaces a value in cache.
  588.  *
  589.  * This method is similar to "add"; however, is does not successfully set a value if
  590.  * the object's key is not already set in cache.
  591.  *
  592.  * @link http://www.php.net/manual/en/memcached.replace.php
  593.  *
  594.  * @param string    $key        The key under which to store the value.
  595.  * @param mixed     $value      The value to store.
  596.  * @param string    $group      The group value appended to the $key.
  597.  * @param int       $expiration The expiration time, defaults to 0.
  598.  * @return bool                 Returns TRUE on success or FALSE on failure.
  599.  */
  600. function wp_cache_replace( $key, $value, $group = '', $expiration = 0 ) {
  601.     global $wp_object_cache;
  602.     return $wp_object_cache->replace( $key, $value, $group, $expiration );
  603. }
  604.  
  605. /**
  606.  * Replaces a value in cache on a specific server.
  607.  *
  608.  * This method is similar to "addByKey"; however, is does not successfully set a value if
  609.  * the object's key is not already set in cache.
  610.  *
  611.  * @link http://www.php.net/manual/en/memcached.addbykey.php
  612.  *
  613.  * @param string    $server_key     The key identifying the server to store the value on.
  614.  * @param string    $key            The key under which to store the value.
  615.  * @param mixed     $value          The value to store.
  616.  * @param string    $group          The group value appended to the $key.
  617.  * @param int       $expiration     The expiration time, defaults to 0.
  618.  * @return bool                     Returns TRUE on success or FALSE on failure.
  619.  */
  620. function wp_cache_replace_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
  621.     global $wp_object_cache;
  622.     return $wp_object_cache->replaceByKey( $server_key, $key, $value, $group, $expiration );
  623. }
  624.  
  625. /**
  626.  * Sets a value in cache.
  627.  *
  628.  * The value is set whether or not this key already exists in memcached.
  629.  *
  630.  * @link http://www.php.net/manual/en/memcached.set.php
  631.  *
  632.  * @param string    $key        The key under which to store the value.
  633.  * @param mixed     $value      The value to store.
  634.  * @param string    $group      The group value appended to the $key.
  635.  * @param int       $expiration The expiration time, defaults to 0.
  636.  * @return bool                 Returns TRUE on success or FALSE on failure.
  637.  */
  638. function wp_cache_set( $key, $value, $group = '', $expiration = 0 ) {
  639.     global $wp_object_cache;
  640.     return $wp_object_cache->set( $key, $value, $group, $expiration );
  641. }
  642.  
  643. /**
  644.  * Sets a value in cache.
  645.  *
  646.  * The value is set whether or not this key already exists in memcached.
  647.  *
  648.  * @link http://www.php.net/manual/en/memcached.set.php
  649.  *
  650.  * @param string    $server_key     The key identifying the server to store the value on.
  651.  * @param string    $key            The key under which to store the value.
  652.  * @param mixed     $value          The value to store.
  653.  * @param string    $group          The group value appended to the $key.
  654.  * @param int       $expiration     The expiration time, defaults to 0.
  655.  * @return bool                     Returns TRUE on success or FALSE on failure.
  656.  */
  657. function wp_cache_set_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
  658.     global $wp_object_cache;
  659.     return $wp_object_cache->setByKey( $server_key, $key, $value, $group, $expiration );
  660. }
  661.  
  662. /**
  663.  * Set multiple values to cache at once.
  664.  *
  665.  * By sending an array of $items to this function, all values are saved at once to
  666.  * memcached, reducing the need for multiple requests to memcached. The $items array
  667.  * keys and values are what are stored to memcached. The keys in the $items array
  668.  * are merged with the $groups array/string value via buildKeys to determine the
  669.  * final key for the object.
  670.  *
  671.  * @param array         $items      An array of key/value pairs to store on the server.
  672.  * @param string|array  $groups     Group(s) to merge with key(s) in $items.
  673.  * @param int           $expiration The expiration time, defaults to 0.
  674.  * @return bool                     Returns TRUE on success or FALSE on failure.
  675.  */
  676. function wp_cache_set_multi( $items, $groups = '', $expiration = 0 ) {
  677.     global $wp_object_cache;
  678.     return $wp_object_cache->setMulti( $items, $groups, $expiration );
  679. }
  680.  
  681. /**
  682.  * Set multiple values to cache at once on specified server.
  683.  *
  684.  * By sending an array of $items to this function, all values are saved at once to
  685.  * memcached, reducing the need for multiple requests to memcached. The $items array
  686.  * keys and values are what are stored to memcached. The keys in the $items array
  687.  * are merged with the $groups array/string value via buildKeys to determine the
  688.  * final key for the object.
  689.  *
  690.  * @param string        $server_key The key identifying the server to store the value on.
  691.  * @param array         $items      An array of key/value pairs to store on the server.
  692.  * @param string|array  $groups     Group(s) to merge with key(s) in $items.
  693.  * @param int           $expiration The expiration time, defaults to 0.
  694.  * @return bool                     Returns TRUE on success or FALSE on failure.
  695.  */
  696. function wp_cache_set_multi_by_key( $server_key, $items, $groups = 'default', $expiration = 0 ) {
  697.     global $wp_object_cache;
  698.     return $wp_object_cache->setMultiByKey( $server_key, $items, $groups, $expiration );
  699. }
  700.  
  701. /**
  702.  * Set a Memcached option.
  703.  *
  704.  * @link http://www.php.net/manual/en/memcached.setoption.php
  705.  *
  706.  * @param int       $option Option name.
  707.  * @param mixed     $value  Option value.
  708.  * @return bool             Returns TRUE on success or FALSE on failure.
  709.  */
  710. function wp_cache_set_option( $option, $value ) {
  711.     global $wp_object_cache;
  712.     return $wp_object_cache->setOption( $option, $value );
  713. }
  714.  
  715. /**
  716.  * Switch blog prefix, which changes the cache that is accessed.
  717.  *
  718.  * @param  int     $blog_id    Blog to switch to.
  719.  * @return void
  720.  */
  721. function wp_cache_switch_to_blog( $blog_id ) {
  722.     global $wp_object_cache;
  723.     return $wp_object_cache->switch_to_blog( $blog_id );
  724. }
  725.  
  726.  
  727. /**
  728.  * Sets up Object Cache Global and assigns it.
  729.  *
  730.  * @global  WP_Object_Cache     $wp_object_cache    WordPress Object Cache
  731.  * @return  void
  732.  */
  733. function wp_cache_init() {
  734.     global $wp_object_cache;
  735.     $wp_object_cache = new WP_Object_Cache();
  736. }
  737.  
  738. /**
  739.  * Adds a group or set of groups to the list of non-persistent groups.
  740.  *
  741.  * @param   string|array    $groups     A group or an array of groups to add.
  742.  * @return  void
  743.  */
  744. function wp_cache_add_global_groups( $groups ) {
  745.     global $wp_object_cache;
  746.     $wp_object_cache->add_global_groups( $groups );
  747. }
  748.  
  749. /**
  750.  * Adds a group or set of groups to the list of non-Memcached groups.
  751.  *
  752.  * @param   string|array    $groups     A group or an array of groups to add.
  753.  * @return  void
  754.  */
  755. function wp_cache_add_non_persistent_groups( $groups ) {
  756.     global $wp_object_cache;
  757.     $wp_object_cache->add_non_persistent_groups( $groups );
  758. }
  759.  
  760. class WP_Object_Cache {
  761.  
  762.     /**
  763.      * Holds the Memcached object.
  764.      *
  765.      * @var Memcached
  766.      */
  767.     public $m;
  768.  
  769.     /**
  770.      * Hold the Memcached server details.
  771.      *
  772.      * @var array
  773.      */
  774.     public $servers;
  775.  
  776.     /**
  777.      * Holds the non-Memcached objects.
  778.      *
  779.      * @var array
  780.      */
  781.     public $cache = array();
  782.  
  783.     /**
  784.      * List of global groups.
  785.      *
  786.      * @var array
  787.      */
  788.     public $global_groups = array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss' );
  789.  
  790.     /**
  791.      * List of groups not saved to Memcached.
  792.      *
  793.      * @var array
  794.      */
  795.     public $no_mc_groups = array( 'comment', 'counts' );
  796.  
  797.     /**
  798.      * Prefix used for global groups.
  799.      *
  800.      * @var string
  801.      */
  802.     public $global_prefix = '';
  803.  
  804.     /**
  805.      * Prefix used for non-global groups.
  806.      *
  807.      * @var string
  808.      */
  809.     public $blog_prefix = '';
  810.  
  811.     /**
  812.      * Instantiate the Memcached class.
  813.      *
  814.      * Instantiates the Memcached class and returns adds the servers specified
  815.      * in the $memcached_servers global array.
  816.      *
  817.      * @link    http://www.php.net/manual/en/memcached.construct.php
  818.      *
  819.      * @param   null    $persistent_id      To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance.
  820.      */
  821.     public function __construct( $persistent_id = NULL ) {
  822.         global $memcached_servers, $blog_id, $table_prefix;
  823.  
  824.         if ( is_null( $persistent_id ) || ! is_string( $persistent_id ) )
  825.             $this->m = new Memcached();
  826.         else
  827.             $this->m = new Memcached( $persistent_id );
  828.  
  829.         if ( isset( $memcached_servers ) )
  830.             $this->servers = $memcached_servers;
  831.         else
  832.             $this->servers = array( array( '127.0.0.1', 11211 ) );
  833.  
  834.         $this->addServers( $this->servers );
  835.  
  836.         /**
  837.          * This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for
  838.          * multi single WP installs on the same server.
  839.          */
  840.         if ( ! defined( 'WP_CACHE_KEY_SALT' ) )
  841.             define( 'WP_CACHE_KEY_SALT', '' );
  842.  
  843.         // Assign global and blog prefixes for use with keys
  844.         if ( function_exists( 'is_multisite' ) ) {
  845.             $this->global_prefix = ( is_multisite() || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) ? '' : $table_prefix;
  846.             $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
  847.         }
  848.  
  849.         // Setup cacheable values for handling expiration times
  850.         $this->thirty_days = 60 * 60 * 24 * 30;
  851.         $this->now         = time();
  852.     }
  853.  
  854.     /**
  855.      * Adds a value to cache.
  856.      *
  857.      * If the specified key already exists, the value is not stored and the function
  858.      * returns false.
  859.      *
  860.      * @link    http://www.php.net/manual/en/memcached.add.php
  861.      *
  862.      * @param   string      $key            The key under which to store the value.
  863.      * @param   mixed       $value          The value to store.
  864.      * @param   string      $group          The group value appended to the $key.
  865.      * @param   int         $expiration     The expiration time, defaults to 0.
  866.      * @param   string      $server_key     The key identifying the server to store the value on.
  867.      * @param   bool        $byKey          True to store in internal cache by key; false to not store by key
  868.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  869.      */
  870.     public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) {
  871.         if ( wp_suspend_cache_addition() ) {
  872.             return false;
  873.         }
  874.  
  875.         $derived_key = $this->buildKey( $key, $group );
  876.         $expiration  = $this->sanitize_expiration( $expiration );
  877.  
  878.         // If group is a non-Memcached group, save to runtime cache, not Memcached
  879.         if ( in_array( $group, $this->no_mc_groups ) ) {
  880.  
  881.             // Add does not set the value if the key exists; mimic that here
  882.             if ( isset( $this->cache[$derived_key] ) )
  883.                 return false;
  884.  
  885.             $this->add_to_internal_cache( $derived_key, $value );
  886.  
  887.             return true;
  888.         }
  889.  
  890.         // Save to Memcached
  891.         if ( $byKey )
  892.             $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration );
  893.         else
  894.             $result = $this->m->add( $derived_key, $value, $expiration );
  895.  
  896.         // Store in runtime cache if add was successful
  897.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  898.             $this->add_to_internal_cache( $derived_key, $value );
  899.  
  900.         return $result;
  901.     }
  902.  
  903.     /**
  904.      * Adds a value to cache on a specific server.
  905.      *
  906.      * Using a server_key value, the object can be stored on a specified server as opposed
  907.      * to a random server in the stack. Note that this method will add the key/value to the
  908.      * _cache object as part of the runtime cache. It will add it to an array for the
  909.      * specified server_key.
  910.      *
  911.      * @link    http://www.php.net/manual/en/memcached.addbykey.php
  912.      *
  913.      * @param   string      $server_key     The key identifying the server to store the value on.
  914.      * @param   string      $key            The key under which to store the value.
  915.      * @param   mixed       $value          The value to store.
  916.      * @param   string      $group          The group value appended to the $key.
  917.      * @param   int         $expiration     The expiration time, defaults to 0.
  918.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  919.      */
  920.     public function addByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
  921.         return $this->add( $key, $value, $group, $expiration, $server_key, true );
  922.     }
  923.  
  924.     /**
  925.      * Add a single server to the list of Memcached servers.
  926.      *
  927.      * @link http://www.php.net/manual/en/memcached.addserver.php
  928.      *
  929.      * @param   string      $host           The hostname of the memcache server.
  930.      * @param   int         $port           The port on which memcache is running.
  931.      * @param   int         $weight         The weight of the server relative to the total weight of all the servers in the pool.
  932.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  933.      */
  934.     public function addServer( $host, $port, $weight = 0 ) {
  935.         $host = is_string( $host ) ? $host : '127.0.0.1';
  936.         $port = is_numeric( $port ) && $port > 0 ? $port : 11211;
  937.         $weight = is_numeric( $weight ) && $weight > 0 ? $weight : 1;
  938.  
  939.         return $this->m->addServer( $host, $port, $weight );
  940.     }
  941.  
  942.     /**
  943.      * Adds an array of servers to the pool.
  944.      *
  945.      * Each individual server in the array must include a domain and port, with an optional
  946.      * weight value: $servers = array( array( '127.0.0.1', 11211, 0 ) );
  947.      *
  948.      * @link    http://www.php.net/manual/en/memcached.addservers.php
  949.      *
  950.      * @param   array       $servers        Array of server to register.
  951.      * @return  bool                        True on success; false on failure.
  952.      */
  953.     public function addServers( $servers ) {
  954.         if ( ! is_object( $this->m ) )
  955.             return false;
  956.  
  957.         return $this->m->addServers( $servers );
  958.     }
  959.  
  960.     /**
  961.      * Append data to an existing item.
  962.      *
  963.      * This method should throw an error if it is used with compressed data. This
  964.      * is an expected behavior. Memcached casts the value to be appended to the initial value to the
  965.      * type of the initial value. Be careful as this leads to unexpected behavior at times. Due to
  966.      * how memcached treats types, the behavior has been mimicked in the internal cache to produce
  967.      * similar results and improve consistency. It is recommend that appends only occur with data of
  968.      * the same type.
  969.      *
  970.      * @link    http://www.php.net/manual/en/memcached.append.php
  971.      *
  972.      * @param   string      $key            The key under which to store the value.
  973.      * @param   mixed       $value          Must be string as appending mixed values is not well-defined.
  974.      * @param   string      $group          The group value appended to the $key.
  975.      * @param   string      $server_key     The key identifying the server to store the value on.
  976.      * @param   bool        $byKey          True to store in internal cache by key; false to not store by key
  977.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  978.      */
  979.     public function append( $key, $value, $group = 'default', $server_key = '', $byKey = false ) {
  980.         if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) )
  981.             return false;
  982.  
  983.         $derived_key = $this->buildKey( $key, $group );
  984.  
  985.         // If group is a non-Memcached group, append to runtime cache value, not Memcached
  986.         if ( in_array( $group, $this->no_mc_groups ) ) {
  987.             if ( ! isset( $this->cache[$derived_key] ) )
  988.                 return false;
  989.  
  990.             $combined = $this->combine_values( $this->cache[$derived_key], $value, 'app' );
  991.             $this->add_to_internal_cache( $derived_key, $combined );
  992.             return true;
  993.         }
  994.  
  995.         // Append to Memcached value
  996.         if ( $byKey )
  997.             $result = $this->m->appendByKey( $server_key, $derived_key, $value );
  998.         else
  999.             $result = $this->m->append( $derived_key, $value );
  1000.  
  1001.         // Store in runtime cache if add was successful
  1002.         if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
  1003.             $combined = $this->combine_values( $this->cache[$derived_key], $value, 'app' );
  1004.             $this->add_to_internal_cache( $derived_key, $combined );
  1005.         }
  1006.  
  1007.         return $result;
  1008.     }
  1009.  
  1010.     /**
  1011.      * Append data to an existing item by server key.
  1012.      *
  1013.      * This method should throw an error if it is used with compressed data. This
  1014.      * is an expected behavior. Memcached casts the value to be appended to the initial value to the
  1015.      * type of the initial value. Be careful as this leads to unexpected behavior at times. Due to
  1016.      * how memcached treats types, the behavior has been mimicked in the internal cache to produce
  1017.      * similar results and improve consistency. It is recommend that appends only occur with data of
  1018.      * the same type.
  1019.      *
  1020.      * @link    http://www.php.net/manual/en/memcached.appendbykey.php
  1021.      *
  1022.      * @param   string      $server_key     The key identifying the server to store the value on.
  1023.      * @param   string      $key            The key under which to store the value.
  1024.      * @param   mixed       $value          Must be string as appending mixed values is not well-defined
  1025.      * @param   string      $group          The group value appended to the $key.
  1026.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1027.      */
  1028.     public function appendByKey( $server_key, $key, $value, $group = 'default' ) {
  1029.         return $this->append( $key, $value, $group, $server_key, true );
  1030.     }
  1031.  
  1032.     /**
  1033.      * Performs a "check and set" to store data.
  1034.      *
  1035.      * The set will be successful only if the no other request has updated the value since it was fetched since
  1036.      * this request.
  1037.      *
  1038.      * @link    http://www.php.net/manual/en/memcached.cas.php
  1039.      *
  1040.      * @param   float       $cas_token      Unique value associated with the existing item. Generated by memcached.
  1041.      * @param   string      $key            The key under which to store the value.
  1042.      * @param   mixed       $value          The value to store.
  1043.      * @param   string      $group          The group value appended to the $key.
  1044.      * @param   int         $expiration     The expiration time, defaults to 0.
  1045.      * @param   string      $server_key     The key identifying the server to store the value on.
  1046.      * @param   bool        $byKey          True to store in internal cache by key; false to not store by key
  1047.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1048.      */
  1049.     public function cas( $cas_token, $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) {
  1050.         $derived_key = $this->buildKey( $key, $group );
  1051.         $expiration  = $this->sanitize_expiration( $expiration );
  1052.  
  1053.         /**
  1054.          * If group is a non-Memcached group, save to runtime cache, not Memcached. Note
  1055.          * that since check and set cannot be emulated in the run time cache, this value
  1056.          * operation is treated as a normal "add" for no_mc_groups.
  1057.          */
  1058.         if ( in_array( $group, $this->no_mc_groups ) ) {
  1059.             $this->add_to_internal_cache( $derived_key, $value );
  1060.             return true;
  1061.         }
  1062.  
  1063.         // Save to Memcached
  1064.         if ( $byKey )
  1065.             $result = $this->m->casByKey( $cas_token, $server_key, $derived_key, $value, $expiration );
  1066.         else
  1067.             $result = $this->m->cas( $cas_token, $derived_key, $value, $expiration );
  1068.  
  1069.         // Store in runtime cache if cas was successful
  1070.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1071.             $this->add_to_internal_cache( $derived_key, $value );
  1072.  
  1073.         return $result;
  1074.     }
  1075.  
  1076.     /**
  1077.      * Performs a "check and set" to store data with a server key.
  1078.      *
  1079.      * The set will be successful only if the no other request has updated the value since it was fetched by
  1080.      * this request.
  1081.      *
  1082.      * @link    http://www.php.net/manual/en/memcached.casbykey.php
  1083.      *
  1084.      * @param   string      $server_key     The key identifying the server to store the value on.
  1085.      * @param   float       $cas_token      Unique value associated with the existing item. Generated by memcached.
  1086.      * @param   string      $key            The key under which to store the value.
  1087.      * @param   mixed       $value          The value to store.
  1088.      * @param   string      $group          The group value appended to the $key.
  1089.      * @param   int         $expiration     The expiration time, defaults to 0.
  1090.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1091.      */
  1092.     public function casByKey( $cas_token, $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
  1093.         return $this->cas( $cas_token, $key, $value, $group, $expiration, $server_key, true );
  1094.     }
  1095.  
  1096.     /**
  1097.      * Decrement a numeric item's value.
  1098.      *
  1099.      * @link http://www.php.net/manual/en/memcached.decrement.php
  1100.      *
  1101.      * @param string    $key    The key under which to store the value.
  1102.      * @param int       $offset The amount by which to decrement the item's value.
  1103.      * @param string    $group  The group value appended to the $key.
  1104.      * @return int|bool         Returns item's new value on success or FALSE on failure.
  1105.      */
  1106.     public function decrement( $key, $offset = 1, $group = 'default' ) {
  1107.         $derived_key = $this->buildKey( $key, $group );
  1108.  
  1109.         // Decrement values in no_mc_groups
  1110.         if ( in_array( $group, $this->no_mc_groups ) ) {
  1111.  
  1112.             // Only decrement if the key already exists and value is 0 or greater (mimics memcached behavior)
  1113.             if ( isset( $this->cache[$derived_key] ) && $this->cache[$derived_key] >= 0 ) {
  1114.  
  1115.                 // If numeric, subtract; otherwise, consider it 0 and do nothing
  1116.                 if ( is_numeric( $this->cache[$derived_key] ) )
  1117.                     $this->cache[$derived_key] -= (int) $offset;
  1118.                 else
  1119.                     $this->cache[$derived_key] = 0;
  1120.  
  1121.                 // Returned value cannot be less than 0
  1122.                 if ( $this->cache[$derived_key] < 0 )
  1123.                     $this->cache[$derived_key] = 0;
  1124.  
  1125.                 return $this->cache[$derived_key];
  1126.             } else {
  1127.                 return false;
  1128.             }
  1129.         }
  1130.  
  1131.         $result = $this->m->decrement( $derived_key, $offset );
  1132.  
  1133.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1134.             $this->add_to_internal_cache( $derived_key, $result );
  1135.  
  1136.         return $result;
  1137.     }
  1138.  
  1139.     /**
  1140.      * Decrement a numeric item's value.
  1141.      *
  1142.      * Alias for $this->decrement. Other caching backends use this abbreviated form of the function. It *may* cause
  1143.      * breakage somewhere, so it is nice to have. This function will also allow the core unit tests to pass.
  1144.      *
  1145.      * @param string    $key    The key under which to store the value.
  1146.      * @param int       $offset The amount by which to decrement the item's value.
  1147.      * @param string    $group  The group value appended to the $key.
  1148.      * @return int|bool         Returns item's new value on success or FALSE on failure.
  1149.      */
  1150.     public function decr( $key, $offset = 1, $group = 'default' ) {
  1151.         return $this->decrement( $key, $offset, $group );
  1152.     }
  1153.  
  1154.     /**
  1155.      * Remove the item from the cache.
  1156.      *
  1157.      * Remove an item from memcached with identified by $key after $time seconds. The
  1158.      * $time parameter allows an object to be queued for deletion without immediately
  1159.      * deleting. Between the time that it is queued and the time it's deleted, add,
  1160.      * replace, and get will fail, but set will succeed.
  1161.      *
  1162.      * @link http://www.php.net/manual/en/memcached.delete.php
  1163.      *
  1164.      * @param   string      $key        The key under which to store the value.
  1165.      * @param   string      $group      The group value appended to the $key.
  1166.      * @param   int         $time       The amount of time the server will wait to delete the item in seconds.
  1167.      * @param   string      $server_key The key identifying the server to store the value on.
  1168.      * @param   bool        $byKey      True to store in internal cache by key; false to not store by key
  1169.      * @return  bool                    Returns TRUE on success or FALSE on failure.
  1170.      */
  1171.     public function delete( $key, $group = 'default', $time = 0, $server_key = '', $byKey = false ) {
  1172.         $derived_key = $this->buildKey( $key, $group );
  1173.  
  1174.         // Remove from no_mc_groups array
  1175.         if ( in_array( $group, $this->no_mc_groups ) ) {
  1176.             if ( isset( $this->cache[$derived_key] ) )
  1177.                 unset( $this->cache[$derived_key] );
  1178.  
  1179.             return true;
  1180.         }
  1181.  
  1182.         if ( $byKey )
  1183.             $result = $this->m->deleteByKey( $server_key, $derived_key, $time );
  1184.         else
  1185.             $result = $this->m->delete( $derived_key, $time );
  1186.  
  1187.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1188.             unset( $this->cache[$derived_key] );
  1189.  
  1190.         return $result;
  1191.     }
  1192.  
  1193.     /**
  1194.      * Remove the item from the cache by server key.
  1195.      *
  1196.      * Remove an item from memcached with identified by $key after $time seconds. The
  1197.      * $time parameter allows an object to be queued for deletion without immediately
  1198.      * deleting. Between the time that it is queued and the time it's deleted, add,
  1199.      * replace, and get will fail, but set will succeed.
  1200.      *
  1201.      * @link http://www.php.net/manual/en/memcached.deletebykey.php
  1202.      *
  1203.      * @param   string      $server_key The key identifying the server to store the value on.
  1204.      * @param   string      $key        The key under which to store the value.
  1205.      * @param   string      $group      The group value appended to the $key.
  1206.      * @param   int         $time       The amount of time the server will wait to delete the item in seconds.
  1207.      * @return  bool                    Returns TRUE on success or FALSE on failure.
  1208.      */
  1209.     public function deleteByKey( $server_key, $key, $group = 'default', $time = 0 ) {
  1210.         return $this->delete( $key, $group, $time, $server_key, true );
  1211.     }
  1212.  
  1213.     /**
  1214.      * Fetch the next result.
  1215.      *
  1216.      * @link http://www.php.net/manual/en/memcached.fetch.php
  1217.      *
  1218.      * @return array|bool   Returns the next result or FALSE on failure.
  1219.      */
  1220.     public function fetch() {
  1221.         return $this->m->fetch();
  1222.     }
  1223.  
  1224.     /**
  1225.      * Fetch all remaining results from the last request.
  1226.      *
  1227.      * @link http://www.php.net/manual/en/memcached.fetchall.php
  1228.      *
  1229.      * @return  array|bool          Returns the results or FALSE on failure.
  1230.      */
  1231.     public function fetchAll() {
  1232.         return $this->m->fetchAll();
  1233.     }
  1234.  
  1235.     /**
  1236.      * Invalidate all items in the cache.
  1237.      *
  1238.      * @link http://www.php.net/manual/en/memcached.flush.php
  1239.      *
  1240.      * @param   int     $delay      Number of seconds to wait before invalidating the items.
  1241.      * @return  bool                Returns TRUE on success or FALSE on failure.
  1242.      */
  1243.     public function flush( $delay = 0 ) {
  1244.         $result = $this->m->flush( $delay );
  1245.  
  1246.         // Only reset the runtime cache if memcached was properly flushed
  1247.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1248.             $this->cache = array();
  1249.  
  1250.         return $result;
  1251.     }
  1252.  
  1253.     /**
  1254.      * Retrieve object from cache.
  1255.      *
  1256.      * Gets an object from cache based on $key and $group. In order to fully support the $cache_cb and $cas_token
  1257.      * parameters, the runtime cache is ignored by this function if either of those values are set. If either of
  1258.      * those values are set, the request is made directly to the memcached server for proper handling of the
  1259.      * callback and/or token. Note that the $cas_token variable cannot be directly passed to the function. The
  1260.      * variable need to be first defined with a non null value.
  1261.      *
  1262.      * If using the $cache_cb argument, the new value will always have an expiration of time of 0 (forever). This
  1263.      * is a limitation of the Memcached PECL extension.
  1264.      *
  1265.      * @link http://www.php.net/manual/en/memcached.get.php
  1266.      *
  1267.      * @param   string          $key        The key under which to store the value.
  1268.      * @param   string          $group      The group value appended to the $key.
  1269.      * @param   bool            $force      Whether or not to force a cache invalidation.
  1270.      * @param   null|bool       $found      Variable passed by reference to determine if the value was found or not.
  1271.      * @param   string          $server_key The key identifying the server to store the value on.
  1272.      * @param   bool            $byKey      True to store in internal cache by key; false to not store by key
  1273.      * @param   null|callable   $cache_cb   Read-through caching callback.
  1274.      * @param   null|float      $cas_token  The variable to store the CAS token in.
  1275.      * @return  bool|mixed                  Cached object value.
  1276.      */
  1277.     public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $byKey = false, $cache_cb = NULL, &$cas_token = NULL ) {
  1278.         $derived_key = $this->buildKey( $key, $group );
  1279.  
  1280.         // Assume object is not found
  1281.         $found = false;
  1282.  
  1283.         // If either $cache_db, or $cas_token is set, must hit Memcached and bypass runtime cache
  1284.         if ( func_num_args() > 6 && ! in_array( $group, $this->no_mc_groups ) ) {
  1285.             if ( $byKey )
  1286.                 $value = $this->m->getByKey( $server_key, $derived_key, $cache_cb, $cas_token );
  1287.             else
  1288.                 $value = $this->m->get( $derived_key, $cache_cb, $cas_token );
  1289.         } else {
  1290.             if ( isset( $this->cache[$derived_key] ) ) {
  1291.                 $found = true;
  1292.                 return is_object( $this->cache[$derived_key] ) ? clone $this->cache[$derived_key] : $this->cache[$derived_key];
  1293.             } elseif ( in_array( $group, $this->no_mc_groups ) ) {
  1294.                 return false;
  1295.             } else {
  1296.                 if ( $byKey )
  1297.                     $value = $this->m->getByKey( $server_key, $derived_key );
  1298.                 else
  1299.                     $value = $this->m->get( $derived_key );
  1300.             }
  1301.         }
  1302.  
  1303.         if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
  1304.             $this->add_to_internal_cache( $derived_key, $value );
  1305.             $found = true;
  1306.         }
  1307.  
  1308.         return is_object( $value ) ? clone $value : $value;
  1309.     }
  1310.  
  1311.     /**
  1312.      * Retrieve object from cache from specified server.
  1313.      *
  1314.      * Gets an object from cache based on $key, $group and $server_key. In order to fully support the $cache_cb and $cas_token
  1315.      * parameters, the runtime cache is ignored by this function if either of those values are set. If either of
  1316.      * those values are set, the request is made directly to the memcached server for proper handling of the
  1317.      * callback and/or token. Note that the $cas_token variable cannot be directly passed to the function. The
  1318.      * variable need to be first defined with a non null value.
  1319.      *
  1320.      * If using the $cache_cb argument, the new value will always have an expiration of time of 0 (forever). This
  1321.      * is a limitation of the Memcached PECL extension.
  1322.      *
  1323.      * @link http://www.php.net/manual/en/memcached.getbykey.php
  1324.      *
  1325.      * @param   string          $server_key The key identifying the server to store the value on.
  1326.      * @param   string          $key        The key under which to store the value.
  1327.      * @param   string          $group      The group value appended to the $key.
  1328.      * @param   bool            $force      Whether or not to force a cache invalidation.
  1329.      * @param   null|bool       $found      Variable passed by reference to determine if the value was found or not.
  1330.      * @param   null|string     $cache_cb   Read-through caching callback.
  1331.      * @param   null|float      $cas_token  The variable to store the CAS token in.
  1332.      * @return  bool|mixed                  Cached object value.
  1333.      */
  1334.     public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = NULL, &$cas_token = NULL ) {
  1335.         /**
  1336.          * Need to be careful how "get" is called. If you send $cache_cb, and $cas_token, it will hit memcached.
  1337.          * Only send those args if they were sent to this function.
  1338.          */
  1339.         if ( func_num_args() > 5 )
  1340.             return $this->get( $key, $group, $force, $found, $server_key, true, $cache_cb, $cas_token );
  1341.         else
  1342.             return $this->get( $key, $group, $force, $found, $server_key, true );
  1343.     }
  1344.  
  1345.     /**
  1346.      * Request multiple keys without blocking.
  1347.      *
  1348.      * @link http://www.php.net/manual/en/memcached.getdelayed.php
  1349.      *
  1350.      * @param   string|array    $keys       Array or string of key(s) to request.
  1351.      * @param   string|array    $groups     Array or string of group(s) for the key(s). See buildKeys for more on how these are handled.
  1352.      * @param   bool            $with_cas   Whether to request CAS token values also.
  1353.      * @param   null            $value_cb   The result callback or NULL.
  1354.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1355.      */
  1356.     public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = NULL ) {
  1357.         $derived_keys = $this->buildKeys( $keys, $groups );
  1358.         return $this->m->getDelayed( $derived_keys, $with_cas, $value_cb );
  1359.     }
  1360.  
  1361.     /**
  1362.      * Request multiple keys without blocking from a specified server.
  1363.      *
  1364.      * @link http://www.php.net/manual/en/memcached.getdelayed.php
  1365.      *
  1366.      * @param   string          $server_key The key identifying the server to store the value on.
  1367.      * @param   string|array    $keys       Array or string of key(s) to request.
  1368.      * @param   string|array    $groups     Array or string of group(s) for the key(s). See buildKeys for more on how these are handled.
  1369.      * @param   bool            $with_cas   Whether to request CAS token values also.
  1370.      * @param   null            $value_cb   The result callback or NULL.
  1371.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1372.      */
  1373.     public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = NULL ) {
  1374.         $derived_keys = $this->buildKeys( $keys, $groups );
  1375.         return $this->m->getDelayedByKey( $server_key, $derived_keys, $with_cas, $value_cb );
  1376.     }
  1377.  
  1378.     /**
  1379.      * Gets multiple values from memcached in one request.
  1380.      *
  1381.      * See the buildKeys method definition to understand the $keys/$groups parameters.
  1382.      *
  1383.      * @link http://www.php.net/manual/en/memcached.getmulti.php
  1384.      *
  1385.      * @param   array           $keys       Array of keys to retrieve.
  1386.      * @param   string|array    $groups     If string, used for all keys. If arrays, corresponds with the $keys array.
  1387.      * @param   string          $server_key The key identifying the server to store the value on.
  1388.      * @param   null|array      $cas_tokens The variable to store the CAS tokens for the found items.
  1389.      * @param   int             $flags      The flags for the get operation.
  1390.      * @return  bool|array                  Returns the array of found items or FALSE on failure.
  1391.      */
  1392.     public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = NULL, $flags = NULL ) {
  1393.         $derived_keys = $this->buildKeys( $keys, $groups );
  1394.  
  1395.         /**
  1396.          * If either $cas_tokens, or $flags is set, must hit Memcached and bypass runtime cache. Note that
  1397.          * this will purposely ignore no_mc_groups values as they cannot handle CAS tokens or the special
  1398.          * flags; however, if the groups of groups contains a no_mc_group, this is bypassed.
  1399.          */
  1400.         if ( func_num_args() > 3 && ! $this->contains_no_mc_group( $groups ) ) {
  1401.             if ( ! empty( $server_key ) )
  1402.                 $values = $this->m->getMultiByKey( $server_key, $derived_keys, $cas_tokens, $flags );
  1403.             else
  1404.                 $values = $this->m->getMulti( $derived_keys, $cas_tokens, $flags );
  1405.         } else {
  1406.             $values = array();
  1407.             $need_to_get = array();
  1408.  
  1409.             // Pull out values from runtime cache, or mark for retrieval
  1410.             foreach ( $derived_keys as $key ) {
  1411.                 if ( isset( $this->cache[$key] ) )
  1412.                     $values[$key] = $this->cache[$key];
  1413.                 else
  1414.                     $need_to_get[$key] = $key;
  1415.             }
  1416.  
  1417.             // Get those keys not found in the runtime cache
  1418.             if ( ! empty( $need_to_get ) ) {
  1419.                 if ( ! empty( $server_key ) )
  1420.                     $result = $this->m->getMultiByKey( $server_key, array_keys( $need_to_get ) );
  1421.                 else
  1422.                     $result = $this->m->getMulti( array_keys( $need_to_get ) );
  1423.             }
  1424.  
  1425.             // Merge with values found in runtime cache
  1426.             if ( isset( $result ) && Memcached::RES_SUCCESS === $this->getResultCode() )
  1427.                 $values = array_merge( $values, $result );
  1428.  
  1429.             // If order should be preserved, reorder now
  1430.             if ( ! empty( $need_to_get ) && $flags === Memcached::GET_PRESERVE_ORDER ) {
  1431.                 $ordered_values = array();
  1432.  
  1433.                 foreach ( $derived_keys as $key ) {
  1434.                     if ( isset( $values[$key] ) )
  1435.                         $ordered_values[$key] = $values[$key];
  1436.                 }
  1437.  
  1438.                 $values = $ordered_values;
  1439.                 unset( $ordered_values );
  1440.             }
  1441.         }
  1442.  
  1443.         // Add the values to the runtime cache
  1444.         $this->cache = array_merge( $this->cache, $values );
  1445.  
  1446.         return $values;
  1447.     }
  1448.  
  1449.     /**
  1450.      * Gets multiple values from memcached in one request by specified server key.
  1451.      *
  1452.      * See the buildKeys method definition to understand the $keys/$groups parameters.
  1453.      *
  1454.      * @link http://www.php.net/manual/en/memcached.getmultibykey.php
  1455.      *
  1456.      * @param   string          $server_key The key identifying the server to store the value on.
  1457.      * @param   array           $keys       Array of keys to retrieve.
  1458.      * @param   string|array    $groups     If string, used for all keys. If arrays, corresponds with the $keys array.
  1459.      * @param   null|array      $cas_tokens The variable to store the CAS tokens for the found items.
  1460.      * @param   int             $flags      The flags for the get operation.
  1461.      * @return  bool|array                  Returns the array of found items or FALSE on failure.
  1462.      */
  1463.     public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = NULL, $flags = NULL ) {
  1464.         /**
  1465.          * Need to be careful how "getMulti" is called. If you send $cache_cb, and $cas_token, it will hit memcached.
  1466.          * Only send those args if they were sent to this function.
  1467.          */
  1468.         if ( func_num_args() > 3 )
  1469.             return $this->getMulti( $keys, $groups, $server_key, $cas_tokens, $flags );
  1470.         else
  1471.             return $this->getMulti( $keys, $groups, $server_key );
  1472.     }
  1473.  
  1474.     /**
  1475.      * Retrieve a Memcached option value.
  1476.      *
  1477.      * @link http://www.php.net/manual/en/memcached.getoption.php
  1478.      *
  1479.      * @param   int         $option     One of the Memcached::OPT_* constants.
  1480.      * @return  mixed                   Returns the value of the requested option, or FALSE on error.
  1481.      */
  1482.     public function getOption( $option ) {
  1483.         return $this->m->getOption( $option );
  1484.     }
  1485.  
  1486.     /**
  1487.      * Return the result code of the last option.
  1488.      *
  1489.      * @link http://www.php.net/manual/en/memcached.getresultcode.php
  1490.      *
  1491.      * @return  int     Result code of the last Memcached operation.
  1492.      */
  1493.     public function getResultCode() {
  1494.         return $this->m->getResultCode();
  1495.     }
  1496.  
  1497.     /**
  1498.      * Return the message describing the result of the last operation.
  1499.      *
  1500.      * @link    http://www.php.net/manual/en/memcached.getresultmessage.php
  1501.      *
  1502.      * @return  string      Message describing the result of the last Memcached operation.
  1503.      */
  1504.     public function getResultMessage() {
  1505.         return $this->m->getResultMessage();
  1506.     }
  1507.  
  1508.     /**
  1509.      * Get server information by key.
  1510.      *
  1511.      * @link    http://www.php.net/manual/en/memcached.getserverbykey.php
  1512.      *
  1513.      * @param   string      $server_key     The key identifying the server to store the value on.
  1514.      * @return  array                       Array with host, post, and weight on success, FALSE on failure.
  1515.      */
  1516.     public function getServerByKey( $server_key ) {
  1517.         return $this->m->getServerByKey( $server_key );
  1518.     }
  1519.  
  1520.     /**
  1521.      * Get the list of servers in the pool.
  1522.      *
  1523.      * @link    http://www.php.net/manual/en/memcached.getserverlist.php
  1524.      *
  1525.      * @return  array       The list of all servers in the server pool.
  1526.      */
  1527.     public function getServerList() {
  1528.         return $this->m->getServerList();
  1529.     }
  1530.  
  1531.     /**
  1532.      * Get server pool statistics.
  1533.      *
  1534.      * @link    http://www.php.net/manual/en/memcached.getstats.php
  1535.      *
  1536.      * @return  array       Array of server statistics, one entry per server.
  1537.      */
  1538.     public function getStats() {
  1539.         return $this->m->getStats();
  1540.     }
  1541.  
  1542.     /**
  1543.      * Get server pool memcached version information.
  1544.      *
  1545.      * @link    http://www.php.net/manual/en/memcached.getversion.php
  1546.      *
  1547.      * @return  array       Array of server versions, one entry per server.
  1548.      */
  1549.     public function getVersion() {
  1550.         return $this->m->getVersion();
  1551.     }
  1552.  
  1553.     /**
  1554.      * Increment a numeric item's value.
  1555.      *
  1556.      * @link http://www.php.net/manual/en/memcached.increment.php
  1557.      *
  1558.      * @param   string      $key        The key under which to store the value.
  1559.      * @param   int         $offset     The amount by which to increment the item's value.
  1560.      * @param   string      $group      The group value appended to the $key.
  1561.      * @return  int|bool                Returns item's new value on success or FALSE on failure.
  1562.      */
  1563.     public function increment( $key, $offset = 1, $group = 'default' ) {
  1564.         $derived_key = $this->buildKey( $key, $group );
  1565.  
  1566.         // Increment values in no_mc_groups
  1567.         if ( in_array( $group, $this->no_mc_groups ) ) {
  1568.  
  1569.             // Only increment if the key already exists and the number is currently 0 or greater (mimics memcached behavior)
  1570.             if ( isset( $this->cache[$derived_key] ) &&  $this->cache[$derived_key] >= 0 ) {
  1571.  
  1572.                 // If numeric, add; otherwise, consider it 0 and do nothing
  1573.                 if ( is_numeric( $this->cache[$derived_key] ) )
  1574.                     $this->cache[$derived_key] += (int) $offset;
  1575.                 else
  1576.                     $this->cache[$derived_key] = 0;
  1577.  
  1578.                 // Returned value cannot be less than 0
  1579.                 if ( $this->cache[$derived_key] < 0 )
  1580.                     $this->cache[$derived_key] = 0;
  1581.  
  1582.                 return $this->cache[$derived_key];
  1583.             } else {
  1584.                 return false;
  1585.             }
  1586.         }
  1587.  
  1588.         $result = $this->m->increment( $derived_key, $offset );
  1589.  
  1590.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1591.             $this->add_to_internal_cache( $derived_key, $result );
  1592.  
  1593.         return $result;
  1594.     }
  1595.  
  1596.     /**
  1597.      * Synonymous with $this->incr.
  1598.      *
  1599.      * Certain plugins expect an "incr" method on the $wp_object_cache object (e.g., Batcache). Since the original
  1600.      * version of this library matched names to the memcached methods, the "incr" method was missing. Adding this
  1601.      * method restores compatibility with plugins expecting an "incr" method.
  1602.      *
  1603.      * @param   string      $key        The key under which to store the value.
  1604.      * @param   int         $offset     The amount by which to increment the item's value.
  1605.      * @param   string      $group      The group value appended to the $key.
  1606.      * @return  int|bool                Returns item's new value on success or FALSE on failure.
  1607.      */
  1608.     public function incr( $key, $offset = 1, $group = 'default' ) {
  1609.         return $this->increment( $key, $offset, $group );
  1610.     }
  1611.  
  1612.     /**
  1613.      * Prepend data to an existing item.
  1614.      *
  1615.      * This method should throw an error if it is used with compressed data. This is an expected behavior.
  1616.      * Memcached casts the value to be prepended to the initial value to the type of the initial value. Be
  1617.      * careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23 to
  1618.      * (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
  1619.      * (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
  1620.      * mimicked in the internal cache to produce similar results and improve consistency. It is recommend
  1621.      * that prepends only occur with data of the same type.
  1622.      *
  1623.      * @link    http://www.php.net/manual/en/memcached.prepend.php
  1624.      *
  1625.      * @param   string    $key          The key under which to store the value.
  1626.      * @param   string    $value        Must be string as prepending mixed values is not well-defined.
  1627.      * @param   string    $group        The group value prepended to the $key.
  1628.      * @param   string    $server_key   The key identifying the server to store the value on.
  1629.      * @param   bool      $byKey        True to store in internal cache by key; false to not store by key
  1630.      * @return  bool                    Returns TRUE on success or FALSE on failure.
  1631.      */
  1632.     public function prepend( $key, $value, $group = 'default', $server_key = '', $byKey = false ) {
  1633.         if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) )
  1634.             return false;
  1635.  
  1636.         $derived_key = $this->buildKey( $key, $group );
  1637.  
  1638.         // If group is a non-Memcached group, prepend to runtime cache value, not Memcached
  1639.         if ( in_array( $group, $this->no_mc_groups ) ) {
  1640.             if ( ! isset( $this->cache[$derived_key] ) )
  1641.                 return false;
  1642.  
  1643.             $combined = $this->combine_values( $this->cache[$derived_key], $value, 'pre' );
  1644.             $this->add_to_internal_cache( $derived_key, $combined );
  1645.             return true;
  1646.         }
  1647.  
  1648.         // Append to Memcached value
  1649.         if ( $byKey )
  1650.             $result = $this->m->prependByKey( $server_key, $derived_key, $value );
  1651.         else
  1652.             $result = $this->m->prepend( $derived_key, $value );
  1653.  
  1654.         // Store in runtime cache if add was successful
  1655.         if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
  1656.             $combined = $this->combine_values( $this->cache[$derived_key], $value, 'pre' );
  1657.             $this->add_to_internal_cache( $derived_key, $combined );
  1658.         }
  1659.  
  1660.         return $result;
  1661.     }
  1662.  
  1663.     /**
  1664.      * Append data to an existing item by server key.
  1665.      *
  1666.      * This method should throw an error if it is used with compressed data. This is an expected behavior.
  1667.      * Memcached casts the value to be prepended to the initial value to the type of the initial value. Be
  1668.      * careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23 to
  1669.      * (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
  1670.      * (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
  1671.      * mimicked in the internal cache to produce similar results and improve consistency. It is recommend
  1672.      * that prepends only occur with data of the same type.
  1673.      *
  1674.      * @link    http://www.php.net/manual/en/memcached.prependbykey.php
  1675.      *
  1676.      * @param   string    $server_key   The key identifying the server to store the value on.
  1677.      * @param   string    $key          The key under which to store the value.
  1678.      * @param   string    $value        Must be string as prepending mixed values is not well-defined.
  1679.      * @param   string    $group        The group value prepended to the $key.
  1680.      * @return  bool                    Returns TRUE on success or FALSE on failure.
  1681.      */
  1682.     public function prependByKey( $server_key, $key, $value, $group = 'default' ) {
  1683.         return $this->prepend( $key, $value, $group, $server_key, true );
  1684.     }
  1685.  
  1686.     /**
  1687.      * Replaces a value in cache.
  1688.      *
  1689.      * This method is similar to "add"; however, is does not successfully set a value if
  1690.      * the object's key is not already set in cache.
  1691.      *
  1692.      * @link    http://www.php.net/manual/en/memcached.replace.php
  1693.      *
  1694.      * @param   string      $server_key     The key identifying the server to store the value on.
  1695.      * @param   string      $key            The key under which to store the value.
  1696.      * @param   mixed       $value          The value to store.
  1697.      * @param   string      $group          The group value appended to the $key.
  1698.      * @param   bool        $byKey          True to store in internal cache by key; false to not store by key
  1699.      * @param   int         $expiration     The expiration time, defaults to 0.
  1700.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1701.      */
  1702.     public function replace( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) {
  1703.         $derived_key = $this->buildKey( $key, $group );
  1704.         $expiration  = $this->sanitize_expiration( $expiration );
  1705.  
  1706.         // If group is a non-Memcached group, save to runtime cache, not Memcached
  1707.         if ( in_array( $group, $this->no_mc_groups ) ) {
  1708.  
  1709.             // Replace won't save unless the key already exists; mimic this behavior here
  1710.             if ( ! isset( $this->cache[$derived_key] ) )
  1711.                 return false;
  1712.  
  1713.             $this->cache[$derived_key] = $value;
  1714.             return true;
  1715.         }
  1716.  
  1717.         // Save to Memcached
  1718.         if ( $byKey )
  1719.             $result = $this->m->replaceByKey( $server_key, $derived_key, $value, $expiration );
  1720.         else
  1721.             $result = $this->m->replace( $derived_key, $value, $expiration );
  1722.  
  1723.         // Store in runtime cache if add was successful
  1724.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1725.             $this->add_to_internal_cache( $derived_key, $value );
  1726.  
  1727.         return $result;
  1728.     }
  1729.  
  1730.     /**
  1731.      * Replaces a value in cache on a specific server.
  1732.      *
  1733.      * This method is similar to "addByKey"; however, is does not successfully set a value if
  1734.      * the object's key is not already set in cache.
  1735.      *
  1736.      * @link    http://www.php.net/manual/en/memcached.addbykey.php
  1737.      *
  1738.      * @param   string      $server_key     The key identifying the server to store the value on.
  1739.      * @param   string      $key            The key under which to store the value.
  1740.      * @param   mixed       $value          The value to store.
  1741.      * @param   string      $group          The group value appended to the $key.
  1742.      * @param   int         $expiration     The expiration time, defaults to 0.
  1743.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1744.      */
  1745.     public function replaceByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
  1746.         return $this->replace( $key, $value, $group, $expiration, $server_key, true );
  1747.     }
  1748.  
  1749.     /**
  1750.      * Sets a value in cache.
  1751.      *
  1752.      * The value is set whether or not this key already exists in memcached.
  1753.      *
  1754.      * @link http://www.php.net/manual/en/memcached.set.php
  1755.      *
  1756.      * @param   string      $key        The key under which to store the value.
  1757.      * @param   mixed       $value      The value to store.
  1758.      * @param   string      $group      The group value appended to the $key.
  1759.      * @param   int         $expiration The expiration time, defaults to 0.
  1760.      * @param   string      $server_key The key identifying the server to store the value on.
  1761.      * @param   bool        $byKey      True to store in internal cache by key; false to not store by key
  1762.      * @return  bool                    Returns TRUE on success or FALSE on failure.
  1763.      */
  1764.     public function set( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) {
  1765.         $derived_key = $this->buildKey( $key, $group );
  1766.         $expiration  = $this->sanitize_expiration( $expiration );
  1767.  
  1768.         // If group is a non-Memcached group, save to runtime cache, not Memcached
  1769.         if ( in_array( $group, $this->no_mc_groups ) ) {
  1770.             $this->add_to_internal_cache( $derived_key, $value );
  1771.             return true;
  1772.         }
  1773.  
  1774.         // Save to Memcached
  1775.         if ( $byKey ) {
  1776.             $result = $this->m->setByKey( $server_key, $derived_key, $value, $expiration );
  1777.         } else {
  1778.             $result = $this->m->set( $derived_key, $value, $expiration );
  1779.         }
  1780.  
  1781.         // Store in runtime cache if add was successful
  1782.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1783.             $this->add_to_internal_cache( $derived_key, $value );
  1784.  
  1785.         return $result;
  1786.     }
  1787.  
  1788.     /**
  1789.      * Sets a value in cache on a specific server.
  1790.      *
  1791.      * The value is set whether or not this key already exists in memcached.
  1792.      *
  1793.      * @link    http://www.php.net/manual/en/memcached.setbykey.php
  1794.      *
  1795.      * @param   string      $server_key     The key identifying the server to store the value on.
  1796.      * @param   string      $key            The key under which to store the value.
  1797.      * @param   mixed       $value          The value to store.
  1798.      * @param   string      $group          The group value appended to the $key.
  1799.      * @param   int         $expiration     The expiration time, defaults to 0.
  1800.      * @return  bool                        Returns TRUE on success or FALSE on failure.
  1801.      */
  1802.     public function setByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
  1803.         return $this->set( $key, $value, $group, $expiration, $server_key, true );
  1804.     }
  1805.  
  1806.     /**
  1807.      * Set multiple values to cache at once.
  1808.      *
  1809.      * By sending an array of $items to this function, all values are saved at once to
  1810.      * memcached, reducing the need for multiple requests to memcached. The $items array
  1811.      * keys and values are what are stored to memcached. The keys in the $items array
  1812.      * are merged with the $groups array/string value via buildKeys to determine the
  1813.      * final key for the object.
  1814.      *
  1815.      * @link    http://www.php.net/manual/en/memcached.setmulti.php
  1816.      *
  1817.      * @param   array           $items          An array of key/value pairs to store on the server.
  1818.      * @param   string|array    $groups         Group(s) to merge with key(s) in $items.
  1819.      * @param   int             $expiration     The expiration time, defaults to 0.
  1820.      * @param   string          $server_key     The key identifying the server to store the value on.
  1821.      * @param   bool            $byKey          True to store in internal cache by key; false to not store by key
  1822.      * @return  bool                            Returns TRUE on success or FALSE on failure.
  1823.      */
  1824.     public function setMulti( $items, $groups = 'default', $expiration = 0, $server_key = '', $byKey = false ) {
  1825.         // Build final keys and replace $items keys with the new keys
  1826.         $derived_keys  = $this->buildKeys( array_keys( $items ), $groups );
  1827.         $expiration    = $this->sanitize_expiration( $expiration );
  1828.         $derived_items = array_combine( $derived_keys, $items );
  1829.  
  1830.         // Do not add to memcached if in no_mc_groups
  1831.         foreach ( $derived_items as $derived_key => $value ) {
  1832.  
  1833.             // Get the individual item's group
  1834.             $key_pieces = explode( ':', $derived_key );
  1835.  
  1836.             // If group is a non-Memcached group, save to runtime cache, not Memcached
  1837.             if ( in_array( $key_pieces[1], $this->no_mc_groups ) ) {
  1838.                 $this->add_to_internal_cache( $derived_key, $value );
  1839.                 unset( $derived_items[$derived_key] );
  1840.             }
  1841.         }
  1842.  
  1843.         // Save to memcached
  1844.         if ( $byKey )
  1845.             $result = $this->m->setMultiByKey( $server_key, $derived_items, $expiration );
  1846.         else
  1847.             $result = $this->m->setMulti( $derived_items, $expiration );
  1848.  
  1849.         // Store in runtime cache if add was successful
  1850.         if ( Memcached::RES_SUCCESS === $this->getResultCode() )
  1851.             $this->cache = array_merge( $this->cache, $derived_items );
  1852.  
  1853.         return $result;
  1854.     }
  1855.  
  1856.     /**
  1857.      * Set multiple values to cache at once on specified server.
  1858.      *
  1859.      * By sending an array of $items to this function, all values are saved at once to
  1860.      * memcached, reducing the need for multiple requests to memcached. The $items array
  1861.      * keys and values are what are stored to memcached. The keys in the $items array
  1862.      * are merged with the $groups array/string value via buildKeys to determine the
  1863.      * final key for the object.
  1864.      *
  1865.      * @link    http://www.php.net/manual/en/memcached.setmultibykey.php
  1866.      *
  1867.      * @param   string          $server_key     The key identifying the server to store the value on.
  1868.      * @param   array           $items          An array of key/value pairs to store on the server.
  1869.      * @param   string|array    $groups         Group(s) to merge with key(s) in $items.
  1870.      * @param   int             $expiration     The expiration time, defaults to 0.
  1871.      * @return  bool                            Returns TRUE on success or FALSE on failure.
  1872.      */
  1873.     public function setMultiByKey( $server_key, $items, $groups = 'default', $expiration = 0 ) {
  1874.         return $this->setMulti( $items, $groups, $expiration, $server_key, true );
  1875.     }
  1876.  
  1877.     /**
  1878.      * Set a Memcached option.
  1879.      *
  1880.      * @link    http://www.php.net/manual/en/memcached.setoption.php
  1881.      *
  1882.      * @param   int         $option     Option name.
  1883.      * @param   mixed       $value      Option value.
  1884.      * @return  bool                Returns TRUE on success or FALSE on failure.
  1885.      */
  1886.     public function setOption( $option, $value ) {
  1887.         return $this->m->setOption( $option, $value );
  1888.     }
  1889.  
  1890.     /**
  1891.      * Builds a key for the cached object using the blog_id, key, and group values.
  1892.      *
  1893.      * @author  Ryan Boren   This function is inspired by the original WP Memcached Object cache.
  1894.      * @link    http://wordpress.org/extend/plugins/memcached/
  1895.      *
  1896.      * @param   string      $key        The key under which to store the value.
  1897.      * @param   string      $group      The group value appended to the $key.
  1898.      * @return  string
  1899.      */
  1900.     public function buildKey( $key, $group = 'default' ) {
  1901.         if ( empty( $group ) )
  1902.             $group = 'default';
  1903.  
  1904.         if ( false !== array_search( $group, $this->global_groups ) )
  1905.             $prefix = $this->global_prefix;
  1906.         else
  1907.             $prefix = $this->blog_prefix;
  1908.  
  1909.         return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key" );
  1910.     }
  1911.  
  1912.     /**
  1913.      * Creates an array of keys from passed key(s) and group(s).
  1914.      *
  1915.      * This function takes a string or array of key(s) and group(s) and combines them into a single dimensional
  1916.      * array that merges the keys and groups. If the same number of keys and groups exist, the final keys will
  1917.      * append $groups[n] to $keys[n]. If there are more keys than groups and the $groups parameter is an array,
  1918.      * $keys[n] will be combined with $groups[n] until $groups runs out of values. 'default' will be used for remaining
  1919.      * values. If $keys is an array and $groups is a string, all final values will append $groups to $keys[n].
  1920.      * If both values are strings, they will be combined into a single string. Note that if more $groups are received
  1921.      * than $keys, the method will return an empty array. This method is primarily a helper method for methods
  1922.      * that call memcached with an array of keys.
  1923.      *
  1924.      * @param   string|array    $keys       Key(s) to merge with group(s).
  1925.      * @param   string|array    $groups     Group(s) to merge with key(s).
  1926.      * @return  array                       Array that combines keys and groups into a single set of memcached keys.
  1927.      */
  1928.     public function buildKeys( $keys, $groups = 'default' ) {
  1929.         $derived_keys = array();
  1930.  
  1931.         // If strings sent, convert to arrays for proper handling
  1932.         if ( ! is_array( $groups ) )
  1933.             $groups = (array) $groups;
  1934.  
  1935.         if ( ! is_array( $keys ) )
  1936.             $keys = (array) $keys;
  1937.  
  1938.         // If we have equal numbers of keys and groups, merge $keys[n] and $group[n]
  1939.         if ( count( $keys ) == count( $groups ) ) {
  1940.             for ( $i = 0; $i < count( $keys ); $i++ ) {
  1941.                 $derived_keys[] = $this->buildKey( $keys[$i], $groups[$i] );
  1942.             }
  1943.  
  1944.         // If more keys are received than groups, merge $keys[n] and $group[n] until no more group are left; remaining groups are 'default'
  1945.         } elseif ( count( $keys ) > count( $groups ) ) {
  1946.             for ( $i = 0; $i < count( $keys ); $i++ ) {
  1947.                 if ( isset( $groups[$i] ) )
  1948.                     $derived_keys[] = $this->buildKey( $keys[$i], $groups[$i] );
  1949.                 elseif ( count( $groups ) == 1 )
  1950.                     $derived_keys[] = $this->buildKey( $keys[$i], $groups[0] );
  1951.                 else
  1952.                     $derived_keys[] = $this->buildKey( $keys[$i], 'default' );
  1953.             }
  1954.         }
  1955.  
  1956.         return $derived_keys;
  1957.     }
  1958.  
  1959.     /**
  1960.      * Ensure that a proper expiration time is set.
  1961.      *
  1962.      * Memcached treats any value over 30 days as a timestamp. If a developer sets the expiration for greater than 30
  1963.      * days or less than the current timestamp, the timestamp is in the past and the value isn't cached. This function
  1964.      * detects values in that range and corrects them.
  1965.      *
  1966.      * @param  string|int    $expiration    The dirty expiration time.
  1967.      * @return string|int                   The sanitized expiration time.
  1968.      */
  1969.     public function sanitize_expiration( $expiration ) {
  1970.         if ( $expiration > $this->thirty_days && $expiration <= $this->now ) {
  1971.             $expiration = $expiration + $this->now;
  1972.         }
  1973.  
  1974.         return $expiration;
  1975.     }
  1976.  
  1977.     /**
  1978.      * Concatenates two values and casts to type of the first value.
  1979.      *
  1980.      * This is used in append and prepend operations to match how these functions are handled
  1981.      * by memcached. In both cases, whichever value is the original value in the combined value
  1982.      * will dictate the type of the combined value.
  1983.      *
  1984.      * @param   mixed       $original   Original value that dictates the combined type.
  1985.      * @param   mixed       $pended     Value to combine with original value.
  1986.      * @param   string      $direction  Either 'pre' or 'app'.
  1987.      * @return  mixed                   Combined value casted to the type of the first value.
  1988.      */
  1989.     public function combine_values( $original, $pended, $direction ) {
  1990.         $type = gettype( $original );
  1991.  
  1992.         // Combine the values based on direction of the "pend"
  1993.         if ( 'pre' == $direction )
  1994.             $combined = $pended . $original;
  1995.         else
  1996.             $combined = $original . $pended;
  1997.  
  1998.         // Cast type of combined value
  1999.         settype( $combined, $type );
  2000.  
  2001.         return $combined;
  2002.     }
  2003.  
  2004.     /**
  2005.      * Simple wrapper for saving object to the internal cache.
  2006.      *
  2007.      * @param   string      $derived_key    Key to save value under.
  2008.      * @param   mixed       $value          Object value.
  2009.      */
  2010.     public function add_to_internal_cache( $derived_key, $value ) {
  2011.         if ( is_object( $value ) ) {
  2012.             $value = clone $value;
  2013.         }
  2014.  
  2015.         $this->cache[$derived_key] = $value;
  2016.     }
  2017.  
  2018.     /**
  2019.      * Determines if a no_mc_group exists in a group of groups.
  2020.      *
  2021.      * @param   mixed   $groups     The groups to search.
  2022.      * @return  bool                True if a no_mc_group is present; false if a no_mc_group is not present.
  2023.      */
  2024.     public function contains_no_mc_group( $groups ) {
  2025.         if ( is_scalar( $groups ) )
  2026.             return in_array( $groups, $this->no_mc_groups );
  2027.  
  2028.         if ( ! is_array( $groups ) )
  2029.             return false;
  2030.  
  2031.         foreach ( $groups as $group ) {
  2032.             if ( in_array( $group, $this->no_mc_groups ) )
  2033.                 return true;
  2034.         }
  2035.  
  2036.         return false;
  2037.     }
  2038.  
  2039.     /**
  2040.      * Add global groups.
  2041.      *
  2042.      * @author  Ryan Boren   This function comes straight from the original WP Memcached Object cache
  2043.      * @link    http://wordpress.org/extend/plugins/memcached/
  2044.      *
  2045.      * @param   array       $groups     Array of groups.
  2046.      * @return  void
  2047.      */
  2048.     public function add_global_groups( $groups ) {
  2049.         if ( ! is_array( $groups ) )
  2050.             $groups = (array) $groups;
  2051.  
  2052.         $this->global_groups = array_merge( $this->global_groups, $groups);
  2053.         $this->global_groups = array_unique( $this->global_groups );
  2054.     }
  2055.  
  2056.     /**
  2057.      * Add non-persistent groups.
  2058.      *
  2059.      * @author  Ryan Boren   This function comes straight from the original WP Memcached Object cache
  2060.      * @link    http://wordpress.org/extend/plugins/memcached/
  2061.      *
  2062.      * @param   array       $groups     Array of groups.
  2063.      * @return  void
  2064.      */
  2065.     public function add_non_persistent_groups( $groups ) {
  2066.         if ( ! is_array( $groups ) )
  2067.             $groups = (array) $groups;
  2068.  
  2069.         $this->no_mc_groups = array_merge( $this->no_mc_groups, $groups );
  2070.         $this->no_mc_groups = array_unique( $this->no_mc_groups );
  2071.     }
  2072.  
  2073.     /**
  2074.      * Get a value specifically from the internal, run-time cache, not memcached.
  2075.      *
  2076.      * @param   int|string  $key        Key value.
  2077.      * @param   int|string  $group      Group that the value belongs to.
  2078.      * @return  bool|mixed              Value on success; false on failure.
  2079.      */
  2080.     public function get_from_runtime_cache( $key, $group ) {
  2081.         $derived_key = $this->buildKey( $key, $group );
  2082.  
  2083.         if ( isset( $this->cache[$derived_key] ) )
  2084.             return $this->cache[$derived_key];
  2085.  
  2086.         return false;
  2087.     }
  2088.  
  2089.     /**
  2090.      * Switch blog prefix, which changes the cache that is accessed.
  2091.      *
  2092.      * @param  int     $blog_id    Blog to switch to.
  2093.      * @return void
  2094.      */
  2095.     public function switch_to_blog( $blog_id ) {
  2096.         global $table_prefix;
  2097.         $blog_id           = (int) $blog_id;
  2098.         $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
  2099.     }
  2100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement