Skip to content

Commit

Permalink
Merge branch 'MDL-57570-master' of https://github.com/mwehr/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jan 18, 2017
2 parents 3610259 + 7ca1161 commit c5bd22b
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions cache/stores/static/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ class cachestore_static extends static_data_store implements cache_is_key_aware,
* @var bool
*/
protected $simpledata = false;

/**
* The number of items currently being stored.
* @var int
*/
protected $storecount = 0;

/**
* igbinary extension available.
* @var bool
*/
protected $igbinaryfound = false;

/**
* Constructs the store instance.
*
Expand Down Expand Up @@ -203,6 +210,7 @@ public function initialise(cache_definition $definition) {
$this->store = &self::register_store_id($this->storeid);
$maxsize = $definition->get_maxsize();
$this->simpledata = $definition->uses_simple_data();
$this->igbinaryfound = extension_loaded('igbinary');
if ($maxsize !== null) {
// Must be a positive int.
$this->maxsize = abs((int)$maxsize);
Expand All @@ -219,6 +227,41 @@ public function is_initialised() {
return (is_array($this->store));
}

/**
* Uses igbinary serializer if igbinary extension is loaded.
* Fallback to PHP serializer.
*
* @param mixed $data
* The value to be serialized.
* @return string a string containing a byte-stream representation of
* value that can be stored anywhere.
*/
protected function serialize($data) {
if ($this->igbinaryfound) {
return igbinary_serialize($data);
} else {
return serialize($data);
}
}

/**
* Uses igbinary unserializer if igbinary extension is loaded.
* Fallback to PHP unserializer.
*
* @param string $str
* The serialized string.
* @return mixed The converted value is returned, and can be a boolean,
* integer, float, string,
* array or object.
*/
protected function unserialize($str) {
if ($this->igbinaryfound) {
return igbinary_unserialize($str);
} else {
return unserialize($str);
}
}

/**
* Retrieves an item from the cache store given its key.
*
Expand All @@ -233,7 +276,7 @@ public function get($key) {
$key = $key['key'];
if (isset($this->store[$key])) {
if ($this->store[$key]['serialized']) {
return unserialize($this->store[$key]['data']);
return $this->unserialize($this->store[$key]['data']);
} else {
return $this->store[$key]['data'];
}
Expand Down Expand Up @@ -261,7 +304,7 @@ public function get_many($keys) {
$return[$key] = false;
if (isset($this->store[$key])) {
if ($this->store[$key]['serialized']) {
$return[$key] = unserialize($this->store[$key]['data']);
$return[$key] = $this->unserialize($this->store[$key]['data']);
} else {
$return[$key] = $this->store[$key]['data'];
}
Expand Down Expand Up @@ -292,7 +335,7 @@ public function set($key, $data, $testmaxsize = true) {
$this->store[$key]['data'] = $data;
$this->store[$key]['serialized'] = false;
} else {
$this->store[$key]['data'] = serialize($data);
$this->store[$key]['data'] = $this->serialize($data);
$this->store[$key]['serialized'] = true;
}

Expand Down

0 comments on commit c5bd22b

Please sign in to comment.