Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Nette\Caching\Cache;
- use Nette\Caching\IStorage;
- class ProductStorage
- {
- protected $cache;
- /**
- * @var Product
- */
- private $repository;
- /**
- * @var Product|NULL
- */
- private $product = NULL;
- public function __construct(IStorage $storage, \Patie\Eshop\Model\ProductRepository $repository)
- {
- $this->repository = $repository;
- $this->cache = new Cache($storage);
- }
- public function getProduct($id)
- {
- $this->product = $this->cache->load($id);
- if ($this->product === NULL) {
- if (!$dbProduct = $this->repository->find($id)) {
- return NULL;
- }
- $product = (object)array(
- 'id' => $dbProduct->id,
- 'name' => $dbProduct->name,
- );
- $this->product = $this->cache->save($product->id, $product, array(
- Cache::TAGS => array("product/" . $product->id),
- ));
- }
- return $this->product;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment