Skip to content

Commit

Permalink
Separate chain calls (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankaest committed May 22, 2022
1 parent dced3ce commit 192096a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/DbCache.php
Expand Up @@ -106,7 +106,8 @@ public function set($key, $value, $ttl = null): bool
}

try {
$this->db->createCommand()
$this->db
->createCommand()
->upsert($this->table, $this->buildDataRow($key, $ttl, $value, true))
->noCache()
->execute()
Expand Down Expand Up @@ -169,7 +170,8 @@ public function setMultiple($values, $ttl = null): bool
$this->deleteData($keys);

if (!empty($rows) && !$this->isExpiredTtl($ttl)) {
$this->db->createCommand()
$this->db
->createCommand()
->batchInsert($this->table, ['id', 'expire', 'data'], $rows)
->noCache()
->execute()
Expand Down Expand Up @@ -238,7 +240,11 @@ private function deleteData($id): void

try {
$condition = $id === true ? '' : ['id' => $id];
$this->db->createCommand()->delete($this->table, $condition)->noCache()->execute();
$this->db
->createCommand()
->delete($this->table, $condition)
->noCache()
->execute();
} catch (Throwable $e) {
throw new CacheException('Unable to delete cache data.', 0, $e);
}
Expand Down Expand Up @@ -274,7 +280,8 @@ private function buildDataRow(string $id, ?int $ttl, $value, bool $associative):
private function gc(): void
{
if (random_int(0, 1000000) < $this->gcProbability) {
$this->db->createCommand()
$this->db
->createCommand()
->delete($this->table, ['AND', ['>', 'expire', 0], ['<', 'expire', time()]])
->execute()
;
Expand All @@ -295,7 +302,9 @@ private function normalizeTtl($ttl): ?int
}

if ($ttl instanceof DateInterval) {
return (new DateTime('@0'))->add($ttl)->getTimestamp();
return (new DateTime('@0'))
->add($ttl)
->getTimestamp();
}

return (int) $ttl;
Expand Down
4 changes: 3 additions & 1 deletion src/Migration/M202101140204CreateCache.php
Expand Up @@ -32,7 +32,9 @@ public function up(MigrationBuilder $b): void
$builder = new MigrationBuilder($this->cache->getDb(), $this->migrationInformer);

$builder->createTable($this->cache->getTable(), [
'id' => $builder->string(128)->notNull(),
'id' => $builder
->string(128)
->notNull(),
'expire' => $builder->integer(),
'data' => $builder->binary(),
'PRIMARY KEY ([[id]])',
Expand Down
11 changes: 8 additions & 3 deletions tests/DbCacheTest.php
Expand Up @@ -481,15 +481,20 @@ public function testDeleteMultipleThrowExceptionForFailExecuteCommand(): void

private function createDbCacheWithFailConnection(): DbCache
{
$command = $this->getMockBuilder(Command::class)
$command = $this
->getMockBuilder(Command::class)
->onlyMethods(['execute'])
->disableOriginalConstructor()
->getMockForAbstractClass()
;
$command->method('execute')->willThrowException(new Exception('Some error.'));
$command
->method('execute')
->willThrowException(new Exception('Some error.'));

$db = $this->createMock(ConnectionInterface::class);
$db->method('createCommand')->willReturn($command);
$db
->method('createCommand')
->willReturn($command);

return new DbCache($db);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/MigrationTest.php
Expand Up @@ -22,6 +22,8 @@ public function testUpAndDown(): void

private function tableExists(string $tableName): bool
{
return $this->db->getSchema()->getTableSchema($tableName) !== null;
return $this->db
->getSchema()
->getTableSchema($tableName) !== null;
}
}

0 comments on commit 192096a

Please sign in to comment.