Esempio n. 1
0
 private function getResults($restrictions, $setOperation)
 {
     $keys = array();
     $rangeQueries = array();
     if (count($restrictions) == 0) {
         throw new InvalidCriteriaException('Criteria must have at least 1 restriction, found 0.');
     }
     foreach ($restrictions as $restriction) {
         if ($restriction instanceof EqualToInterface) {
             $keys[] = $this->keyNamingStrategy->getKeyName(array($restriction->getKey(), $restriction->getValue()));
         } elseif ($restriction instanceof LessThanInterface) {
             $key = $restriction->getKey();
             $query = isset($rangeQueries[$key]) ? $rangeQueries[$key] : new ZRangeByScore($key);
             $query->setMax($restriction->getValue());
             $rangeQueries[$key] = $query;
         } elseif ($restriction instanceof GreaterThanInterface) {
             $key = $restriction->getKey();
             $query = isset($rangeQueries[$key]) ? $rangeQueries[$key] : new ZRangeByScore($key);
             $query->setMin($restriction->getValue());
             $rangeQueries[$key] = $query;
         } elseif ($restriction instanceof LessThanXDaysAgoInterface) {
             $key = $restriction->getKey();
             $query = isset($rangeQueries[$key]) ? $rangeQueries[$key] : new ZRangeByScore($key);
             $value = strtotime($restriction->getValue());
             if (false === $value) {
                 throw new InvalidRestrictionValue(sprintf('The value "%s" is not a valid format. Must be similar to "5 days ago" or "1 month 15 days ago".', $restriction->getValue()));
             }
             $date = DateTime::createFromFormat('U', $value);
             $date->setTime(0, 0, 0);
             $query->setMin($date->format('U'));
             $rangeQueries[$key] = $query;
         } elseif ($restriction instanceof GreaterThanXDaysAgoInterface) {
             $key = $restriction->getKey();
             $query = isset($rangeQueries[$key]) ? $rangeQueries[$key] : new ZRangeByScore($key);
             $value = strtotime($restriction->getValue());
             if (false === $value) {
                 throw new InvalidRestrictionValue(sprintf('The value "%s" is not a valid format. Must be similar to "5 days ago" or "1 month 15 days ago".', $restriction->getValue()));
             }
             $date = DateTime::createFromFormat('U', $value);
             $date->setTime(0, 0, 0);
             $query->setMax($date->format('U'));
             $rangeQueries[$key] = $query;
         } elseif ($restriction instanceof AndGroupInterface) {
             $keys[] = $this->getResults($restriction->getValue(), self::OP_INTERSECT);
         } elseif ($restriction instanceof OrGroupInterface) {
             $keys[] = $this->getResults($restriction->getValue(), self::OP_UNION);
         } else {
             throw new \InvalidArgumentException(sprintf('Either the given restriction is of an invalid type, or the restriction type "%s" has not been implemented.', get_class($restriction)));
         }
     }
     /*
     //@ TODO redo optimization. If we are at the top level set of restrictions, we can return the restriction data directly
     if (count($rangeQueries) == 0) {
         if ($countOnly) {
             $tmpKey = 'redis-orm:cache:'.md5(time().$criteria->__toString());
             array_unshift($keys, $tmpKey);
             call_user_func_array(array($this->redis, $setOperation === self::OP_UNION ? 'sunionstore' : 'sinterstore'), $keys);
             $this->redis->expire($tmpKey, 1200);
     
             return $this->redis->scard($tmpKey);
         }
         return call_user_func_array(array($this->redis, $setOperation === self::OP_UNION ? 'sunion' : 'sinter'), $keys);
     }
     */
     $restrictionsKey = $this->restrictionsKeyGenerator->getKeyName($restrictions instanceof Collection ? $restrictions->toArray() : $restrictions);
     $tmpKey = $this->keyNamingStrategy->getKeyName(array('redis-orm:cache', str_replace(' ', '.', microtime()), md5($restrictionsKey)));
     $rangeKeys = $this->handleRangeQueries($rangeQueries, $tmpKey);
     //$keys = array_merge($keys, array_keys($rangeQueries));
     $keys = array_merge($rangeKeys, $keys);
     array_unshift($keys, $tmpKey, count($keys));
     array_push($keys, 'AGGREGATE', 'MAX');
     call_user_func_array(array($this->redis, $setOperation === self::OP_UNION ? 'zunionstore' : 'zinterstore'), $keys);
     //$this->handleRangeQueries($rangeQueries, $tmpKey);
     $this->redis->expire($tmpKey, 1200);
     return $tmpKey;
 }
 /**
  * @param $restrictions
  * @param $expectedKey
  * @dataProvider dataProvider
  */
 public function testKeyGenerator($restrictions, $expectedKey)
 {
     $keyGenerator = new RestrictionsKeyGenerator();
     assertEquals($expectedKey, $keyGenerator->getKeyName($restrictions));
 }
Esempio n. 3
0
 /**
  * @return string
  */
 public function __toString()
 {
     $keyGenerator = new RestrictionsKeyGenerator();
     $keyGenerator->getKeyName($this->getRestrictions());
 }