wp/wp-includes/cache.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Object Cache API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     5
 * @link https://codex.wordpress.org/Function_Reference/WP_Cache
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * @subpackage Cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * Adds data to the cache, if the cache key doesn't already exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * @see WP_Object_Cache::add()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * @param int|string $key The cache key to use for retrieval later
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * @param mixed $data The data to add to the cache store
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * @param string $group The group to add the cache to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * @param int $expire When the cache data should be expired
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * @return bool False if cache key and group already exist, true on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
	return $wp_object_cache->add( $key, $data, $group, (int) $expire );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 * Closes the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 * This function has ceased to do anything since WordPress 2.5. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
 * functionality was removed along with the rest of the persistent cache. This
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
 * does not mean that plugins can't implement this function when they need to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
 * make sure that the cache is cleaned up after WordPress no longer needs it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
 * @return bool Always returns True
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
function wp_cache_close() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
 * Decrement numeric cache item's value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
 * @see WP_Object_Cache::decr()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
 * @param int|string $key The cache key to increment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
 * @param int $offset The amount by which to decrement the item's value. Default is 1.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
 * @param string $group The group the key is in.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
 * @return false|int False on failure, the item's new value on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
function wp_cache_decr( $key, $offset = 1, $group = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
	return $wp_object_cache->decr( $key, $offset, $group );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
 * Removes the cache contents matching key and group.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
 * @see WP_Object_Cache::delete()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
 * @param int|string $key What the contents in the cache are called
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
 * @param string $group Where the cache contents are grouped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
 * @return bool True on successful removal, false on failure
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
function wp_cache_delete($key, $group = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	return $wp_object_cache->delete($key, $group);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
 * Removes all cache items.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
 * @see WP_Object_Cache::flush()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
 * @return bool False on failure, true on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
function wp_cache_flush() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	return $wp_object_cache->flush();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
 * Retrieves the cache contents from the cache by key and group.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
 * @see WP_Object_Cache::get()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
 * @param int|string $key What the contents in the cache are called
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
 * @param string $group Where the cache contents are grouped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
 * @param bool $force Whether to force an update of the local cache from the persistent cache (default is false)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
 * @param bool &$found Whether key was found in the cache. Disambiguates a return of false, a storable value.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
 * @return bool|mixed False on failure to retrieve contents or the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
 *		contents on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
	return $wp_object_cache->get( $key, $group, $force, $found );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
 * Increment numeric cache item's value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
 * @see WP_Object_Cache::incr()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
 * @param int|string $key The cache key to increment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
 * @param int $offset The amount by which to increment the item's value. Default is 1.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
 * @param string $group The group the key is in.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
 * @return false|int False on failure, the item's new value on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
function wp_cache_incr( $key, $offset = 1, $group = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	return $wp_object_cache->incr( $key, $offset, $group );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 * Sets up Object Cache Global and assigns it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
function wp_cache_init() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
	$GLOBALS['wp_object_cache'] = new WP_Object_Cache();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
 * Replaces the contents of the cache with new data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
 * @see WP_Object_Cache::replace()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
 * @param int|string $key What to call the contents in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
 * @param mixed $data The contents to store in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
 * @param string $group Where to group the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
 * @param int $expire When to expire the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
 * @return bool False if not exists, true if contents were replaced
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
	return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
 * Saves the data to the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
 * @uses $wp_object_cache Object Cache Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
 * @see WP_Object_Cache::set()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
 * @param int|string $key What to call the contents in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
 * @param mixed $data The contents to store in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
 * @param string $group Where to group the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
 * @param int $expire When to expire the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
 * @return bool False on failure, true on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
	return $wp_object_cache->set( $key, $data, $group, (int) $expire );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
 * Switch the interal blog id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
 * This changes the blog id used to create keys in blog specific groups.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
 * @param int $blog_id Blog ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
function wp_cache_switch_to_blog( $blog_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
	return $wp_object_cache->switch_to_blog( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
 * Adds a group or set of groups to the list of global groups.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
 * @param string|array $groups A group or an array of groups to add
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
function wp_cache_add_global_groups( $groups ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	return $wp_object_cache->add_global_groups( $groups );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
 * Adds a group or set of groups to the list of non-persistent groups.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 * @param string|array $groups A group or an array of groups to add
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
function wp_cache_add_non_persistent_groups( $groups ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
	// Default cache doesn't persist so nothing to do here.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
 * Reset internal cache keys and structures. If the cache backend uses global
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
 * blog or site IDs as part of its cache keys, this function instructs the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
 * backend to reset those keys and perform any cleanup since blog or site IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
 * have changed since cache init.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
 * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
 * function when preparing the cache for a blog switch. For clearing the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
 * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
 * recommended outside of unit tests as the performance penality for using it is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
 * high.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
 * @deprecated 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
function wp_cache_reset() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
	_deprecated_function( __FUNCTION__, '3.5' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	global $wp_object_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
	return $wp_object_cache->reset();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
 * WordPress Object Cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
 * The WordPress Object Cache is used to save on trips to the database. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
 * Object Cache stores all of the cache data to memory and makes the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
 * contents available by using a key, which is used to name and later retrieve
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
 * the cache contents.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
 * The Object Cache can be replaced by other caching mechanisms by placing files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
 * in the wp-content folder which is looked at in wp-settings. If that file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
 * exists, then this file will not be included.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
 * @subpackage Cache
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
 * @since 2.0.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
class WP_Object_Cache {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
	 * Holds the cached objects
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
	private $cache = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
	 * The amount of times the cache data was already stored in the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
	 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
	private $cache_hits = 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
	 * Amount of times the cache did not have the request in cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
	public $cache_misses = 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	 * List of global groups
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
	 * @access protected
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
	 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
	protected $global_groups = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
	 * The blog prefix to prepend to keys in non-global groups.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
	private $blog_prefix;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
	 * Holds the value of `is_multisite()`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
	 * @var bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
	 * @access private
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
	 * @since 3.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
	private $multisite;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   318
	 * Make private properties readable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
	 * @param string $name Property to get.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   324
	 * @return mixed Property.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   325
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   326
	public function __get( $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   327
		return $this->$name;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   328
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   329
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   330
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   331
	 * Make private properties settable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   332
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   333
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   335
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   336
	 * @param string $name  Property to set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   337
	 * @param mixed  $value Property value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
	 * @return mixed Newly-set property.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
	public function __set( $name, $value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
		return $this->$name = $value;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
	 * Make private properties checkable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   350
	 * @param string $name Property to check if set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
	 * @return bool Whether the property is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   353
	public function __isset( $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
		return isset( $this->$name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   358
	 * Make private properties un-settable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
	 * @param string $name Property to unset.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
	public function __unset( $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
		unset( $this->$name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
	 * Adds data to the cache if it doesn't already exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
	 * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
	 * @uses WP_Object_Cache::set Sets the data after the checking the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
	 *		contents existence.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
	 * @param int|string $key What to call the contents in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
	 * @param mixed $data The contents to store in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	 * @param string $group Where to group the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	 * @param int $expire When to expire the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	 * @return bool False if cache key and group already exist, true on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   384
	public function add( $key, $data, $group = 'default', $expire = 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
		if ( wp_suspend_cache_addition() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
		if ( empty( $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
			$group = 'default';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
		$id = $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
			$id = $this->blog_prefix . $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
		if ( $this->_exists( $id, $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
		return $this->set( $key, $data, $group, (int) $expire );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	 * Sets the list of global groups.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
	 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
	 * @param array $groups List of groups that are global.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   408
	public function add_global_groups( $groups ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
		$groups = (array) $groups;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
		$groups = array_fill_keys( $groups, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		$this->global_groups = array_merge( $this->global_groups, $groups );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
	 * Decrement numeric cache item's value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
	 * @param int|string $key The cache key to increment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
	 * @param int $offset The amount by which to decrement the item's value. Default is 1.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
	 * @param string $group The group the key is in.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	 * @return false|int False on failure, the item's new value on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   425
	public function decr( $key, $offset = 1, $group = 'default' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
		if ( empty( $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
			$group = 'default';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
			$key = $this->blog_prefix . $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
		if ( ! $this->_exists( $key, $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
		if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
			$this->cache[ $group ][ $key ] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
		$offset = (int) $offset;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
		$this->cache[ $group ][ $key ] -= $offset;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
		if ( $this->cache[ $group ][ $key ] < 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
			$this->cache[ $group ][ $key ] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
		return $this->cache[ $group ][ $key ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
	 * Remove the contents of the cache key in the group
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   451
	 * If the cache key does not exist in the group, then nothing will happen.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
	 * @param int|string $key What the contents in the cache are called
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
	 * @param string $group Where the cache contents are grouped
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
	 * @param bool $deprecated Deprecated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
	 * @return bool False if the contents weren't deleted and true on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   461
	public function delete( $key, $group = 'default', $deprecated = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
		if ( empty( $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
			$group = 'default';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
			$key = $this->blog_prefix . $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   468
		if ( ! $this->_exists( $key, $group ) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
		unset( $this->cache[$group][$key] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
	 * Clears the object cache of all data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	 * @return bool Always returns true
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   482
	public function flush() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
		$this->cache = array ();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
	 * Retrieves the cache contents, if it exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
	 * The contents will be first attempted to be retrieved by searching by the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
	 * key in the cache group. If the cache is hit (success) then the contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
	 * are returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
	 * On failure, the number of cache misses will be incremented.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
	 * @param int|string $key What the contents in the cache are called
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
	 * @param string $group Where the cache contents are grouped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
	 * @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
	 * @return bool|mixed False on failure to retrieve contents or the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
	 *		contents on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   505
	public function get( $key, $group = 'default', $force = false, &$found = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
		if ( empty( $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
			$group = 'default';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
			$key = $this->blog_prefix . $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
		if ( $this->_exists( $key, $group ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
			$found = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
			$this->cache_hits += 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
			if ( is_object($this->cache[$group][$key]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
				return clone $this->cache[$group][$key];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
				return $this->cache[$group][$key];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
		$found = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
		$this->cache_misses += 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
	 * Increment numeric cache item's value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
	 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
	 * @param int|string $key The cache key to increment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
	 * @param int $offset The amount by which to increment the item's value. Default is 1.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
	 * @param string $group The group the key is in.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
	 * @return false|int False on failure, the item's new value on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   536
	public function incr( $key, $offset = 1, $group = 'default' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
		if ( empty( $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
			$group = 'default';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
			$key = $this->blog_prefix . $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
		if ( ! $this->_exists( $key, $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
			$this->cache[ $group ][ $key ] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
		$offset = (int) $offset;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		$this->cache[ $group ][ $key ] += $offset;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
		if ( $this->cache[ $group ][ $key ] < 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
			$this->cache[ $group ][ $key ] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
		return $this->cache[ $group ][ $key ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
	 * Replace the contents in the cache, if contents already exist
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
	 * @see WP_Object_Cache::set()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
	 * @param int|string $key What to call the contents in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
	 * @param mixed $data The contents to store in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
	 * @param string $group Where to group the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	 * @param int $expire When to expire the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	 * @return bool False if not exists, true if contents were replaced
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   571
	public function replace( $key, $data, $group = 'default', $expire = 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
		if ( empty( $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
			$group = 'default';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
		$id = $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
			$id = $this->blog_prefix . $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
		if ( ! $this->_exists( $id, $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
		return $this->set( $key, $data, $group, (int) $expire );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
	 * Reset keys
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
	 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
	 * @deprecated 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   591
	public function reset() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
		_deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
		// Clear out non-global caches since the blog ID has changed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
		foreach ( array_keys( $this->cache ) as $group ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
			if ( ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
				unset( $this->cache[ $group ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
	 * Sets the data contents into the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
	 * The cache contents is grouped by the $group parameter followed by the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
	 * $key. This allows for duplicate ids in unique groups. Therefore, naming of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
	 * the group should be used with care and should follow normal function
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
	 * naming guidelines outside of core WordPress usage.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
	 * The $expire parameter is not used, because the cache will automatically
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
	 * expire for each time a page is accessed and PHP finishes. The method is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
	 * more for cache plugins which use files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
	 * @param int|string $key What to call the contents in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
	 * @param mixed $data The contents to store in the cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
	 * @param string $group Where to group the cache contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
	 * @param int $expire Not Used
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
	 * @return bool Always returns true
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   621
	public function set( $key, $data, $group = 'default', $expire = 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
		if ( empty( $group ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
			$group = 'default';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
			$key = $this->blog_prefix . $key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
		if ( is_object( $data ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
			$data = clone $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
		$this->cache[$group][$key] = $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
	 * Echoes the stats of the caching.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
	 * Gives the cache hits, and cache misses. Also prints every cached group,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
	 * key and the data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
	public function stats() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
		echo "<p>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
		echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
		echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
		echo "</p>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
		echo '<ul>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
		foreach ($this->cache as $group => $cache) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
			echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
		echo '</ul>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
	 * Switch the interal blog id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
	 * This changes the blog id used to create keys in blog specific groups.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
	 * @param int $blog_id Blog ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   664
	public function switch_to_blog( $blog_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
		$blog_id = (int) $blog_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
		$this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
	 * Utility function to determine whether a key exists in the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
	 * @access protected
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   675
	 * @param string $key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   676
	 * @param string $group
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   677
	 * @return bool
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
	protected function _exists( $key, $group ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
		return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
	 * Sets up object properties; PHP 5 style constructor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
	 * @since 2.0.8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
	public function __construct() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
		global $blog_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
		$this->multisite = is_multisite();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
		$this->blog_prefix =  $this->multisite ? $blog_id . ':' : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
		 * @todo This should be moved to the PHP4 style constructor, PHP5
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
		 * already calls __destruct()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
		register_shutdown_function( array( $this, '__destruct' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
	 * Will save the object cache before object is completely destroyed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
	 * Called upon object destruction, which should be when PHP ends.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
	 * @since  2.0.8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
	 * @return bool True value. Won't be used by PHP
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   711
	public function __destruct() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
}