MediaWiki  REL1_31
APCBagOStuff.php
Go to the documentation of this file.
1 <?php
29 class APCBagOStuff extends BagOStuff {
30 
35  protected $nativeSerialize;
36 
42  const KEY_SUFFIX = ':2';
43 
52  public function __construct( array $params = [] ) {
53  parent::__construct( $params );
54 
55  if ( isset( $params['nativeSerialize'] ) ) {
56  $this->nativeSerialize = $params['nativeSerialize'];
57  } elseif ( extension_loaded( 'apcu' ) && ini_get( 'apc.serializer' ) === 'default' ) {
58  // APCu has a memory corruption bug when the serializer is set to 'default'.
59  // See T120267, and upstream bug reports:
60  // - https://github.com/krakjoe/apcu/issues/38
61  // - https://github.com/krakjoe/apcu/issues/35
62  // - https://github.com/krakjoe/apcu/issues/111
63  $this->logger->warning(
64  'The APCu extension is loaded and the apc.serializer INI setting ' .
65  'is set to "default". This can cause memory corruption! ' .
66  'You should change apc.serializer to "php" instead. ' .
67  'See <https://github.com/krakjoe/apcu/issues/38>.'
68  );
69  $this->nativeSerialize = false;
70  } else {
71  $this->nativeSerialize = true;
72  }
73  }
74 
75  protected function doGet( $key, $flags = 0 ) {
76  return $this->getUnserialize(
77  apc_fetch( $key . self::KEY_SUFFIX )
78  );
79  }
80 
81  protected function getUnserialize( $value ) {
82  if ( is_string( $value ) && !$this->nativeSerialize ) {
83  $value = $this->isInteger( $value )
84  ? intval( $value )
85  : unserialize( $value );
86  }
87  return $value;
88  }
89 
90  public function set( $key, $value, $exptime = 0, $flags = 0 ) {
91  apc_store(
92  $key . self::KEY_SUFFIX,
93  $this->setSerialize( $value ),
94  $exptime
95  );
96 
97  return true;
98  }
99 
100  protected function setSerialize( $value ) {
101  if ( !$this->nativeSerialize && !$this->isInteger( $value ) ) {
102  $value = serialize( $value );
103  }
104  return $value;
105  }
106 
107  public function delete( $key ) {
108  apc_delete( $key . self::KEY_SUFFIX );
109 
110  return true;
111  }
112 
113  public function incr( $key, $value = 1 ) {
114  return apc_inc( $key . self::KEY_SUFFIX, $value );
115  }
116 
117  public function decr( $key, $value = 1 ) {
118  return apc_dec( $key . self::KEY_SUFFIX, $value );
119  }
120 }
BagOStuff\isInteger
isInteger( $value)
Check if a value is an integer.
Definition: BagOStuff.php:728
array
the array() calling protocol came about after MediaWiki 1.4rc1.
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:192
$params
$params
Definition: styleTest.css.php:40
serialize
serialize()
Definition: ApiMessage.php:184
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:47
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
APCBagOStuff\decr
decr( $key, $value=1)
Decrease stored value of $key by $value while preserving its TTL.
Definition: APCBagOStuff.php:117
APCBagOStuff\__construct
__construct(array $params=[])
Available parameters are:
Definition: APCBagOStuff.php:52
APCBagOStuff\$nativeSerialize
bool $nativeSerialize
If true, trust the APC implementation to serialize and deserialize objects correctly.
Definition: APCBagOStuff.php:35
APCBagOStuff\getUnserialize
getUnserialize( $value)
Definition: APCBagOStuff.php:81
$value
$value
Definition: styleTest.css.php:45
APCBagOStuff\setSerialize
setSerialize( $value)
Definition: APCBagOStuff.php:100
APCBagOStuff\incr
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
Definition: APCBagOStuff.php:113
APCBagOStuff\doGet
doGet( $key, $flags=0)
Definition: APCBagOStuff.php:75
APCBagOStuff
This is a wrapper for APC's shared memory functions.
Definition: APCBagOStuff.php:29