From b510fc3b6af2bcc63b831cbabb1a9668ec823a25 Mon Sep 17 00:00:00 2001 From: iclukas Date: Tue, 24 May 2022 17:42:58 +0200 Subject: [PATCH] limit nesting loop for SQLite filename creation to count of key array, some minor cleanups --- src/Stash/Driver/Sqlite.php | 12 ++++++------ tests/Stash/Test/Driver/SqliteAnyTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Stash/Driver/Sqlite.php b/src/Stash/Driver/Sqlite.php index 13cc4899..9bb353f3 100644 --- a/src/Stash/Driver/Sqlite.php +++ b/src/Stash/Driver/Sqlite.php @@ -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. * @@ -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; } @@ -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); } /** @@ -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) { @@ -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) { @@ -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] . '_'; diff --git a/tests/Stash/Test/Driver/SqliteAnyTest.php b/tests/Stash/Test/Driver/SqliteAnyTest.php index 8f1cf98a..6c093586 100644 --- a/tests/Stash/Test/Driver/SqliteAnyTest.php +++ b/tests/Stash/Test/Driver/SqliteAnyTest.php @@ -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());