Angejia\Pea\QueryBuilder::getSimple PHP Method

getSimple() private method

简单查询,只根据主键过滤结果集
private getSimple ( )
    private function getSimple()
    {
        $primaryKeyName = $this->model->primaryKey();
        $cacheKeys = $this->buildCacheKeys();
        $keyId = array_flip($cacheKeys);
        $cache = $this->getCache();
        $cachedRows = $cache->get(array_values($cacheKeys));
        foreach ($cachedRows as $key => $row) {
            unset($cacheKeys[$keyId[$key]]);
        }
        // TODO 如何处理顺序
        $cachedRows = array_filter(array_values($cachedRows), function ($row) {
            return $row !== [];
        });
        $missedIds = array_keys($cacheKeys);
        if (!$missedIds) {
            $this->fireEvent('hit.simple.1000');
            return $cachedRows;
        }
        if (count($cachedRows) === 0) {
            $this->fireEvent('miss.simple');
        } else {
            $cachedNum = count($cachedRows);
            $missedNum = count($missedIds);
            $percent = (int) ($cachedNum / ($cachedNum + $missedNum) * 1000);
            $this->fireEvent('hit.simple.' . $percent);
        }
        $originWheres = $this->wheres;
        $originWhereBindings = $this->bindings['where'];
        $originColumns = $this->columns;
        $this->wheres = [];
        $this->bindings['where'] = [];
        $this->whereIn($primaryKeyName, $missedIds);
        $this->columns = null;
        $missedRows = array_fill_keys($missedIds, []);
        foreach (parent::get() as $row) {
            $missedRows[$row->{$primaryKeyName}] = $row;
        }
        $this->wheres = $originWheres;
        $this->bindings['where'] = $originWhereBindings;
        $this->columns = $originColumns;
        $toCachRows = [];
        $toCachIds = array_keys($missedRows);
        $toCachKeys = $this->buildRowCacheKey($toCachIds);
        foreach ($missedRows as $id => $row) {
            $toCachRows[$toCachKeys[$id]] = $row;
        }
        if ($toCachRows) {
            $cache->set($toCachRows);
        }
        $missedRows = array_filter(array_values($missedRows), function ($row) {
            return $row !== [];
        });
        return array_merge($cachedRows, $missedRows);
    }