Skip to content

Commit

Permalink
Remove external dependency ArrayCache
Browse files Browse the repository at this point in the history
  • Loading branch information
lav45 committed Jun 1, 2024
1 parent 834e151 commit b81bade
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
22 changes: 6 additions & 16 deletions framework/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\caching\ArrayCache;
use yii\db\ActiveQueryInterface;
use yii\db\Connection;
use yii\db\QueryInterface;
Expand Down Expand Up @@ -152,6 +151,8 @@ protected function prepareKeys($models)
return array_keys($models);
}

private $_totalCount = [];

/**
* {@inheritdoc}
*/
Expand All @@ -161,23 +162,12 @@ protected function prepareTotalCount()
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
}
$query = (clone $this->query)->limit(-1)->offset(-1)->orderBy([]);
$key = md5((string)$query);

return $this->getQueryCache()->getOrSet((string)$query, function () use ($query) {
return (int) $query->count('*', $this->db);
});
}

private $_queryCache;

/**
* @return \yii\caching\CacheInterface
*/
protected function getQueryCache()
{
if ($this->_queryCache === null) {
$this->_queryCache = new ArrayCache();
if (isset($this->_totalCount[$key]) === false) {
$this->_totalCount[$key] = (int)$query->count('*', $this->db);
}
return $this->_queryCache;
return $this->_totalCount[$key];
}

/**
Expand Down
22 changes: 6 additions & 16 deletions framework/data/SqlDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace yii\data;

use yii\base\InvalidConfigException;
use yii\caching\ArrayCache;
use yii\db\Connection;
use yii\db\Expression;
use yii\db\Query;
Expand Down Expand Up @@ -155,6 +154,8 @@ protected function prepareKeys($models)
return array_keys($models);
}

private $_totalCount = [];

/**
* {@inheritdoc}
*/
Expand All @@ -164,22 +165,11 @@ protected function prepareTotalCount()
'from' => ['sub' => "({$this->sql})"],
'params' => $this->params,
]);
$key = md5((string)$query);

return $this->getQueryCache()->getOrSet((string)$query, function () use ($query) {
return (int) $query->count('*', $this->db);
});
}

private $_queryCache;

/**
* @return \yii\caching\CacheInterface
*/
protected function getQueryCache()
{
if ($this->_queryCache === null) {
$this->_queryCache = new ArrayCache();
if (isset($this->_totalCount[$key]) === false) {
$this->_totalCount[$key] = (int)$query->count('*', $this->db);
}
return $this->_queryCache;
return $this->_totalCount[$key];
}
}

0 comments on commit b81bade

Please sign in to comment.