Created
May 6, 2012 10:47
-
-
Save patie/2621602 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Author: Patrik Gmitter <[email protected]> | |
* Date: 4/30/12 | |
*/ | |
namespace Patie\Eshop\Store; | |
use Nette; | |
use Nette\Caching\Cache; | |
class Product extends \Nette\Object | |
{ | |
private $storage; | |
public $id; | |
protected $name; | |
public function __construct(ProductStorage $storage) | |
{ | |
$this->storage = $storage; | |
} | |
/** | |
* @return ProductStorage | |
*/ | |
final public function getStorage() | |
{ | |
return $this->storage; | |
} | |
/** | |
* @return Product|NULL | |
*/ | |
final public function getProduct($id) | |
{ | |
return $this->storage->getProduct($id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Author: Patrik Gmitter <[email protected]> | |
* Date: 5/6/12 | |
*/ | |
namespace Patie\Eshop\Store; | |
class ProductStorage | |
{ | |
protected $cache; | |
/** | |
* @var ProductRepository | |
*/ | |
private $repository; | |
/** | |
* @var Product|NULL | |
*/ | |
private $product = NULL; | |
public function __construct(\Nette\Caching\Storages\FileStorage $storage, \Patie\Eshop\Model\ProductRepository $repository) | |
{ | |
$this->repository = $repository; | |
$this->cache = new \Nette\Caching\Cache($storage); | |
} | |
public function getProduct($id) | |
{ | |
$this->product = $this->cache->load($id); | |
if ($this->product === NULL) { | |
$dbProduct = $this->repository->find($id); | |
if (!$dbProduct) { | |
return; | |
} | |
// TOTO BUDE ZREJME HLUPOST PRETOZE PRODUCT SAM VYZADUJE ProductCache | |
// neviem ako stvorit objekt, ktory sa zapise do cache | |
// $product = new Product(); | |
// $product->id = $dbProduct->id; | |
// $product->name = $dbProduct->name; | |
$this->product = $this->cache->save($id, $product, array( | |
Cache::TAGS => array("product/$this->id"), | |
)); | |
} | |
return $this->product; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment