Skip to content

Commit

Permalink
limit nesting loop for SQLite filename creation to count of key array…
Browse files Browse the repository at this point in the history
…, some minor cleanups
  • Loading branch information
iclukas committed May 24, 2022
1 parent 68ca733 commit b510fc3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Stash/Driver/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Stash\Exception\RuntimeException;

/**
* StashSqlite is a wrapper around one or more SQLite databases stored on the local system. While not as quick at at
* StashSqlite is a wrapper around one or more SQLite databases stored on the local system. While not as quick at
* reading as the StashFilesystem driver this class is significantly better when it comes to clearing multiple keys
* at once.
*
Expand Down Expand Up @@ -81,7 +81,7 @@ protected function setOptions(array $options = array())
*/
public function getData($key)
{
$sqlKey = $this->makeSqlKey($key);
$sqlKey = self::makeSqlKey($key);
if (!($sqlDriver = $this->getSqliteDriver($key)) || !($data = $sqlDriver->get($sqlKey))) {
return false;
}
Expand All @@ -105,7 +105,7 @@ public function storeData($key, $data, $expiration)
'encoding' => Utilities::encoding($data)
);

return $sqlDriver->set($this->makeSqlKey($key), $storeData, $expiration);
return $sqlDriver->set(self::makeSqlKey($key), $storeData, $expiration);
}

/**
Expand All @@ -118,7 +118,7 @@ public function clear($key = null)
}

if (!is_null($key)) {
$sqlKey = $this->makeSqlKey($key);
$sqlKey = self::makeSqlKey($key);
}

foreach ($databases as $database) {
Expand Down Expand Up @@ -157,7 +157,7 @@ public function purge()
*
* @param null|array $key
* @param bool $name = false
* @return \Stash\Driver\Sub\Sqlite
* @return \Stash\Driver\Sub\SqlitePdo|false
*/
protected function getSqliteDriver($key, $name = false)
{
Expand All @@ -174,7 +174,7 @@ protected function getSqliteDriver($key, $name = false)

$key = Utilities::normalizeKeys($key);

$nestingLevel = $this->nesting;
$nestingLevel = min($this->nesting, count($key)+1);
$fileName = 'cache_';
for ($i = 1; $i < $nestingLevel; $i++) {
$fileName .= $key[$i - 1] . '_';
Expand Down
12 changes: 12 additions & 0 deletions tests/Stash/Test/Driver/SqliteAnyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public function testConstruction()
$this->assertTrue($pool->save($item), 'Able to load and store with unconfigured extension.');
}

public function testNesting()
{
$key = array('apple', 'sauce');

$driver = new Sqlite(array('nesting' => 3));
$pool = new Pool();
$pool->setDriver($driver);
$item = $pool->getItem('testKey');
$item->set($key);
$this->assertTrue($pool->save($item), 'Able to load and store with nesting level 3.');
}

public static function tearDownAfterClass() : void
{
Utilities::deleteRecursive(Utilities::getBaseDirectory());
Expand Down

0 comments on commit b510fc3

Please sign in to comment.