app = $application; $this->repository = $repository; $this->cache = $cache; $this->cacheKey = $cacheKey; } /** * @return \collection[] */ public function findAll() { $cacheKey = hash('sha256', $this->cacheKey); /** @var \collection[] $collections */ $collections = $this->cache->fetch($cacheKey); if ($collections === false) { $collections = $this->repository->findAll(); $this->putInCache($cacheKey, $collections); } else { foreach ($collections as $collection) { $collection->hydrate($this->app); } } return $collections; } /** * @param int $collectionId * @return \collection|null */ public function find($collectionId) { $collections = $this->findAll(); if (isset($collections[$collectionId])) { return $collections[$collectionId]; } return null; } public function save(\collection $collection) { $this->repository->save($collection); $cacheKey = hash('sha256', $this->cacheKey); $this->cache->delete($cacheKey); } private function putInCache($key, $value) { $this->cache->save($key, $value); } }