38 private $nativeSerialize;
45 private const KEY_SUFFIX =
':4';
48 private static $CAS_MAX_ATTEMPTS = 100;
52 $params[
'segmentationSize'] = INF;
53 parent::__construct( $params );
55 $this->nativeSerialize = ( ini_get(
'apc.serializer' ) !==
'default' );
59 ini_set(
'apc.use_request_time',
'0' );
61 if ( PHP_SAPI ===
'cli' ) {
62 $this->attrMap[self::ATTR_DURABILITY] = ini_get(
'apc.enable_cli' )
63 ? self::QOS_DURABILITY_SCRIPT
64 : self::QOS_DURABILITY_NONE;
66 $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SERVICE;
70 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
71 $getToken = ( $casToken === self::PASS_BY_REF );
74 $blob = apcu_fetch( $key . self::KEY_SUFFIX );
76 if ( $getToken && $value !==
false ) {
85 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
88 return apcu_store( $key . self::KEY_SUFFIX,
$blob, $ttl );
91 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
92 if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
98 return apcu_add( $key . self::KEY_SUFFIX,
$blob, $ttl );
102 apcu_delete( $key . self::KEY_SUFFIX );
107 public function incr( $key, $value = 1, $flags = 0 ) {
109 $value = (int)$value;
112 for ( $i = 0; $i < self::$CAS_MAX_ATTEMPTS; ++$i ) {
113 $oldCount = apcu_fetch( $key . self::KEY_SUFFIX );
114 if ( !is_int( $oldCount ) ) {
117 $count = $oldCount + $value;
118 if ( apcu_cas( $key . self::KEY_SUFFIX, $oldCount, $count ) ) {
127 public function decr( $key, $value = 1, $flags = 0 ) {
129 $value = (int)$value;
132 for ( $i = 0; $i < self::$CAS_MAX_ATTEMPTS; ++$i ) {
133 $oldCount = apcu_fetch( $key . self::KEY_SUFFIX );
134 if ( !is_int( $oldCount ) ) {
137 $count = $oldCount - $value;
138 if ( apcu_cas( $key . self::KEY_SUFFIX, $oldCount, $count ) ) {
150 if ( $step === $init ) {
153 $result = apcu_inc( $key . self::KEY_SUFFIX, $step,
$success, $ttl );
156 for ( $i = 0; $i < self::$CAS_MAX_ATTEMPTS; ++$i ) {
157 $oldCount = apcu_fetch( $key . self::KEY_SUFFIX );
158 if ( $oldCount ===
false ) {
161 if ( apcu_add( $key . self::KEY_SUFFIX, $count, $ttl ) ) {
165 } elseif ( is_int( $oldCount ) ) {
166 $count = $oldCount + $step;
167 if ( apcu_cas( $key . self::KEY_SUFFIX, $oldCount, $count ) ) {
182 return $this->nativeSerialize
184 : parent::setNewPreparedValues( $valueByKey );
unserialize( $serialized)
This is a wrapper for APCu's shared memory functions.
setNewPreparedValues(array $valueByKey)
Stage a set of new key values for storage and estimate the amount of bytes needed.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
doDelete( $key, $flags=0)
Delete an item.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
__construct(array $params=[])
doGet( $key, $flags=0, &$casToken=null)
Get an item.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
string $keyspace
Default keyspace; used by makeKey()
Storage medium specific cache for storing items (e.g.
getSerialized( $value, $key)
Get the serialized form a value, using any applicable prepared value.
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.
guessSerialSizeOfValues(array $values)
Estimate the size of a each variable once serialized.