public function testStoreAndRetrieveItemByKey() { $cacheItem = new MCacheItem(); $cacheItem->setKey('key')->setValue('value')->setTtl(100000); $this->assertTrue($this->cacheManager->addItem($cacheItem)); $dbCacheItems = $this->cacheManager->getItems(array('key')); $this->assertEquals($cacheItem, $dbCacheItems[0]); }
public function testToArray() { $array = array('key' => 'key', 'value' => 'value', 'ttl' => 1); $item = new MCacheItem(); $item->setValue('value')->setTtl(1)->setKey('key'); $t = ItemTransformer::toArray($item); unset($t['endValidation']); $this->assertEquals($array, $t); }
/** * @param MCacheItem $item * @return bool */ public function addItem(MCacheItem $item) { $inserted = new \DateTime('now'); $endValidation = new \DateTime('now'); $endValidation = $endValidation->add(new \DateInterval(sprintf('PT%dS', $item->getTtl()))); $array = ItemTransformer::toArray($item); $array['end_validation'] = $endValidation->format(self::FILE_DATE_FORMAT); $array['inserted'] = $inserted->format(self::FILE_DATE_FORMAT); $json = json_encode($array); $filePath = $this->getBackupFilePath($item->getKey()); if (file_exists($filePath)) { unlink($this->getBackupFilePath($item->getKey())); } return file_put_contents($filePath, $json) !== false; }
/** * @param MCacheItem $item * @return bool */ public function addItem(MCacheItem $item) { $i = 0; $stmt = $this->pdoConnection->prepare(sprintf(self::$UPSERT, $this->tableName)); $stmt->bindParam(++$i, $item->getKey(), \PDO::PARAM_STR); $stmt->bindParam(++$i, $item->getValue(), \PDO::PARAM_STR); $stmt->bindParam(++$i, $item->getTtl(), \PDO::PARAM_INT); $stmt->bindParam(++$i, $item->getTtl(), \PDO::PARAM_INT); $stmt->bindParam(++$i, $item->getValue(), \PDO::PARAM_STR); $stmt->bindParam(++$i, $item->getTtl(), \PDO::PARAM_INT); $stmt->bindParam(++$i, $item->getTtl(), \PDO::PARAM_INT); return $stmt->execute(); }
public static function fromArray(array $item) { $i = new MCacheItem(); $i->setTtl($item['ttl'])->setKey($item['key'])->setValue($item['value']); return $i; }