예제 #1
0
 /**
  * {@inheritDoc}
  */
 public function count(array $sources)
 {
     call_user_func_array([$this->redis, 'zunionstore'], array_merge([$key = sha1(microtime()), count($sources)], array_map(function ($source) {
         return "aggregator:sources:{$source}";
     }, $sources)));
     $count = $this->redis->zcard($key);
     $this->redis->del($key);
     return $count;
 }
예제 #2
0
 /**
  * @param CriteriaInterface $criteria
  *
  * @throws InvalidCriteriaException
  *
  * @return array
  */
 public function findIdsBy(CriteriaInterface $criteria, $countOnly = false)
 {
     $resultKey = $this->getResults($criteria->getRestrictions(), self::OP_INTERSECT);
     if ($countOnly) {
         return $this->redis->zcard($resultKey);
     }
     return $this->redis->zrange($resultKey, 0, -1);
 }
예제 #3
0
 /**
  * @param CriteriaInterface $criteria
  * @throws InvalidCriteriaException
  * @return array
  */
 public function findIdsBy(CriteriaInterface $criteria, $countOnly = false)
 {
     $keys = array();
     $rangeQueries = array();
     $restrictions = $criteria->getRestrictions();
     if ($restrictions->count() == 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;
         } 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)));
         }
     }
     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, 'sinterstore'), $keys);
             $this->redis->expire($tmpKey, 1200);
             return $this->redis->scard($tmpKey);
         }
         return call_user_func_array(array($this->redis, 'sinter'), $keys);
     }
     $tmpKey = 'redis-orm:cache:' . md5(time() . $criteria->__toString());
     $rangeKeys = $this->handleRangeQueries($rangeQueries, $tmpKey);
     //$keys = array_merge($keys, array_keys($rangeQueries));
     $keys = array_merge($keys, $rangeKeys);
     array_unshift($keys, $tmpKey, count($keys));
     array_push($keys, 'AGGREGATE', 'MAX');
     call_user_func_array(array($this->redis, 'zinterstore'), $keys);
     //$this->handleRangeQueries($rangeQueries, $tmpKey);
     $this->redis->expire($tmpKey, 1200);
     if ($countOnly) {
         return $this->redis->zcard($tmpKey);
     }
     return $this->redis->zrange($tmpKey, 0, -1);
 }
예제 #4
0
 /**
  * 有序集合是否存在
  * @param $key
  * @return bool
  */
 public function existsZSet($key)
 {
     return $this->redis->zcard($key) > 0;
 }