Skip to content

Instantly share code, notes, and snippets.

@patie
Created May 6, 2012 10:47
Show Gist options
  • Save patie/2621602 to your computer and use it in GitHub Desktop.
Save patie/2621602 to your computer and use it in GitHub Desktop.
<?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);
}
}
<?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