Example #1
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);
 }
Example #2
0
 /**
  * Checks whether a key exists and, optionally, whether it has a given $value
  *
  * @param string $key   The key name
  * @param mixed  $value Optional. If specified, also checks the key has this
  * value. Booleans will be converted to 1 and 0 (even inside arrays)
  *
  * @return bool
  */
 private function checkKeyExists($key, $value = null)
 {
     $type = $this->driver->type($key);
     if (is_null($value)) {
         return $type != 'none';
     }
     $value = $this->boolToString($value);
     switch ($type) {
         case 'string':
             $reply = $this->driver->get($key);
             // Allow non strict equality (2 equals '2')
             $result = $reply == $value;
             break;
         case 'list':
             $reply = $this->driver->lrange($key, 0, -1);
             // Check both arrays have the same key/value pairs + same order
             $result = $reply === $value;
             break;
         case 'set':
             $reply = $this->driver->smembers($key);
             // Only check both arrays have the same values
             sort($reply);
             sort($value);
             $result = $reply === $value;
             break;
         case 'zset':
             $reply = $this->driver->zrange($key, 0, -1, 'WITHSCORES');
             // Check both arrays have the same key/value pairs + same order
             $reply = $this->scoresToFloat($reply);
             $value = $this->scoresToFloat($value);
             $result = $reply === $value;
             break;
         case 'hash':
             $reply = $this->driver->hgetall($key);
             // Only check both arrays have the same key/value pairs (==)
             $result = $reply == $value;
             break;
         default:
             $result = false;
     }
     return $result;
 }
 /**
  * @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);
 }
Example #4
0
 /**
  * 获取有序集合的数据列表
  *  按 score 值递增(从小到大)来排序。
  * @param $key
  * @param int $start
  * @param int $stop
  * @param array $options
  * @return array
  */
 public function getZSetList($key, $start = 0, $stop = -1, $options = [])
 {
     return $this->redis->zrange($key, $start, $stop, $options);
 }