MediaWiki master
APCUBagOStuff.php
Go to the documentation of this file.
1<?php
7
28 private const KEY_SUFFIX = ':5';
29
31 private const CAS_MAX_ATTEMPTS = 100;
32
33 public function __construct( array $params = [] ) {
34 // No use in segmenting values
35 $params['segmentationSize'] = INF;
36 parent::__construct( $params );
37
38 if ( PHP_SAPI === 'cli' ) {
39 $this->attrMap[self::ATTR_DURABILITY] = ini_get( 'apc.enable_cli' )
40 ? self::QOS_DURABILITY_SCRIPT
41 : self::QOS_DURABILITY_NONE;
42 } else {
43 $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SERVICE;
44 }
45 }
46
48 protected function doGet( $key, $flags = 0, &$casToken = null ) {
49 $getToken = ( $casToken === self::PASS_BY_REF );
50 $casToken = null;
51
52 $value = apcu_fetch( $key . self::KEY_SUFFIX );
53 if ( $getToken && $value !== false ) {
54 // Note that if the driver handles serialization then this uses the PHP value
55 // as the token. This might require inspection or re-serialization in doCas().
56 $casToken = $value;
57 }
58
59 return $value;
60 }
61
63 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
64 $ttl = $this->getExpirationAsTTL( $exptime );
65
66 return apcu_store( $key . self::KEY_SUFFIX, $value, $ttl );
67 }
68
70 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
71 if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
72 // Avoid global write locks for high contention keys
73 return false;
74 }
75
76 $ttl = $this->getExpirationAsTTL( $exptime );
77
78 return apcu_add( $key . self::KEY_SUFFIX, $value, $ttl );
79 }
80
82 protected function doDelete( $key, $flags = 0 ) {
83 apcu_delete( $key . self::KEY_SUFFIX );
84
85 return true;
86 }
87
89 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
90 // Use apcu 5.1.12 $ttl argument if apcu_inc() will initialize to $init:
91 // https://www.php.net/manual/en/function.apcu-inc.php
92 $keyWithSuffix = $key . self::KEY_SUFFIX;
93 if ( $step === $init ) {
95 $ttl = $this->getExpirationAsTTL( $exptime );
96 $result = apcu_inc( $keyWithSuffix, $step, $success, $ttl );
97 } else {
98 $result = false;
99 for ( $i = 0; $i < self::CAS_MAX_ATTEMPTS; ++$i ) {
100 $oldCount = apcu_fetch( $keyWithSuffix );
101 if ( $oldCount === false ) {
102 $count = $init;
103 $ttl = $this->getExpirationAsTTL( $exptime );
104 if ( apcu_add( $keyWithSuffix, $count, $ttl ) ) {
105 $result = $count;
106 break;
107 }
108 } elseif ( is_int( $oldCount ) ) {
109 $count = $oldCount + $step;
110 if ( apcu_cas( $keyWithSuffix, $oldCount, $count ) ) {
111 $result = $count;
112 break;
113 }
114 } else {
115 break;
116 }
117 }
118 }
119
120 return $result;
121 }
122}
123
125class_alias( APCUBagOStuff::class, 'APCUBagOStuff' );
Store data in the local server memory via APCu (php-apcu).
doIncrWithInit( $key, $exptime, $step, $init, $flags)
int|bool New value or false on failure
doDelete( $key, $flags=0)
Delete an item.bool True if the item was deleted or not found, false on failure
doGet( $key, $flags=0, &$casToken=null)
Get an item.The CAS token should be null if the key does not exist or the value is corruptmixed Retur...
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.bool Success
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.bool Success
Helper class that implements most of BagOStuff for a backend.
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.