Commit 7f1a66a6 authored by Bong Cosca's avatar Bong Cosca
Browse files

Add support for $ttl argument in count() (Issue #393)

parent 38a3e162
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -110,9 +110,9 @@ class Mapper extends \DB\Cursor {
		);
		$fw=\Base::instance();
		$cache=\Cache::instance();
		if (!$fw->get('CACHE') || !$ttl || !($cached=$cache->exists(
			$hash=$fw->hash($fw->stringify(array($fields,$filter,$options))).
				'.mongo',$result)) || $cached[0]+$ttl<microtime(TRUE)) {
		if (!($cached=$cache->exists($hash=$fw->hash($fw->stringify(
			array($fields,$filter,$options))).'.mongo',$result)) || !$ttl ||
			$cached[0]+$ttl<microtime(TRUE)) {
			if ($options['group']) {
				$tmp=$this->db->selectcollection(
					$fw->get('HOST').'.'.$fw->get('BASE').'.'.uniqid().'.tmp'
@@ -185,7 +185,17 @@ class Mapper extends \DB\Cursor {
	*	@param $filter array
	**/
	function count($filter=NULL) {
		return $this->collection->count($filter);
		$fw=\Base::instance();
		$cache=\Cache::instance();
		if (!($cached=$cache->exists($hash=$fw->hash($fw->stringify(
			array($filter))).'.mongo',$result)) || !$ttl ||
			$cached[0]+$ttl<microtime(TRUE)) {
			$result=$this->collection->count($filter);
			if ($fw->get('CACHE') && $ttl)
				// Save to cache backend
				$cache->set($hash,$result,$ttl);
		}
		return $result;
	}

	/**