Example #1
0
 /**
  * @param string $type
  * @param string $namespace
  *
  * @throws \InvalidArgumentException
  * @internal param string $storeId
  */
 public function __construct($type = CacheTypes::ARRAY_CACHE, $namespace = null)
 {
     if (!CacheTypes::contains($type)) {
         throw new \InvalidArgumentException('The $type "' . $type . '" is not valid.');
     }
     $_class = static::STORE_NAMESPACE . $type . 'Cache';
     $_mirror = new \ReflectionClass($_class);
     $this->_store = $_mirror->getConstructor() ? $_mirror->newInstanceArgs($this->_getCacheTypeArguments($type)) : $_mirror->newInstance();
     if (null !== $namespace) {
         $this->_store->setNamespace($namespace);
     }
     $this->_initializeCache($type);
 }
Example #2
0
 public function testFlushAllAndNamespaceVersioningBetweenCaches()
 {
     foreach (static::$_testTypes as $_type => $_tests) {
         if (!$this->isSharedStorage($_type)) {
             echo 'The store type of "' . CacheTypes::prettyNameOf($_type) . '" does not use shared storage' . PHP_EOL;
             continue;
         }
         $cache1 = $this->_getDriver($_type);
         $cache2 = $this->_getDriver($_type);
         /* Deleting all elements from the first provider should increment its
          * namespace version before saving the first entry.
          */
         $cache1->deleteAll();
         $this->assertTrue($cache1->save('key1', 1));
         /* The second provider will be initialized with the same namespace
          * version upon its first save operation.
          */
         $this->assertTrue($cache2->save('key2', 2));
         /* Both providers have the same namespace version and can see entires
          * set by each other.
          */
         $this->assertTrue($cache1->contains('key1'));
         $this->assertTrue($cache1->contains('key2'));
         $this->assertTrue($cache2->contains('key1'));
         $this->assertTrue($cache2->contains('key2'));
         /* Flushing all entries through one cache will remove all entries from
          * the cache but leave their namespace version as-is.
          */
         $this->assertTrue($cache1->flushAll());
         $this->assertFalse($cache1->contains('key1'));
         $this->assertFalse($cache1->contains('key2'));
         $this->assertFalse($cache2->contains('key1'));
         $this->assertFalse($cache2->contains('key2'));
         /* Inserting a new entry will use the same, incremented namespace
          * version, and it will be visible to both providers.
          */
         $this->assertTrue($cache1->save('key1', 1));
         $this->assertTrue($cache1->contains('key1'));
         $this->assertTrue($cache2->contains('key1'));
         /* A new cache provider will be initialized with the original namespace
          * version and not share any visibility with the first two providers.
          */
         $cache3 = $this->_getDriver($_type);
         $this->assertFalse($cache3->contains('key1'));
         $this->assertFalse($cache3->contains('key2'));
         $this->assertTrue($cache3->save('key3', 3));
         $this->assertTrue($cache3->contains('key3'));
     }
 }