Skip to content

Commit 5ed671b

Browse files
committed
Merge pull request #2 from php-cache/develop
Implementation and travis fixes
2 parents 301ddcc + 1bf3893 commit 5ed671b

File tree

6 files changed

+128
-16
lines changed

6 files changed

+128
-16
lines changed

.travis.yml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,53 @@ php:
55
- 5.5
66
- hhvm
77

8+
sudo: required
9+
10+
env:
11+
global:
12+
- MONGODB_HOST="localhost:27017"
13+
- MONGODB_COLLECTION="test.cache"
14+
- KEY_SERVER="hkp://keyserver.ubuntu.com:80"
15+
- MONGO_REPO_URI="http://repo.mongodb.com/apt/ubuntu"
16+
- MONGO_REPO_TYPE="precise/mongodb-enterprise/"
17+
- SOURCES_LOC="/etc/apt/sources.list.d/mongodb.list"
18+
- SERVER_PACKAGE=mongodb-enterprise
19+
- SERVER_SERVICE=mongod
20+
- SERVER_VERSION=3.2
21+
822
services:
923
- mongodb
1024

1125
matrix:
1226
fast_finish: true
27+
allow_failures:
28+
- php: hhvm
1329

1430
before_install:
15-
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then phpenv config-rm xdebug.ini; fi;
16-
- pip install --user codecov
31+
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then phpenv config-rm xdebug.ini; fi;
32+
- pip install --user codecov
33+
34+
# Fetch mongo stuff
35+
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv 7F0CEB10
36+
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv EA312927
37+
- echo "deb ${MONGO_REPO_URI} ${MONGO_REPO_TYPE}${SERVER_VERSION} multiverse" | sudo tee ${SOURCES_LOC}
38+
- sudo apt-get update -qq
39+
40+
install:
41+
- sudo apt-get install ${SERVER_PACKAGE}
42+
- sudo apt-get -y install gdb
1743

1844
before_script:
19-
- travis_retry composer self-update
20-
- travis_retry composer install --prefer-source --no-interaction
45+
- if ! nc -z localhost 27017; then sudo service ${SERVER_SERVICE} start; fi
46+
- mongod --version
47+
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then pecl install -f mongodb-1.1.2; fi;
48+
- mongo --eval 'tojson(db.runCommand({buildInfo:1}))'
49+
- php --ri mongodb
50+
- travis_retry composer install --prefer-source --no-interaction
51+
- travis_retry composer self-update
2152

2253
script:
23-
- php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-clover=coverage.xml
54+
- php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-clover=coverage.xml
2455

2556
after_success:
26-
- codecov
57+
- codecov

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
</testsuite>
1818
</testsuites>
1919

20+
<php>
21+
<env name="MONGODB_HOST" value="localhost:27017"/>
22+
<env name="MONGODB_COLLECTION" value="psr6test.cache"/>
23+
</php>
24+
2025
<logging>
2126
<log type="coverage-text" target="php://stdout"/>
2227
<log type="coverage-html" target="./coverageReport"/>

src/MongoDBCachePool.php

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,82 @@
1212
namespace Cache\Adapter\MongoDB;
1313

1414
use Cache\Adapter\Common\AbstractCachePool;
15+
use Cache\Adapter\Common\CacheItem;
16+
use MongoDB\BSON\UTCDateTime;
17+
use MongoDB\Collection;
18+
use MongoDB\Driver\Manager;
1519
use Psr\Cache\CacheItemInterface;
16-
use MongoDB\Client;
1720

1821
/**
1922
* @author Tobias Nyholm <[email protected]>
2023
*/
2124
class MongoDBCachePool extends AbstractCachePool
2225
{
2326
/**
24-
* @var Client
27+
* @type Collection
2528
*/
26-
private $client;
29+
private $collection;
2730

2831
/**
29-
*
30-
* @param Client $client
32+
* @param Collection $collection
3133
*/
32-
public function __construct(Client $client)
34+
public function __construct(Collection $collection)
3335
{
34-
$this->client = $client;
36+
$this->collection = $collection;
37+
}
38+
39+
public static function createCollection(Manager $manager, $namespace)
40+
{
41+
$collection = new Collection($manager, $namespace);
42+
$collection->createIndex(['expireAt' => 1], ['expireAfterSeconds' => 0]);
43+
44+
return $collection;
3545
}
3646

3747
protected function fetchObjectFromCache($key)
3848
{
49+
$object = $this->collection->findOne(['_id' => $key]);
50+
51+
if ($object && isset($object->data)) {
52+
$item = new CacheItem($key, true, unserialize($object->data));
53+
54+
if (isset($object->expiresAt)) {
55+
$item->expiresAt($object->expiresAt->toDateTime());
56+
}
57+
58+
return $item;
59+
}
60+
3961
return false;
4062
}
4163

4264
protected function clearAllObjectsFromCache()
4365
{
66+
$this->collection->deleteMany([]);
67+
4468
return true;
4569
}
4670

4771
protected function clearOneObjectFromCache($key)
4872
{
73+
$this->collection->deleteOne(['_id' => $key]);
74+
4975
return true;
5076
}
5177

5278
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
5379
{
80+
$object = [
81+
'_id' => $key,
82+
'data' => serialize($item->get()),
83+
];
84+
85+
if ($ttl) {
86+
$object['expiresAt'] = new UTCDateTime((time() + $ttl) * 1000);
87+
}
88+
89+
$this->collection->updateOne(['_id' => $key], ['$set' => $object], ['upsert' => true]);
90+
5491
return true;
5592
}
56-
}
93+
}

tests/CreateServerTrait.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\mongodb-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\MongoDB\Tests;
13+
14+
use Cache\Adapter\MongoDB\MongoDBCachePool;
15+
use MongoDB\Driver\Manager;
16+
17+
trait CreateServerTrait
18+
{
19+
private $collection = null;
20+
21+
/**
22+
* @return mixed
23+
*/
24+
public function getCollection()
25+
{
26+
if ($this->collection === null) {
27+
$manager = new Manager('mongodb://'.getenv('MONGODB_HOST'));
28+
29+
// In your own code, only do this *once* to initialize your cache
30+
$this->collection = MongoDBCachePool::createCollection($manager, getenv('MONGODB_COLLECTION'));
31+
}
32+
33+
return $this->collection;
34+
}
35+
}

tests/IntegrationPoolTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
class IntegrationPoolTest extends CachePoolTest
1818
{
19+
use CreateServerTrait;
20+
1921
public function createCachePool()
2022
{
21-
return new MongoDBCachePool();
23+
return new MongoDBCachePool($this->getCollection());
2224
}
2325
}

tests/IntegrationTagTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
class IntegrationTagTest extends TaggableCachePoolTest
1818
{
19+
use CreateServerTrait;
20+
1921
public function createCachePool()
2022
{
21-
return new MongoDBCachePool();
23+
return new MongoDBCachePool($this->getCollection());
2224
}
2325
}

0 commit comments

Comments
 (0)