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 MediumSpecificBagOStuff
{
31 public function __construct( array $params = [] ) {
32 $params['segmentationSize'] = $params['segmentationSize'] ?? INF
;
33 parent
::__construct( $params );
36 protected function doGet( $key, $flags = 0, &$casToken = null ) {
39 $blob = wincache_ucache_get( $key );
40 if ( !is_string( $blob ) && !is_int( $blob ) ) {
44 $value = $this->unserialize( $blob );
45 if ( $value !== false ) {
46 $casToken = (string)$blob; // don't bother hashing this
52 protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
53 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
57 $curCasToken = null; // passed by reference
58 $this->doGet( $key, self
::READ_LATEST
, $curCasToken );
59 if ( $casToken === $curCasToken ) {
60 $success = $this->set( $key, $value, $exptime, $flags );
63 __METHOD__
. ' failed due to race condition for {key}.',
67 $success = false; // mismatched or failed
70 wincache_unlock( $key );
75 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
76 $result = wincache_ucache_set( $key, $this->serialize( $value ), $exptime );
78 // false positive, wincache_ucache_set returns an empty array
79 // in some circumstances.
80 // @phan-suppress-next-line PhanTypeComparisonToArray
81 return ( $result === [] ||
$result === true );
84 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
85 if ( wincache_ucache_exists( $key ) ) {
86 return false; // avoid warnings
89 $result = wincache_ucache_add( $key, $this->serialize( $value ), $exptime );
91 // false positive, wincache_ucache_add returns an empty array
92 // in some circumstances.
93 // @phan-suppress-next-line PhanTypeComparisonToArray
94 return ( $result === [] ||
$result === true );
97 protected function doDelete( $key, $flags = 0 ) {
98 wincache_ucache_delete( $key );
104 * Construct a cache key.
107 * @param string $keyspace
111 public function makeKeyInternal( $keyspace, $args ) {
112 // WinCache keys have a maximum length of 150 characters. From that,
113 // subtract the number of characters we need for the keyspace and for
114 // the separator character needed for each argument. To handle some
115 // custom prefixes used by thing like WANObjectCache, limit to 125.
116 // NOTE: Same as in memcached, except the max key length there is 255.
117 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
120 function ( $arg ) use ( &$charsLeft ) {
121 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
122 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
123 $arg = '#' . md5( $arg );
126 $charsLeft -= strlen( $arg );
132 if ( $charsLeft < 0 ) {
133 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
136 return $keyspace . ':' . implode( ':', $args );
140 * Increase stored value of $key by $value while preserving its original TTL
141 * @param string $key Key to increase
142 * @param int $value Value to add to $key (Default 1)
143 * @return int|bool New value or false on failure
145 public function incr( $key, $value = 1 ) {
146 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
150 $n = $this->doGet( $key );
151 if ( $this->isInteger( $n ) ) {
152 $n = max( $n +
(int)$value, 0 );
153 $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
154 $this->set( $key, $n, $oldTTL );
159 wincache_unlock( $key );