3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 namespace Wikimedia\ObjectCache
;
23 use InvalidArgumentException
;
27 * Store data in a memcached server or memcached cluster.
29 * This is a base class for MemcachedPhpBagOStuff and MemcachedPeclBagOStuff.
33 abstract class MemcachedBagOStuff
extends MediumSpecificBagOStuff
{
34 /** @var string Routing prefix appended to keys during operations */
35 protected $routingPrefix;
38 * @param array $params Additional parameters include:
39 * - routingPrefix: a routing prefix of the form "<datacenter>/<cluster>/" used to convey
40 * the location/strategy to use for handling keys accessed from this instance. The prefix
41 * is prepended to keys during cache operations. The memcached proxy must preserve these
42 * prefixes in any responses that include requested keys (e.g. get/gets). The proxy is
43 * also assumed to strip the routing prefix from the stored key name, which allows for
44 * unprefixed access. This can be used with mcrouter. [optional]
46 public function __construct( array $params ) {
47 $params['segmentationSize'] ??
= 917_504
; // < 1MiB
48 parent
::__construct( $params );
50 $this->routingPrefix
= $params['routingPrefix'] ??
'';
52 // ...and does not use special disk-cache plugins
53 $this->attrMap
[self
::ATTR_DURABILITY
] = self
::QOS_DURABILITY_SERVICE
;
60 * @see BagOStuff::makeKeyInternal
62 * @param string $keyspace
63 * @param string[]|int[] $components
67 protected function makeKeyInternal( $keyspace, $components ) {
68 // Memcached keys have a maximum length of 255 characters. From that,
69 // subtract the number of characters we need for the keyspace and for
70 // the separator character needed for each argument. To handle some
71 // custom prefixes used by thing like WANObjectCache, limit to 205.
72 $charsLeft = 205 - strlen( $keyspace ) - count( $components );
74 foreach ( $components as &$component ) {
75 $component = strtr( $component, ' ', '_' );
77 // Make sure %, #, and non-ASCII chars are escaped
78 $component = preg_replace_callback(
79 '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
80 static function ( $m ) {
81 return rawurlencode( $m[0] );
86 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
87 if ( $charsLeft > 33 && strlen( $component ) > $charsLeft ) {
88 $component = '#' . md5( $component );
91 $charsLeft -= strlen( $component );
94 if ( $charsLeft < 0 ) {
95 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $components ) );
98 return $keyspace . ':' . implode( ':', $components );
101 protected function requireConvertGenericKey(): bool {
106 * Ensure that a key is safe to use (contains no control characters and no
107 * characters above the ASCII range.)
114 public function validateKeyEncoding( $key ) {
115 if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
116 throw new InvalidArgumentException( "Key contains invalid characters: $key" );
127 protected function validateKeyAndPrependRoute( $key ) {
128 $this->validateKeyEncoding( $key );
130 if ( $this->routingPrefix
=== '' ) {
134 if ( $key[0] === '/' ) {
135 throw new RuntimeException( "Key '$key' already contains a route." );
138 return $this->routingPrefix
. $key;
146 protected function stripRouteFromKey( $key ) {
147 if ( $this->routingPrefix
=== '' ) {
151 if ( str_starts_with( $key, $this->routingPrefix
) ) {
152 return substr( $key, strlen( $this->routingPrefix
) );
159 * @param int|float $exptime
163 protected function fixExpiry( $exptime ) {
164 if ( $exptime < 0 ) {
165 // The PECL driver does not seem to like negative relative values
166 $expiresAt = $this->getCurrentTime() +
$exptime;
167 } elseif ( $this->isRelativeExpiration( $exptime ) ) {
168 // TTLs higher than 30 days will be detected as absolute TTLs
169 // (UNIX timestamps), and will result in the cache entry being
170 // discarded immediately because the expiry is in the past.
171 // Clamp expires >30d at 30d, unless they're >=1e9 in which
172 // case they are likely to really be absolute (1e9 = 2011-09-09)
173 $expiresAt = min( $exptime, self
::TTL_MONTH
);
175 $expiresAt = $exptime;
178 return (int)$expiresAt;
181 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
182 if ( $flags & self
::WRITE_BACKGROUND
) {
183 return $this->doIncrWithInitAsync( $key, $exptime, $step, $init );
185 return $this->doIncrWithInitSync( $key, $exptime, $step, $init );
191 * @param int $exptime
195 * @return bool True on success, false on failure
197 abstract protected function doIncrWithInitAsync( $key, $exptime, $step, $init );
201 * @param int $exptime
205 * @return int|bool New value or false on failure
207 abstract protected function doIncrWithInitSync( $key, $exptime, $step, $init );
210 /** @deprecated class alias since 1.43 */
211 class_alias( MemcachedBagOStuff
::class, 'MemcachedBagOStuff' );