Example #1
0
 /**
  * @return boolean whether the flush operation was successful.
  */
 protected function flushValues()
 {
     $this->ssdb->multi_del($this->getkeys());
     return $this->ssdb->hclear($this->cache_keys_hash);
 }
 /**
  * Initializes the DB connection component.
  * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
  * @throws InvalidConfigException if [[db]] is invalid.
  */
 public function init()
 {
     parent::init();
     if (is_string($this->db)) {
         $this->db = Instance::ensure($this->db, Connection::className());
     }
 }
Example #3
0
 /**
  * Fetch by pk if possible as this is much faster
  * @param Connection $db the database connection used to execute the query.
  * If this parameter is not given, the `db` application component will be used.
  * @param string $type the type of the script to generate
  * @param string $columnName
  * @return array|bool|null|string
  * @throws \yii\base\InvalidParamException
  * @throws \yii\base\NotSupportedException
  */
 private function findByPk($db, $type, $columnName = null)
 {
     if (count($this->where) == 1) {
         $pks = (array) reset($this->where);
     } else {
         foreach ($this->where as $values) {
             if (is_array($values)) {
                 // TODO support composite IN for composite PK
                 throw new NotSupportedException('Find by composite PK is not supported by redis ActiveRecord.');
             }
         }
         $pks = [$this->where];
     }
     /* @var $modelClass ActiveRecord */
     $modelClass = $this->modelClass;
     if ($type == 'Count') {
         $start = 0;
         $limit = null;
     } else {
         $start = $this->offset === null ? 0 : $this->offset;
         $limit = $this->limit;
     }
     $i = 0;
     $data = [];
     foreach ($pks as $pk) {
         if (++$i > $start && ($limit === null || $i <= $start + $limit)) {
             $key = $modelClass::keyPrefix() . ':a:' . $modelClass::buildKey($pk);
             $result = $db->hgetall($key);
             if (!empty($result)) {
                 $data[] = $result;
                 if ($type === 'One' && $this->orderBy === null) {
                     break;
                 }
             }
         }
     }
     // TODO support orderBy
     switch ($type) {
         case 'All':
             return $data;
         case 'One':
             return reset($data);
         case 'Count':
             return count($data);
         case 'Column':
             $column = [];
             foreach ($data as $dataRow) {
                 $row = [];
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     $row[$dataRow[$i++]] = $dataRow[$i++];
                 }
                 $column[] = $row[$columnName];
             }
             return $column;
         case 'Sum':
             $sum = 0;
             foreach ($data as $dataRow) {
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName) {
                         $sum += $dataRow[$i];
                         break;
                     }
                 }
             }
             return $sum;
         case 'Average':
             $sum = 0;
             $count = 0;
             foreach ($data as $dataRow) {
                 $count++;
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName) {
                         $sum += $dataRow[$i];
                         break;
                     }
                 }
             }
             return $sum / $count;
         case 'Min':
             $min = null;
             foreach ($data as $dataRow) {
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName && ($min == null || $dataRow[$i] < $min)) {
                         $min = $dataRow[$i];
                         break;
                     }
                 }
             }
             return $min;
         case 'Max':
             $max = null;
             foreach ($data as $dataRow) {
                 $c = count($dataRow);
                 for ($i = 0; $i < $c;) {
                     if ($dataRow[$i++] == $columnName && ($max == null || $dataRow[$i] > $max)) {
                         $max = $dataRow[$i];
                         break;
                     }
                 }
             }
             return $max;
     }
     throw new InvalidParamException('Unknown fetch type: ' . $type);
 }
Example #4
0
 /**
  * Session destroy handler.
  * Do not call this method directly.
  * @param string $id session ID
  * @return boolean whether session is destroyed successfully
  */
 public function destroySession($id)
 {
     return (bool) $this->ssdb->del($this->calculateKey($id));
 }
Example #5
0
 /**
  * 所有数据
  *
  * @param ActiveRecord $modelClass
  * @param \wsl\ssdb\Connection $db
  * @return mixed
  */
 protected function buildCount($modelClass, $db)
 {
     $count = 0;
     $keyPrefix = $modelClass::keyPrefix();
     if ($this->orderBy) {
         foreach ($this->orderBy as $field => $sort) {
             if (SORT_ASC === $sort) {
                 $count = $db->zsize($keyPrefix . ':f:' . $field);
             } elseif (SORT_DESC === $sort) {
                 $count = $db->zsize($keyPrefix . ':f:' . $field);
             }
             break;
         }
     } else {
         $count = $db->zsize($keyPrefix);
     }
     return $count;
 }