repository = $repository; $this->cache = $cache; $this->cacheKey = $cacheKey; $this->factory = $factory; } public function find($id) { $rows = $this->cache->fetch($this->cacheKey); if (isset($rows[$id])) { return $this->factory->create($id, $rows[$id]); } return $this->repository->find($id); } public function findAll() { $rows = $this->cache->fetch($this->cacheKey); if (is_array($rows)) { return $this->factory->createMany($rows); } $databoxes = $this->repository->findAll(); $this->saveCache($databoxes); return $databoxes; } /** * @param \databox[] $databoxes */ private function saveCache(array $databoxes) { $rows = array(); foreach ($databoxes as $databox) { $rows[$databox->get_sbas_id()] = $databox->getRawData(); } $this->cache->save($this->cacheKey, $rows); } }