Example #1
1
 public function set($key, $value, $ttl)
 {
     if (is_array($key)) {
         apcu_store($key, null, $ttl);
     } else {
         apcu_store($key, $value, $ttl);
     }
 }
Example #2
0
 protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
 {
     if ($ttl < 0) {
         return false;
     }
     return apcu_store($key, $item->get(), $ttl);
 }
Example #3
0
 /**
  * Set a value to cache with id and lifetime
  *
  *     $data = 'bar';
  *
  *     // Set 'bar' to 'foo' in apc group, using default expiry
  *     Cache::instance('apc')->set('foo', $data);
  *
  *     // Set 'bar' to 'foo' in apc group for 30 seconds
  *     Cache::instance('apc')->set('foo', $data, 30);
  *
  * @param   string $id id of cache entry
  * @param   string $data data to set to cache
  * @param   integer $lifetime lifetime in seconds
  * @return  boolean
  */
 public function set($id, $data, $lifetime = NULL)
 {
     if ($lifetime === NULL) {
         $lifetime = $this->default_expire;
     }
     return \apcu_store($this->_sanitize_id($id), $data, $lifetime);
 }
Example #4
0
 public function save($objectId, $object, $expiration = null)
 {
     $value = apcu_store($this->encode($this->apcNameSpace(), $objectId), $object, $expiration === null ? $this->expire() : $expiration);
     if ($value === false) {
         throw new WURFL_Storage_Exception("Error saving variable in APC cache. Cache may be full.");
     }
 }
Example #5
0
 public function store($key, $value)
 {
     if (apcu_store($key, $value)) {
         apcu_inc('__known_apcu_size', strlen($value));
     }
     return false;
 }
Example #6
0
 public function strongAdd(string $key, string $value, int $life = 3600) : bool
 {
     if ($life == 0) {
         $life = null;
     }
     return apcu_store($key, $value, $life);
 }
Example #7
0
 /**
  * @param string $key
  * @param mixed $item
  * @return bool
  */
 protected static function cacheStore($key, $item)
 {
     if (self::apcuExists()) {
         return apcu_store($key, $item);
     }
     return false;
 }
 /**
  * Test purging the apcu cache store.
  */
 public function test_purge()
 {
     if (!cachestore_apcu::are_requirements_met()) {
         $this->markTestSkipped('Could not test cachestore_apcu. Requirements are not met.');
     }
     $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
     $instance = cachestore_apcu::initialise_unit_test_instance($definition);
     // Test a simple purge return.
     $this->assertTrue($instance->purge());
     // Test purge works.
     $this->assertTrue($instance->set('test', 'monster'));
     $this->assertSame('monster', $instance->get('test'));
     $this->assertTrue($instance->purge());
     $this->assertFalse($instance->get('test'));
     // Test purge with custom data.
     $this->assertTrue($instance->set('test', 'monster'));
     $this->assertSame('monster', $instance->get('test'));
     $this->assertTrue(apcu_store('test', 'pirate', 180));
     $this->assertSame('monster', $instance->get('test'));
     $this->assertTrue(apcu_exists('test'));
     $this->assertSame('pirate', apcu_fetch('test'));
     // Purge and check that our data is gone but the the custom data is still there.
     $this->assertTrue($instance->purge());
     $this->assertFalse($instance->get('test'));
     $this->assertTrue(apcu_exists('test'));
     $this->assertSame('pirate', apcu_fetch('test'));
 }
Example #9
0
 /**
  * apcu library 5.x for php 7 have no more functions apc_*
  * Uses apcu_* instead
  */
 public static function apc_store($key, $var, $ttl)
 {
     if (self::isPhp7()) {
         return apcu_store($key, $var, $ttl);
     } else {
         return apc_store($key, $var, $ttl);
     }
 }
Example #10
0
 /**
  * Store the value in the apc memory
  *
  * @false string $key
  * @false mix $value
  * @param $key
  * @param $value
  * @throws \Exception
  * @return bool
  */
 public function store($key, $value, $minute = null)
 {
     if (is_null($key) || $key == "") {
         throw new \InvalidArgumentException("Key shouldn't be empty");
     }
     $time = is_null($minute) ? $this->getLifeTime() : $minute * 60;
     return $this->isApcUEnabled ? apcu_store($key, $value, $time) : apc_store($key, $value, $time);
 }
Example #11
0
 public function testAPCUIterator()
 {
     $key = __CLASS__;
     $this->assertTrue(apcu_store($key, 456));
     $entries = iterator_to_array(new \APCUIterator('/^' . preg_quote($key, '/') . '$/', APC_ITER_KEY | APC_ITER_VALUE));
     $this->assertSame(array($key), array_keys($entries));
     $this->assertSame($key, $entries[$key]['key']);
     $this->assertSame(456, $entries[$key]['value']);
 }
Example #12
0
 /**
  * Put a key/value set in the cache.
  * @see apc_store()
  *
  * @param mixed   $key
  * @param mixed   $value
  * @param integer $ttl   (in seconds)
  *
  * @return boolean
  */
 public function put($key, $value, $ttl = 0)
 {
     if (function_exists('apcu_store')) {
         return apcu_store($key, $value, $ttl);
     } elseif (function_exists('apc_store')) {
         return apc_store($key, $value, $ttl);
     }
     return false;
 }
Example #13
0
 public static function store($k, $v, $ttl = 300)
 {
     if (function_exists('apc_store')) {
         return apc_store($k, $v, $ttl);
     } elseif (function_exists('apcu_store')) {
         return apcu_store($k, $v, $ttl);
     }
     return false;
 }
Example #14
0
 public function set($name, $value, $ttl = 0)
 {
     if ($this->hasAPCu) {
         apcu_store($name, $value, $ttl);
     } else {
         $_SESSION[$name] = $value;
     }
     return true;
 }
Example #15
0
 public function setDataStore($key, $data, $timeCache = null)
 {
     if (!empty($timeCache)) {
         apcu_store($key, $data, $timeCache);
     } else {
         apcu_store($key, $data, $this->timeCache);
     }
     return true;
 }
Example #16
0
 /**
  * {@inheritdoc}
  */
 protected function storeItemInCache(CacheItemInterface $item, $ttl)
 {
     if ($this->skipIfCli()) {
         return false;
     }
     if ($ttl < 0) {
         return false;
     }
     return apcu_store($item->getKey(), $item->get(), $ttl);
 }
Example #17
0
 public function __construct($id, $url)
 {
     $this->content = apcu_fetch('cachecontrol_' . $id);
     if (!$this->content || isset($_GET['nocache'])) {
         $api = new Github\Api();
         $response = $api->get($url);
         $this->content = $api->decode($response);
         apcu_store('cachecontrol_' . $id, $this->content, 300);
     }
 }
Example #18
0
 /**
  * {@inheritdoc }
  */
 public function set($key, $value, $ttl = null)
 {
     $tKey = $this->getKey($key);
     $tValue = $this->serialize($value);
     if (!$ttl) {
         $ttl = $this->ttl;
     }
     if (!($this->apcu ? \apcu_store($tKey, $tValue, $ttl) : \apc_store($tKey, $tValue, $ttl))) {
         throw new ApcCacheException('Error saving data with the key "' . $key . '" to the APC cache.');
     }
 }
 /**
  * Writes item into the cache.
  * @param  string key
  * @param  mixed  data
  * @param  array  dependencies
  * @return void
  */
 public function write($key, $data, array $dependencies)
 {
     $expire = isset($dependencies[Cache::EXPIRATION]) ? $dependencies[Cache::EXPIRATION] + time() : NULL;
     $slide = isset($dependencies[Cache::SLIDING]) ? $dependencies[Cache::EXPIRATION] : NULL;
     $data = array('data' => $data, 'expire' => $expire, 'slide' => $slide);
     if (static::$usedExtension == "apcu") {
         apcu_store($key, $data);
     } else {
         apc_store($key, $data);
     }
     // TODO: tags
 }
Example #20
0
 function cache_Touch($key, $ttl = 0)
 {
     global $CACHE_STORE_COUNT;
     global $CACHE_FETCH_COUNT;
     $CACHE_STORE_COUNT++;
     $CACHE_FETCH_COUNT++;
     $value = apcu_fetch($key);
     if (is_array($value)) {
         return apcu_store($value, null, $ttl);
     } else {
         return apcu_store($key, $value, $ttl);
     }
 }
Example #21
0
 /**
  * {@inheritdoc}
  */
 protected function doSave(array $values, $lifetime)
 {
     try {
         return array_keys(apcu_store($values, null, $lifetime));
     } catch (\Error $e) {
     } catch (\Exception $e) {
     }
     if (1 === count($values)) {
         // Workaround https://github.com/krakjoe/apcu/issues/170
         apcu_delete(key($values));
     }
     throw $e;
 }
Example #22
0
 /**
  * @inheritdoc
  */
 public function set($key_or_map, $val = null, $ttl = null)
 {
     if (!$this->is_enabled()) {
         return false;
     }
     if (is_string($key_or_map)) {
         $key_or_map = [$key_or_map => $val];
     }
     $ttl = $this->get_ttl($ttl);
     // http://php.net/manual/en/function.apcu-store.php
     $res = apcu_store($key_or_map, null, $ttl);
     return count($res) <= 0;
 }
 /**
  * {@inheritdoc}
  */
 function loadClass($class)
 {
     // Look if the cache has anything for this class.
     if ($file = \apcu_fetch($this->prefix . $class)) {
         if (is_file($file)) {
             require $file;
             return;
         }
         \apcu_delete($this->prefix . $class);
     }
     // Resolve cache miss.
     $api = new LoadClassGetFileInjectedApi($class);
     if ($this->finder->apiFindFile($api, $class)) {
         \apcu_store($this->prefix . $class, $api->getFile());
     }
 }
Example #24
0
 protected static function load()
 {
     $time = filemtime(__FILE__);
     foreach ($files = glob(self::$conf) as $f) {
         $time = max($time, filemtime($f));
     }
     if (($cache = apcu_fetch('app.routes'))[0] >= $time) {
         self::$routes = $cache[1];
     } else {
         $conf = '';
         foreach ($files as $f) {
             $conf .= load_file($f) . "\n";
         }
         apcu_store('app.routes', [$time, self::$routes = self::parse($conf)]);
     }
 }
Example #25
0
 /**
  * Test that the Moodle APCu store doesn't cross paths with other code using APCu as well.
  */
 public function test_cross_application_interaction()
 {
     $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
     $instance = cachestore_apcu::initialise_unit_test_instance($definition);
     // Test purge with custom data.
     $this->assertTrue($instance->set('test', 'monster'));
     $this->assertSame('monster', $instance->get('test'));
     $this->assertTrue(apcu_store('test', 'pirate', 180));
     $this->assertSame('monster', $instance->get('test'));
     $this->assertTrue(apcu_exists('test'));
     $this->assertSame('pirate', apcu_fetch('test'));
     // Purge and check that our data is gone but the the custom data is still there.
     $this->assertTrue($instance->purge());
     $this->assertFalse($instance->get('test'));
     $this->assertTrue(apcu_exists('test'));
     $this->assertSame('pirate', apcu_fetch('test'));
 }
 /**
  * @param string[] $toBeAdded
  * @param string[] $toBeRemoved
  *
  * @return string[]
  */
 protected function updateClassFiles($toBeAdded, $toBeRemoved)
 {
     $class_files = $toBeAdded;
     // Other requests may have already written to the cache, so we get an up to
     // date version.
     $cached = \apcu_fetch($this->prefix);
     if (!empty($cached)) {
         $class_files += $cached;
         foreach ($toBeRemoved as $class => $file) {
             if (isset($class_files[$class]) && $class_files[$class] === $file) {
                 unset($class_files[$class]);
             }
         }
     }
     \apcu_store($this->prefix, $class_files);
     return $class_files;
 }
Example #27
0
 public function file($id, $file, $format = '', $readCache = true, $writeCache = true)
 {
     if (!is_string($id)) {
         throw new \InvalidArgumentException('Expected string as first parameter.', E_ERROR);
     }
     if (apcu_exists($id)) {
         if ($readCache === true && $this->settingCache === true) {
             return apcu_fetch($id);
         }
     }
     switch ($format) {
         case 'yml':
             $data = \Symfony\Component\Yaml\Yaml::parse($file);
             break;
         default:
             throw new \Exception(sprintf('Requested format "%s" is not supported, refer to manual for supported formats.', $format), E_ERROR);
     }
     if ($writeCache === true && $this->settingCache === true) {
         apcu_store($id, $data);
     }
     return $data;
 }
Example #28
-1
 public function set($key, $data, $timeout = null)
 {
     if ($this->apcu) {
         return apcu_store((string) $key, $data, $timeout);
     }
     return apc_store((string) $key, $data, $timeout);
 }
Example #29
-1
 /**
  * {@inheritdoc}
  */
 public function set($name, $data, $lifetime)
 {
     if ($this->driver == self::APCU_DRIVER) {
         return apcu_store($this->prefix . $name, $data, $lifetime);
     }
     return apc_store($this->prefix . $name, $data, $lifetime);
 }
Example #30
-1
 /**
  * Save cache item
  *
  * @param ICacheItem $item
  * @param int|null   $ttl
  *
  * @return bool
  */
 public function saveItem(ICacheItem $item, $ttl = null)
 {
     if (function_exists('apcu_store')) {
         return apcu_store($item->getKey(), $item->get(), (int) $ttl);
     }
     return apc_store($item->getKey(), $item->get(), (int) $ttl);
 }