MediaWiki master
APCUBagOStuff.php
Go to the documentation of this file.
1<?php
25
44 private const KEY_SUFFIX = ':5';
45
47 private static $CAS_MAX_ATTEMPTS = 100;
48
49 public function __construct( array $params = [] ) {
50 // No use in segmenting values
51 $params['segmentationSize'] = INF;
52 parent::__construct( $params );
53 // Versions of apcu < 5.1.19 use apc.use_request_time=1 by default, causing new keys
54 // to be assigned timestamps based on the start of the PHP request/script. The longer
55 // the request has been running, the more likely that newly stored keys will instantly
56 // be seen as expired by other requests. Disable apc.use_request_time.
57 ini_set( 'apc.use_request_time', '0' );
58
59 if ( PHP_SAPI === 'cli' ) {
60 $this->attrMap[self::ATTR_DURABILITY] = ini_get( 'apc.enable_cli' )
63 } else {
65 }
66 }
67
68 protected function doGet( $key, $flags = 0, &$casToken = null ) {
69 $getToken = ( $casToken === self::PASS_BY_REF );
70 $casToken = null;
71
72 $value = apcu_fetch( $key . self::KEY_SUFFIX );
73 if ( $getToken && $value !== false ) {
74 // Note that if the driver handles serialization then this uses the PHP value
75 // as the token. This might require inspection or re-serialization in doCas().
76 $casToken = $value;
77 }
78
79 return $value;
80 }
81
82 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
83 $ttl = $this->getExpirationAsTTL( $exptime );
84
85 return apcu_store( $key . self::KEY_SUFFIX, $value, $ttl );
86 }
87
88 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
89 if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
90 // Avoid global write locks for high contention keys
91 return false;
92 }
93
94 $ttl = $this->getExpirationAsTTL( $exptime );
95
96 return apcu_add( $key . self::KEY_SUFFIX, $value, $ttl );
97 }
98
99 protected function doDelete( $key, $flags = 0 ) {
100 apcu_delete( $key . self::KEY_SUFFIX );
101
102 return true;
103 }
104
105 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
106 // Use apcu 5.1.12 $ttl argument if apcu_inc() will initialize to $init:
107 // https://www.php.net/manual/en/function.apcu-inc.php
108 if ( $step === $init ) {
110 $ttl = $this->getExpirationAsTTL( $exptime );
111 $result = apcu_inc( $key . self::KEY_SUFFIX, $step, $success, $ttl );
112 } else {
113 $result = false;
114 for ( $i = 0; $i < self::$CAS_MAX_ATTEMPTS; ++$i ) {
115 $oldCount = apcu_fetch( $key . self::KEY_SUFFIX );
116 if ( $oldCount === false ) {
117 $count = $init;
118 $ttl = $this->getExpirationAsTTL( $exptime );
119 if ( apcu_add( $key . self::KEY_SUFFIX, $count, $ttl ) ) {
120 $result = $count;
121 break;
122 }
123 } elseif ( is_int( $oldCount ) ) {
124 $count = $oldCount + $step;
125 if ( apcu_cas( $key . self::KEY_SUFFIX, $oldCount, $count ) ) {
126 $result = $count;
127 break;
128 }
129 } else {
130 break;
131 }
132 }
133 }
134
135 return $result;
136 }
137}
138
140class_alias( APCUBagOStuff::class, 'APCUBagOStuff' );
array $params
The job parameters.
This is a wrapper for APCu's shared memory functions.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
doDelete( $key, $flags=0)
Delete an item.
doGet( $key, $flags=0, &$casToken=null)
Get an item.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
Storage medium specific cache for storing items (e.g.
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.
const PASS_BY_REF
Idiom for doGet() to return extra information by reference.
const QOS_DURABILITY_SERVICE
Data is lost once the service storing the data restarts.
const QOS_DURABILITY_SCRIPT
Data is lost at the end of the current web request or CLI script.
const QOS_DURABILITY_NONE
Data is never saved to begin with (blackhole store)
const ATTR_DURABILITY
Durability of writes; see QOS_DURABILITY_* (higher means stronger)