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