Exemplo n.º 1
0
    public function testFindExpired()
    {
        $capabilities = $this->_storage->getCapabilities();
        if (!$capabilities->getIterable()) {
            $this->markTestSkipped("Find isn't supported by this adapter");
        }

        $this->_options->setTtl($capabilities->getTtlPrecision());

        $this->assertTrue($this->_storage->setItem('key1', 'value1'));
        $this->assertTrue($this->_storage->setItem('key2', 'value2'));

        // wait until first 2 items expired
        usleep($capabilities->getTtlPrecision() * 2000000);

        $this->assertTrue($this->_storage->setItem('key3', 'value3'));
        $this->assertTrue($this->_storage->setItem('key4', 'value4'));

        $this->assertTrue($this->_storage->find(AdapterInterface::MATCH_EXPIRED));

        if ($capabilities->getExpiredRead() && !$capabilities->getUseRequestTime()) {
            $expectedItems = array(
                'key1' => 'value1',
                'key2' => 'value2'
            );
        } else {
            $expectedItems = array();
        }

        $actualItems = array();
        while (($item = $this->_storage->fetch()) !== false) {
            // check $item
            $this->assertArrayHasKey('key', $item);
            $this->assertArrayHasKey('value', $item);
            $this->assertEquals(2, count($item));

            $actualItems[ $item['key'] ] = $item['value'];
        }

        ksort($actualItems);
        $this->assertEquals($expectedItems, $actualItems);
    }
Exemplo n.º 2
0
 /**
  * Saves the given cache
  * Prevents broken cache when write_control is disabled and displays problems by log or error
  *
  * @param  mixed  $data
  * @param  string $id
  * @return boolean Returns false when the cache has not been written
  */
 protected function saveCache($data, $id)
 {
     if (self::$_cacheTags) {
         self::$_cache->setItem($id, $data, array('tags' => array($this->_options['tag'])));
     } else {
         self::$_cache->setItem($id, $data);
     }
     if (!self::$_cache->hasItem($id)) {
         if (!$this->_options['disableNotices']) {
             if ($this->_options['log']) {
                 $this->_options['log']->log($this->_options['logPriority'], "Writing to cache failed.");
             } else {
                 trigger_error("Writing to cache failed.", E_USER_NOTICE);
             }
         }
         self::$_cache->removeItem($id);
         return false;
     }
     return true;
 }
Exemplo n.º 3
0
    /**
     * Returns the items for a given page.
     *
     * @param integer $pageNumber
     * @return mixed
     */
    public function getItemsByPage($pageNumber)
    {
        $pageNumber = $this->normalizePageNumber($pageNumber);

        if ($this->_cacheEnabled()) {
            $data = self::$_cache->getItem($this->_getCacheId($pageNumber));
            if ($data) {
                return $data;
            }
        }

        $offset = ($pageNumber - 1) * $this->getItemCountPerPage();

        $items = $this->_adapter->getItems($offset, $this->getItemCountPerPage());

        $filter = $this->getFilter();

        if ($filter !== null) {
            $items = $filter->filter($items);
        }

        if (!$items instanceof Traversable) {
            $items = new ArrayIterator($items);
        }

        if ($this->_cacheEnabled()) {
            self::$_cache->setItem(
                $this->_getCacheId($pageNumber), 
                $items, 
                array('tags' => array($this->_getCacheInternalId()))
            );
        }

        return $items;
    }