/** * Attempts to find cached models based on the current query. * * @return array */ private function findCachedModels() { if (!$this->sourceModel || !$this->sourceModel->isCacheing()) { return false; } $uniqueKeys = $this->sourceModel->getUniqueKeys(); // Early validation allows us to fail immediately on obvious unsupported query types if (empty($uniqueKeys) || $this->distinct || $this->limit > 1 || !empty($this->groups) || !empty($this->havings) || !empty($this->orders) || !empty($this->offset) || !empty($this->unions) || !empty($this->joins) || !empty($this->columns) || empty($this->wheres)) { return false; } $query = new SquirrelQuery($this->wheres, $this->sourceObjectDeletedAtColumnName()); $searchingKey = $query->uniqueKeyString(); $modelKeys = SquirrelCache::uniqueKeys($this->sourceModel); $cacheKeyPrefix = SquirrelCache::getCacheKeyPrefix(get_class($this->sourceModel)); if (array_key_exists($searchingKey, $modelKeys)) { $cacheKeys = $query->cacheKeys($cacheKeyPrefix); if (empty($cacheKeys)) { return; } $models = []; foreach ($cacheKeys as $key) { $model = SquirrelCache::get($key); if ($model) { if ($deletedAt = $query->deletedAtObject()) { if (array_key_exists($deletedAt->column, $model)) { if ($deletedAt->value == SquirrelQueryWhere::WHERE_CLAUSE_TYPE_NULL && !empty($model[$deletedAt->column])) { // The query requires the deleted at column be empty return; } if ($deletedAt->value == SquirrelQueryWhere::WHERE_CLAUSE_TYPE_NOT_NULL && empty($model[$deletedAt->column])) { // The query requires the deleted at column have a value return; } } else { // The deleted at column could not be found, so we are not going to return anything return; } } $models[] = (object) $model; continue; } return; } return $models; } }