Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(ConnectionInterface $connection,
$lockLottery = [2, 100])
{
$this->table = $table;
$this->prefix = $prefix;
$this->setPrefix($prefix);
$this->connection = $connection;
$this->lockTable = $lockTable;
$this->lockLottery = $lockLottery;
Expand Down Expand Up @@ -361,6 +361,17 @@ public function getPrefix()
return $this->prefix;
}

/**
* Set the cache key prefix.
*
* @param string $prefix
* @return void
*/
public function setPrefix($prefix)
{
$this->prefix = ! empty($prefix) ? $prefix.':' : '';
}

/**
* Serialize the given value.
*
Expand Down
34 changes: 17 additions & 17 deletions tests/Cache/CacheDatabaseStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testNullIsReturnedWhenItemNotFound()
$store = $this->getStore();
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefix:foo')->andReturn($table);
$table->shouldReceive('first')->once()->andReturn(null);

$this->assertNull($store->get('foo'));
Expand All @@ -34,7 +34,7 @@ public function testNullIsReturnedAndItemDeletedWhenItemIsExpired()
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['forget'])->setConstructorArgs($this->getMocks())->getMock();
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefix:foo')->andReturn($table);
$table->shouldReceive('first')->once()->andReturn((object) ['expiration' => 1]);
$store->expects($this->once())->method('forget')->with($this->equalTo('foo'))->willReturn(null);

Expand All @@ -46,7 +46,7 @@ public function testDecryptedValueIsReturnedWhenItemIsValid()
$store = $this->getStore();
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefix:foo')->andReturn($table);
$table->shouldReceive('first')->once()->andReturn((object) ['value' => serialize('bar'), 'expiration' => 999999999999999]);

$this->assertSame('bar', $store->get('foo'));
Expand All @@ -57,7 +57,7 @@ public function testValueIsReturnedOnPostgres()
$store = $this->getPostgresStore();
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefix:foo')->andReturn($table);
$table->shouldReceive('first')->once()->andReturn((object) ['value' => base64_encode(serialize('bar')), 'expiration' => 999999999999999]);

$this->assertSame('bar', $store->get('foo'));
Expand All @@ -69,7 +69,7 @@ public function testValueIsInsertedWhenNoExceptionsAreThrown()
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$store->expects($this->once())->method('getTime')->willReturn(1);
$table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => serialize('bar'), 'expiration' => 61])->andReturnTrue();
$table->shouldReceive('insert')->once()->with(['key' => 'prefix:foo', 'value' => serialize('bar'), 'expiration' => 61])->andReturnTrue();

$result = $store->put('foo', 'bar', 60);
$this->assertTrue($result);
Expand All @@ -81,10 +81,10 @@ public function testValueIsUpdatedWhenInsertThrowsException()
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->with('table')->andReturn($table);
$store->expects($this->once())->method('getTime')->willReturn(1);
$table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => serialize('bar'), 'expiration' => 61])->andReturnUsing(function () {
$table->shouldReceive('insert')->once()->with(['key' => 'prefix:foo', 'value' => serialize('bar'), 'expiration' => 61])->andReturnUsing(function () {
throw new Exception;
});
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:foo')->andReturn($table);
$table->shouldReceive('update')->once()->with(['value' => serialize('bar'), 'expiration' => 61])->andReturnTrue();

$result = $store->put('foo', 'bar', 60);
Expand All @@ -97,7 +97,7 @@ public function testValueIsInsertedOnPostgres()
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$store->expects($this->once())->method('getTime')->willReturn(1);
$table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => base64_encode(serialize("\0")), 'expiration' => 61])->andReturnTrue();
$table->shouldReceive('insert')->once()->with(['key' => 'prefix:foo', 'value' => base64_encode(serialize("\0")), 'expiration' => 61])->andReturnTrue();

$result = $store->put('foo', "\0", 60);
$this->assertTrue($result);
Expand All @@ -116,7 +116,7 @@ public function testItemsMayBeRemovedFromCache()
$store = $this->getStore();
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefix:foo')->andReturn($table);
$table->shouldReceive('delete')->once();

$store->forget('foo');
Expand All @@ -143,7 +143,7 @@ public function testIncrementReturnsCorrectValues()
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:foo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn(null);
$this->assertFalse($store->increment('foo'));
Expand All @@ -153,7 +153,7 @@ public function testIncrementReturnsCorrectValues()
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:foo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$this->assertFalse($store->increment('foo'));
Expand All @@ -163,11 +163,11 @@ public function testIncrementReturnsCorrectValues()
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:foo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:foo')->andReturn($table);
$table->shouldReceive('update')->once()->with(['value' => serialize(3)]);
$this->assertEquals(3, $store->increment('foo'));
}
Expand All @@ -182,7 +182,7 @@ public function testDecrementReturnsCorrectValues()
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:foo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn(null);
$this->assertFalse($store->decrement('foo'));
Expand All @@ -192,7 +192,7 @@ public function testDecrementReturnsCorrectValues()
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:foo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$this->assertFalse($store->decrement('foo'));
Expand All @@ -202,11 +202,11 @@ public function testDecrementReturnsCorrectValues()
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixbar')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:bar')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixbar')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefix:bar')->andReturn($table);
$table->shouldReceive('update')->once()->with(['value' => serialize(2)]);
$this->assertEquals(2, $store->decrement('bar'));
}
Expand Down