3 * Object caching using WinCache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Wrapper for WinCache object caching functions; identical interface
30 class WinCacheBagOStuff
extends BagOStuff
{
31 protected function doGet( $key, $flags = 0 ) {
32 $val = wincache_ucache_get( $key );
33 if ( is_string( $val ) ) {
34 $val = unserialize( $val );
40 public function set( $key, $value, $expire = 0, $flags = 0 ) {
41 $result = wincache_ucache_set( $key, serialize( $value ), $expire );
43 return ( $result === [] ||
$result === true );
46 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
47 $result = wincache_ucache_add( $key, serialize( $value ), $exptime );
49 return ( $result === [] ||
$result === true );
52 public function delete( $key, $flags = 0 ) {
53 wincache_ucache_delete( $key );
58 public function merge( $key, callable
$callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
59 if ( wincache_lock( $key ) ) { // optimize with FIFO lock
60 $ok = $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
61 wincache_unlock( $key );
70 * Construct a cache key.
73 * @param string $keyspace
77 public function makeKeyInternal( $keyspace, $args ) {
78 // WinCache keys have a maximum length of 150 characters. From that,
79 // subtract the number of characters we need for the keyspace and for
80 // the separator character needed for each argument. To handle some
81 // custom prefixes used by thing like WANObjectCache, limit to 125.
82 // NOTE: Same as in memcached, except the max key length there is 255.
83 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
86 function ( $arg ) use ( &$charsLeft ) {
87 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
88 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
89 $arg = '#' . md5( $arg );
92 $charsLeft -= strlen( $arg );
98 if ( $charsLeft < 0 ) {
99 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
102 return $keyspace . ':' . implode( ':', $args );
106 * Increase stored value of $key by $value while preserving its original TTL
107 * @param string $key Key to increase
108 * @param int $value Value to add to $key (Default 1)
109 * @return int|bool New value or false on failure
111 public function incr( $key, $value = 1 ) {
112 if ( !$this->lock( $key ) ) {
115 $n = $this->get( $key );
116 if ( $this->isInteger( $n ) ) { // key exists?
117 $n +
= intval( $value );
118 $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
119 $this->set( $key, max( 0, $n ), $oldTTL );
123 $this->unlock( $key );