pconnect('localhost', 11211); } return self::$cache->set($key, $value, 0, $ttl); } /** * Fetch data from cache. * @param string $key * @param unknown $default * @return unknown */ static public function fetch($key, $default = false) { if (is_null(self::$cache)) { self::$cache = new \Memcache(); self::$cache->pconnect('localhost', 11211); } $value = self::$cache->get($key); return false !== $value ? $value : $default; } /** * Delete cached item. * @param string $key */ static public function delete($key) { if (is_null(self::$cache)) { self::$cache = new \Memcache(); self::$cache->pconnect('localhost', 11211); } self::$cache->delete($key); } /** * Load a doctrine entity from cache. If not found in cache it will load from db and store in cache. * @param string $entityName * @param integer $id * @param \Doctrine\ORM\EntityManager $em * @param number $ttl * @param number $depth * @return unknown */ static public function fetchEntity($entityName, $id, \Doctrine\ORM\EntityManager $em, $ttl = 300, $depth = 1) { if (isset(self::$loadedFromCache[$entityName . $id])) { $record = $em->getRepository($entityName)->find($id); return !is_null($record) ? $record->toArray() : null; } $cachedEntity = self::fetch($entityName . '::' . $id); if (false !== $cachedEntity) { return $cachedEntity; } $entity = $em->getRepository($entityName)->find($id); if (!is_null($entity)) { $entity = $entity->toArray(); self::store($entityName . '::' . $id, $entity, $ttl); self::$loadedFromCache[$entityName . $id] = true; } return $entity; } }