Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cache clearing & setting cachePrefix for Aws S3 #336

Merged
merged 4 commits into from
Mar 6, 2014
Merged
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
32 changes: 30 additions & 2 deletions Imagine/Cache/Resolver/AwsS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class AwsS3Resolver implements ResolverInterface, CacheManagerAwareInterface
*/
protected $logger;

/**
* @var string
*/
protected $cachePrefix;

/**
* Constructs a cache resolver storing images on Amazon S3.
*
Expand Down Expand Up @@ -82,6 +87,14 @@ public function setCacheManager(CacheManager $cacheManager)
$this->cacheManager = $cacheManager;
}

/**
* @param string $cachePrefix
*/
public function setCachePrefix($cachePrefix)
{
$this->cachePrefix = $cachePrefix;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -184,7 +197,18 @@ public function setObjectUrlOption($key, $value)
*/
public function clear($cachePrefix)
{
// TODO: implement cache clearing for Amazon S3 service
// Let's just avoid to clear the whole bucket if cache prefix is empty
if ($cachePrefix === '') {
return;
}

try {
$response = $this->storage->deleteMatchingObjects($this->bucket, ltrim($cachePrefix, '/') . '/');

return true;
} catch (\Exception $e) {
return false;
}
}

/**
Expand All @@ -197,7 +221,11 @@ public function clear($cachePrefix)
*/
protected function getObjectPath($path, $filter)
{
return str_replace('//', '/', $filter.'/'.$path);
$path = $this->cachePrefix
? sprintf('%s/%s/%s', $this->cachePrefix, $filter, $path)
: sprintf('%s/%s', $filter, $path);

return str_replace('//', '/', $path);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/S3Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public function putObject($args) { }
public function deleteObject($args) { }

public function getObjectUrl($bucket, $key, $expires = 0, $args = array()) { }

public function deleteMatchingObjects($bucket, $prefix = '', $regex = '', array $options = array()) { }
}
16 changes: 14 additions & 2 deletions Tests/Imagine/Cache/Resolver/AwsS3ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,24 @@ public function testRemoveNotExisting()
$this->assertTrue($resolver->remove('thumb/some-folder/targetpath.jpg', 'thumb'));
}

public function testClearIsDisabled()
public function testClear()
{
$s3 = $this->getMock('Aws\S3\S3Client');
$s3
->expects($this->once())
->method('deleteMatchingObjects')
;

$resolver = new AwsS3Resolver($s3, 'images.example.com');
$this->assertTrue($resolver->clear('cache'));
}

public function testClearWithoutPrefix()
{
$s3 = $this->getMock('Aws\S3\S3Client');
$s3
->expects($this->never())
->method('deleteObject')
->method('deleteMatchingObjects')
;

$resolver = new AwsS3Resolver($s3, 'images.example.com');
Expand Down