Merge "Change the autonym of Manipuri to Meetei script"
[lhc/web/wiklou.git] / includes / libs / objectcache / WinCacheBagOStuff.php
1 <?php
2 /**
3 * Object caching using WinCache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Wrapper for WinCache object caching functions; identical interface
26 * to the APC wrapper
27 *
28 * @ingroup Cache
29 */
30 class WinCacheBagOStuff extends MediumSpecificBagOStuff {
31 public function __construct( array $params = [] ) {
32 $params['segmentationSize'] = $params['segmentationSize'] ?? INF;
33 parent::__construct( $params );
34 }
35
36 protected function doGet( $key, $flags = 0, &$casToken = null ) {
37 $casToken = null;
38
39 $blob = wincache_ucache_get( $key );
40 if ( !is_string( $blob ) && !is_int( $blob ) ) {
41 return false;
42 }
43
44 $value = $this->unserialize( $blob );
45 if ( $value !== false ) {
46 $casToken = (string)$blob; // don't bother hashing this
47 }
48
49 return $value;
50 }
51
52 protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
53 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
54 return false;
55 }
56
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 );
61 } else {
62 $this->logger->info(
63 __METHOD__ . ' failed due to race condition for {key}.',
64 [ 'key' => $key ]
65 );
66
67 $success = false; // mismatched or failed
68 }
69
70 wincache_unlock( $key );
71
72 return $success;
73 }
74
75 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
76 $result = wincache_ucache_set( $key, $this->serialize( $value ), $exptime );
77
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 );
82 }
83
84 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
85 if ( wincache_ucache_exists( $key ) ) {
86 return false; // avoid warnings
87 }
88
89 $result = wincache_ucache_add( $key, $this->serialize( $value ), $exptime );
90
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 );
95 }
96
97 protected function doDelete( $key, $flags = 0 ) {
98 wincache_ucache_delete( $key );
99
100 return true;
101 }
102
103 /**
104 * Construct a cache key.
105 *
106 * @since 1.27
107 * @param string $keyspace
108 * @param array $args
109 * @return string
110 */
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 );
118
119 $args = array_map(
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 );
124 }
125
126 $charsLeft -= strlen( $arg );
127 return $arg;
128 },
129 $args
130 );
131
132 if ( $charsLeft < 0 ) {
133 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
134 }
135
136 return $keyspace . ':' . implode( ':', $args );
137 }
138
139 /**
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
144 */
145 public function incr( $key, $value = 1 ) {
146 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
147 return false;
148 }
149
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 );
155 } else {
156 $n = false;
157 }
158
159 wincache_unlock( $key );
160
161 return $n;
162 }
163 }