Skip to content

Instantly share code, notes, and snippets.

@patie
Created May 6, 2012 11:12
Show Gist options
  • Save patie/2621687 to your computer and use it in GitHub Desktop.
Save patie/2621687 to your computer and use it in GitHub Desktop.
len objekt pre produkty
<?php
/**
* Author: Patrik Gmitter <[email protected]>
* Date: 4/30/12
*/
namespace Patie\Eshop\Store;
class Product extends \Nette\Object
{
public $id;
public $name;
}
<?php
/**
* Author: Patrik Gmitter <[email protected]>
* Date: 5/6/12
*/
namespace Patie\Eshop\Store;
class ProductStorage
{
protected $cache;
/**
* @var Product
*/
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 NULL;
}
$product = new Product();
$product->id = $dbProduct->id;
$product->name = $dbProduct->name;
$this->product = $this->cache->save($product->id, $product, array(
\Nette\Caching\Cache::TAGS => array("product/$product->id"),
));
}
return $this->product;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment