HosipLan

Untitled

May 6th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. use Nette\Caching\Cache;
  2. use Nette\Caching\IStorage;
  3.  
  4. class ProductStorage
  5. {
  6.  
  7.     protected $cache;
  8.  
  9.     /**
  10.      * @var Product
  11.      */
  12.     private $repository;
  13.  
  14.     /**
  15.      * @var Product|NULL
  16.      */
  17.     private $product = NULL;
  18.  
  19.  
  20.     public function __construct(IStorage $storage, \Patie\Eshop\Model\ProductRepository $repository)
  21.     {
  22.         $this->repository = $repository;
  23.         $this->cache = new Cache($storage);
  24.     }
  25.  
  26.  
  27.     public function getProduct($id)
  28.     {
  29.  
  30.         $this->product = $this->cache->load($id);
  31.         if ($this->product === NULL) {
  32.             if (!$dbProduct = $this->repository->find($id)) {
  33.                 return NULL;
  34.             }
  35.  
  36.             $product = (object)array(
  37.                 'id' => $dbProduct->id,
  38.                 'name' => $dbProduct->name,
  39.             );
  40.  
  41.             $this->product = $this->cache->save($product->id, $product, array(
  42.                 Cache::TAGS => array("product/" . $product->id),
  43.             ));
  44.  
  45.         }
  46.  
  47.         return $this->product;
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment