6 * Central cache functionality.
16 static protected $cache = null;
20 static protected $cacheKick = true;
24 static private $loadedFromCache = array();
29 * Store data in cache.
31 * @param unknown $value
35 static public function store($key, $value, $ttl = 0)
37 if (is_null(self::$cache))
39 self::$cache = new \Memcache();
40 self::$cache->pconnect('localhost', 11211);
42 return self::$cache->set($key, $value, 0, $ttl);
46 * Fetch data from cache.
48 * @param unknown $default
51 static public function fetch($key, $default = false)
53 if (is_null(self::$cache))
55 self::$cache = new \Memcache();
56 self::$cache->pconnect('localhost', 11211);
58 $value = self::$cache->get($key);
59 return false !== $value
69 static public function delete($key)
71 if (is_null(self::$cache))
73 self::$cache = new \Memcache();
74 self::$cache->pconnect('localhost', 11211);
76 self::$cache->delete($key);
80 * Load a doctrine entity from cache. If not found in cache it will load from db and store in cache.
81 * @param string $entityName
83 * @param \Doctrine\ORM\EntityManager $em
85 * @param number $depth
88 static public function fetchEntity($entityName, $id, \Doctrine\ORM\EntityManager $em, $ttl = 300, $depth = 1)
90 if (isset(self::$loadedFromCache[$entityName . $id]))
92 $record = $em->getRepository($entityName)->find($id);
93 return !is_null($record)
97 $cachedEntity = self::fetch($entityName . '::' . $id);
98 if (false !== $cachedEntity)
100 return $cachedEntity;
103 $entity = $em->getRepository($entityName)->find($id);
104 if (!is_null($entity))
106 $entity = $entity->toArray();
107 self::store($entityName . '::' . $id, $entity, $ttl);
108 self::$loadedFromCache[$entityName . $id] = true;