| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\ObjectCache; |
| 21 |
|
| 22 |
use Exception; |
| 23 |
use InvalidArgumentException; |
| 24 |
use RuntimeException; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
abstract class MemcachedBagOStuff extends MediumSpecificBagOStuff { |
| 34 |
|
| 35 |
protected $routingPrefix; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( array $params ) { |
| 47 |
$params['segmentationSize'] ??= 917_504; |
| 48 |
parent::__construct( $params ); |
| 49 |
|
| 50 |
$this->routingPrefix = $params['routingPrefix'] ?? ''; |
| 51 |
|
| 52 |
|
| 53 |
$this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SERVICE; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
protected function makeKeyInternal( $keyspace, $components ) { |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
$charsLeft = 205 - strlen( $keyspace ) - count( $components ); |
| 73 |
|
| 74 |
foreach ( $components as &$component ) { |
| 75 |
$component = strtr( $component, ' ', '_' ); |
| 76 |
|
| 77 |
|
| 78 |
$component = preg_replace_callback( |
| 79 |
'/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/', |
| 80 |
static function ( $m ) { |
| 81 |
return rawurlencode( $m[0] ); |
| 82 |
}, |
| 83 |
$component |
| 84 |
); |
| 85 |
|
| 86 |
|
| 87 |
if ( $charsLeft > 33 && strlen( $component ) > $charsLeft ) { |
| 88 |
$component = '#' . md5( $component ); |
| 89 |
} |
| 90 |
|
| 91 |
$charsLeft -= strlen( $component ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( $charsLeft < 0 ) { |
| 95 |
return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $components ) ); |
| 96 |
} |
| 97 |
|
| 98 |
return $keyspace . ':' . implode( ':', $components ); |
| 99 |
} |
| 100 |
|
| 101 |
protected function requireConvertGenericKey(): bool { |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function validateKeyEncoding( $key ) { |
| 115 |
if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) { |
| 116 |
throw new InvalidArgumentException( "Key contains invalid characters: $key" ); |
| 117 |
} |
| 118 |
|
| 119 |
return $key; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
protected function validateKeyAndPrependRoute( $key ) { |
| 128 |
$this->validateKeyEncoding( $key ); |
| 129 |
|
| 130 |
if ( $this->routingPrefix === '' ) { |
| 131 |
return $key; |
| 132 |
} |
| 133 |
|
| 134 |
if ( $key[0] === '/' ) { |
| 135 |
throw new RuntimeException( "Key '$key' already contains a route." ); |
| 136 |
} |
| 137 |
|
| 138 |
return $this->routingPrefix . $key; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected function stripRouteFromKey( $key ) { |
| 147 |
if ( $this->routingPrefix === '' ) { |
| 148 |
return $key; |
| 149 |
} |
| 150 |
|
| 151 |
if ( str_starts_with( $key, $this->routingPrefix ) ) { |
| 152 |
return substr( $key, strlen( $this->routingPrefix ) ); |
| 153 |
} |
| 154 |
|
| 155 |
return $key; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
protected function fixExpiry( $exptime ) { |
| 164 |
if ( $exptime < 0 ) { |
| 165 |
|
| 166 |
$expiresAt = $this->getCurrentTime() + $exptime; |
| 167 |
} elseif ( $this->isRelativeExpiration( $exptime ) ) { |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
$expiresAt = min( $exptime, self::TTL_MONTH ); |
| 174 |
} else { |
| 175 |
$expiresAt = $exptime; |
| 176 |
} |
| 177 |
|
| 178 |
return (int)$expiresAt; |
| 179 |
} |
| 180 |
|
| 181 |
protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) { |
| 182 |
if ( $flags & self::WRITE_BACKGROUND ) { |
| 183 |
return $this->doIncrWithInitAsync( $key, $exptime, $step, $init ); |
| 184 |
} else { |
| 185 |
return $this->doIncrWithInitSync( $key, $exptime, $step, $init ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
abstract protected function doIncrWithInitAsync( $key, $exptime, $step, $init ); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
abstract protected function doIncrWithInitSync( $key, $exptime, $step, $init ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
class_alias( MemcachedBagOStuff::class, 'MemcachedBagOStuff' ); |