Skip to content

Commit 4c9b4e4

Browse files
committed
Merge pull request #16 from php-cache/updates
[WIP] Updated to adapter 0.2 and tagging 0.3
2 parents 1059955 + 73fb851 commit 4c9b4e4

File tree

8 files changed

+135
-236
lines changed

8 files changed

+135
-236
lines changed

README.md

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# Doctrine PSR-6 adapter
2-
[![Build Status](https://travis-ci.org/php-cache/doctrine-adapter.svg?branch=master)](https://travis-ci.org/php-cache/doctrine-adapter) [![codecov.io](https://codecov.io/github/php-cache/doctrine-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/doctrine-adapter?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/1ac09139-8edd-41a9-84ab-3d84791a2659/mini.png)](https://insight.sensiolabs.com/projects/1ac09139-8edd-41a9-84ab-3d84791a2659)
1+
# Doctrine PSR-6 Cache pool
2+
[![Latest Stable Version](https://poser.pugx.org/cache/doctrine-adapter/v/stable)](https://packagist.org/packages/cache/doctrine-adapter) [![codecov.io](https://codecov.io/github/php-cache/doctrine-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/doctrine-adapter?branch=master) [![Build Status](https://travis-ci.org/php-cache/doctrine-adapter.svg?branch=master)](https://travis-ci.org/php-cache/doctrine-adapter) [![Total Downloads](https://poser.pugx.org/cache/doctrine-adapter/downloads)](https://packagist.org/packages/cache/doctrine-adapter) [![Monthly Downloads](https://poser.pugx.org/cache/doctrine-adapter/d/monthly.png)](https://packagist.org/packages/cache/doctrine-adapter) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
33

4-
This is a implementation for the PSR-6 that wraps the Doctrine cache. This implementation supports tags.
4+
This is a PSR-6 cache implementation using Doctrine. It is a part of the PHP Cache organisation. To read about
5+
features like tagging and hierarchy support please read the shared documentation at [www.php-cache.com](http://www.php-cache.com).
56

6-
If you want to use this library with Symfony you may be intrerested in
7-
[Doctrine Adapter Bundle](https://github.com/php-cache/doctrine-adapter-bundle).
7+
This is a PSR-6 to Doctrine bridge. If you are interested in a Doctrine to PSR-6 bridge you should have a look at
8+
[PSR-6 Doctrine Bridge](https://github.com/php-cache/doctrine-bridge).
89

9-
## To Install
10+
### Install
1011

11-
Run the following in your project root, assuming you have composer set up for your project
12-
```sh
12+
```bash
1313
composer require cache/doctrine-adapter
1414
```
1515

@@ -19,42 +19,14 @@ composer require cache/doctrine-adapter
1919
use Doctrine\Common\Cache\MemcachedCache;
2020
use Cache\Doctrine\CachePool;
2121

22-
// Create a instance of Doctrine's MemcachedCache
22+
2323
$memcached = new \Memcached();
2424
$memcached->addServer('localhost', 11211);
25+
26+
// Create a instance of Doctrine's MemcachedCache
2527
$doctrineCache = new MemcachedCache();
2628
$doctrineCache->setMemcached($memcached);
2729

2830
// Wrap Doctrine's cache with the PSR-6 adapter
2931
$pool = new CachePool($doctrineCache);
30-
31-
/** @var CacheItemInterface $item */
32-
$item = $pool->getItem('key');
33-
```
34-
35-
## Tagging
36-
37-
The `CachePool` implements `Cache\Taggable\TaggablePoolInterface` from [Taggable Cache](https://github.com/php-cache/taggable-cache).
38-
Below is an example of how you could use tags:
39-
40-
```php
41-
42-
$item = $pool->getItem('tobias', ['person']);
43-
$item->set('foobar');
44-
$pool->save($item);
45-
46-
$item = $pool->getItem('aaron', ['person', 'developer']);
47-
$item->set('foobar');
48-
$pool->save($item);
49-
50-
$pool->getItem('tobias', ['person'])->isHit(); // true
51-
$pool->getItem('aaron', ['person', 'developer'])->isHit(); // true
52-
53-
// Clear all cache items tagged with 'developer'
54-
$pool->clear(['developer']);
55-
56-
$pool->getItem('tobias', ['person'])->isHit(); // true
57-
$pool->getItem('aaron', ['person', 'developer'])->isHit(); // false
5832
```
59-
60-
See more example and understand how you use tags here: https://github.com/php-cache/taggable-cache

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
],
2828
"require":
2929
{
30-
"psr/cache": "1.0.0",
31-
"cache/adapter-common": "^0.1",
32-
"cache/taggable-cache": "^0.2",
30+
"psr/cache": "1.0",
31+
"cache/adapter-common": "^0.2",
32+
"cache/taggable-cache": "^0.3",
3333
"doctrine/cache": "^1.3"
3434
},
3535
"require-dev":
3636
{
3737
"phpunit/phpunit": "^5.1|^4.0",
3838
"mockery/mockery": "^0.9.4",
39-
"cache/integration-tests": "dev-master"
39+
"cache/integration-tests": "^0.7"
4040
},
4141
"provide":
4242
{

src/DoctrineCachePool.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public function __construct(Cache $cache)
3939

4040
protected function fetchObjectFromCache($key)
4141
{
42-
return $this->cache->fetch($key);
42+
if (false === $data = $this->cache->fetch($key)) {
43+
return [false, null];
44+
}
45+
46+
return [true, unserialize($data)];
4347
}
4448

4549
protected function clearAllObjectsFromCache()
@@ -62,7 +66,7 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
6266
$ttl = 0;
6367
}
6468

65-
return $this->cache->save($key, $item, $ttl);
69+
return $this->cache->save($key, serialize($item->get()), $ttl);
6670
}
6771

6872
/**

tests/CachePoolTest.php

Lines changed: 0 additions & 155 deletions
This file was deleted.

tests/CreatePoolTrait.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\doctrine-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Doctrine\Tests;
13+
14+
use Cache\Adapter\Doctrine\DoctrineCachePool;
15+
use Doctrine\Common\Cache\ArrayCache;
16+
17+
trait CreatePoolTrait
18+
{
19+
private $doctrineCache = null;
20+
21+
public function createCachePool()
22+
{
23+
return new DoctrineCachePool($this->getDoctrineCache());
24+
}
25+
26+
private function getDoctrineCache()
27+
{
28+
if ($this->doctrineCache === null) {
29+
$this->doctrineCache = new ArrayCache();
30+
}
31+
32+
return $this->doctrineCache;
33+
}
34+
}

tests/DoctrineAdapterTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\doctrine-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Doctrine\Tests;
13+
14+
use Cache\Adapter\Common\CacheItem;
15+
use Cache\Adapter\Doctrine\DoctrineCachePool;
16+
use Doctrine\Common\Cache\Cache;
17+
use Doctrine\Common\Cache\FlushableCache;
18+
use Mockery as m;
19+
use Mockery\MockInterface;
20+
use Psr\Cache\CacheItemPoolInterface;
21+
22+
/**
23+
* @author Aaron Scherer <[email protected]>
24+
*/
25+
class DoctrineAdapterTest extends \PHPUnit_Framework_TestCase
26+
{
27+
/**
28+
* @type DoctrineCachePool
29+
*/
30+
private $pool;
31+
32+
/**
33+
* @type MockInterface|CacheItem
34+
*/
35+
private $mockItem;
36+
37+
/**
38+
* @type MockInterface|Cache
39+
*/
40+
private $mockDoctrine;
41+
42+
protected function setUp()
43+
{
44+
$this->mockItem = m::mock(CacheItem::class);
45+
$this->mockDoctrine = m::mock(Cache::class);
46+
47+
$this->pool = new DoctrineCachePool($this->mockDoctrine);
48+
}
49+
50+
public function testConstructor()
51+
{
52+
$this->assertInstanceOf(DoctrineCachePool::class, $this->pool);
53+
$this->assertInstanceOf(CacheItemPoolInterface::class, $this->pool);
54+
}
55+
56+
public function testGetCache()
57+
{
58+
$this->assertInstanceOf(Cache::class, $this->pool->getCache());
59+
$this->assertEquals($this->mockDoctrine, $this->pool->getCache());
60+
}
61+
62+
public function testClear()
63+
{
64+
$this->assertFalse($this->pool->clear());
65+
66+
$cache = m::mock(Cache::class.','.FlushableCache::class);
67+
$cache->shouldReceive('flushAll')->andReturn(true);
68+
69+
$newPool = new DoctrineCachePool($cache);
70+
$this->assertTrue($newPool->clear());
71+
72+
$cache->shouldReceive('fetch');
73+
$cache->shouldReceive('save');
74+
$this->assertTrue($newPool->clear(['dummy_tag']));
75+
}
76+
}

0 commit comments

Comments
 (0)