/** * */ public function testModifiedSquirrelModel() { $user = new ModifiedTestUser(); $uniqueKeys = $user->getUniqueKeys(); $primaryKey = $user->getKeyName(); $this->assertContains($primaryKey, $uniqueKeys); $this->assertcount(3, $uniqueKeys); SquirrelCache::setCacheActive(true); // Set global cache to true $this->assertFalse($user->isCacheing(), "Modified User Model has cache turned off, but it's still returning true."); $this->assertEquals(24 * 60 * 7, $user->cacheExpirationMinutes()); $keys = $user->cacheKeys(); $this->assertCount(3, $keys, "Expected 3 cache keys returned, but received different amount."); $prefix = SquirrelCache::getCacheKeyPrefix(get_class($user)); $primary = [$primaryKey => strval($user->id)]; $expectedCacheKey = $prefix . serialize($primary); $this->assertContains($expectedCacheKey, $keys); $primaryCacheKey = $user->primaryCacheKey(); $this->assertEquals($expectedCacheKey, $primaryCacheKey); }
/** * Returns just the primary cache key. * * @return [type] [description] */ public final function primaryCacheKey() { return SquirrelCache::primaryCacheKey($this, $this->getAttributes()); }
/** * 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; } }