Skip to content

Commit d92996b

Browse files
author
Thomas Rabaix
committed
Refactor CacheElement, Add Cache Adapter unit test
1 parent fdf568f commit d92996b

14 files changed

+367
-155
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
coverage

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: php
2+
php:
3+
- 5.3
4+
# - 5.4
5+
6+
env:
7+
- SYMFONY_VERSION=v2.0.10
8+
- SYMFONY_VERSION=origin/master
9+
10+
before_script: php Tests/tests/vendors.php

Adapter/ApcCache.php

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1616
use Symfony\Component\Routing\RouterInterface;
1717

18-
use Sonata\BlockBundle\Cache\CacheInterface;
18+
use Sonata\CacheBundle\Cache\CacheInterface;
1919
use Sonata\CacheBundle\Cache\CacheElement;
2020

2121
class ApcCache implements CacheInterface
@@ -43,15 +43,7 @@ public function __construct(RouterInterface $router, $token, $prefix, array $ser
4343
}
4444

4545
/**
46-
* @return string
47-
*/
48-
private function getToken()
49-
{
50-
return $this->token;
51-
}
52-
53-
/**
54-
* @return bool
46+
* {@inheritdoc}
5547
*/
5648
public function flushAll()
5749
{
@@ -87,58 +79,54 @@ public function flushAll()
8779
}
8880

8981
/**
90-
* @param array $keys
91-
* @return bool
82+
* {@inheritdoc}
9283
*/
9384
public function flush(array $keys = array())
9485
{
9586
return $this->flushAll();
9687
}
9788

9889
/**
99-
* @param CacheElement $cacheElement
100-
* @return bool|\string[]
90+
* {@inheritdoc}
10191
*/
102-
public function has(CacheElement $cacheElement)
92+
public function has(array $keys)
10393
{
104-
return apc_exists($this->computeCacheKeys($cacheElement));
94+
return apc_exists($this->computeCacheKeys($keys));
10595
}
10696

10797
/**
108-
* @param CacheElement $cacheElement
109-
* @return mixed
98+
* {@inheritdoc}
11099
*/
111-
public function set(CacheElement $cacheElement)
100+
public function set(array $keys, $data, $ttl = 84600, array $contextualKeys = array())
112101
{
113-
$return = apc_store(
114-
$this->computeCacheKeys($cacheElement),
115-
$cacheElement->getValue(),
102+
$cacheElement = new CacheElement($keys, $data, $ttl);
103+
104+
$result = apc_store(
105+
$this->computeCacheKeys($keys),
106+
$cacheElement,
116107
$cacheElement->getTtl()
117108
);
118109

119-
return $return;
110+
return $cacheElement;
120111
}
121112

122113
/**
123114
* @param CacheElement $cacheElement
124115
* @return string
125116
*/
126-
private function computeCacheKeys(CacheElement $cacheElement)
117+
private function computeCacheKeys($keys)
127118
{
128-
$keys = $cacheElement->getKeys();
129-
130119
ksort($keys);
131120

132121
return md5($this->prefix.serialize($keys));
133122
}
134123

135124
/**
136-
* @param CacheElement $cacheElement
137-
* @return mixed
125+
* {@inheritdoc}
138126
*/
139-
public function get(CacheElement $cacheElement)
127+
public function get(array $keys)
140128
{
141-
return apc_fetch($this->computeCacheKeys($cacheElement));
129+
return apc_fetch($this->computeCacheKeys($keys));
142130
}
143131

144132
/**
@@ -148,7 +136,7 @@ public function get(CacheElement $cacheElement)
148136
*/
149137
public function cacheAction($token)
150138
{
151-
if ($this->getToken() == $token) {
139+
if ($this->token == $token) {
152140
apc_clear_cache('user');
153141

154142
return new Response('ok', 200, array(
@@ -160,7 +148,7 @@ public function cacheAction($token)
160148
}
161149

162150
/**
163-
* @return bool
151+
* {@inheritdoc}
164152
*/
165153
public function isContextual()
166154
{

Adapter/MemcachedCache.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Symfony\Component\Routing\Router;
1414
use Symfony\Component\HttpFoundation\Response;
1515

16-
use Sonata\BlockBundle\Cache\CacheInterface;
16+
use Sonata\CacheBundle\Cache\CacheInterface;
1717
use Sonata\CacheBundle\Cache\CacheElement;
1818

1919
class MemcachedCache implements CacheInterface
@@ -35,33 +35,31 @@ public function __construct($prefix, array $servers)
3535
}
3636

3737
/**
38-
* @return bool
38+
* {@inheritdoc}
3939
*/
4040
public function flushAll()
4141
{
4242
return $this->getCollection()->flush();
4343
}
4444

4545
/**
46-
* @param array $keys
47-
* @return bool
46+
* {@inheritdoc}
4847
*/
4948
public function flush(array $keys = array())
5049
{
51-
return $this->getCollection()->delete($this->computeCacheKeys(new CacheElement($keys)));
50+
return $this->getCollection()->delete($this->computeCacheKeys($keys));
5251
}
5352

5453
/**
55-
* @param CacheElement $cacheElement
56-
* @return bool
54+
* {@inheritdoc}
5755
*/
58-
public function has(CacheElement $cacheElement)
56+
public function has(array $keys)
5957
{
60-
return $this->getCollection()->get($this->computeCacheKeys($cacheElement)) !== false;
58+
return $this->getCollection()->get($this->computeCacheKeys($keys)) !== false;
6159
}
6260

6361
/**
64-
* @return \Memcached
62+
* {@inheritdoc}
6563
*/
6664
private function getCollection()
6765
{
@@ -77,44 +75,41 @@ private function getCollection()
7775
}
7876

7977
/**
80-
* @param CacheElement $cacheElement
81-
* @return mixed
78+
* {@inheritdoc}
8279
*/
83-
public function set(CacheElement $cacheElement)
80+
public function set(array $keys, $data, $ttl = 84600, array $contextualKeys = array())
8481
{
85-
$return = $this->getCollection()->set(
86-
$this->computeCacheKeys($cacheElement),
87-
$cacheElement->getValue(),
82+
$cacheElement = new CacheElement($keys, $data, $ttl);
83+
84+
$this->getCollection()->set(
85+
$this->computeCacheKeys($keys),
86+
$cacheElement,
8887
time() + $cacheElement->getTtl()
8988
);
9089

91-
return $return;
90+
return $cacheElement;
9291
}
9392

9493
/**
95-
* @param CacheElement $cacheElement
96-
* @return string
94+
* {@inheritdoc}
9795
*/
98-
private function computeCacheKeys(CacheElement $cacheElement)
96+
private function computeCacheKeys(array $keys)
9997
{
100-
$keys = $cacheElement->getKeys();
101-
10298
ksort($keys);
10399

104100
return md5($this->prefix.serialize($keys));
105101
}
106102

107103
/**
108-
* @param CacheElement $cacheElement
109-
* @return mixed
104+
* {@inheritdoc}
110105
*/
111-
public function get(CacheElement $cacheElement)
106+
public function get(array $keys)
112107
{
113-
return $this->getCollection()->get($this->computeCacheKeys($cacheElement));
108+
return $this->getCollection()->get($this->computeCacheKeys($keys));
114109
}
115110

116111
/**
117-
* @return bool
112+
* {@inheritdoc}
118113
*/
119114
public function isContextual()
120115
{

Adapter/MongoCache.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
use Symfony\Component\Routing\Router;
1414
use Symfony\Component\HttpFoundation\Response;
1515

16-
use Sonata\BlockBundle\Cache\CacheInterface;
16+
use Sonata\CacheBundle\Cache\CacheInterface;
1717
use Sonata\CacheBundle\Cache\CacheElement;
1818

1919
class MongoCache implements CacheInterface
2020
{
21-
protected $settings;
21+
private $servers;
22+
23+
private $databaseName;
24+
25+
private $collectionName;
2226

2327
protected $collection;
2428

@@ -29,37 +33,32 @@ class MongoCache implements CacheInterface
2933
*/
3034
public function __construct(array $servers, $database, $collection)
3135
{
32-
$this->settings = array(
33-
'servers' => $servers,
34-
'database' => $database,
35-
'collection' => $collection
36-
);
36+
$this->servers = $servers;
37+
$this->databaseName = $database;
38+
$this->collectionName = $collection;
3739
}
3840

3941
/**
40-
* @return mixed
42+
* {@inheritdoc}
4143
*/
4244
public function flushAll()
4345
{
4446
return $this->getCollection()->remove(array());
4547
}
4648

4749
/**
48-
* @param array $keys
49-
* @return mixed
50+
* {@inheritdoc}
5051
*/
5152
public function flush(array $keys = array())
5253
{
5354
return $this->getCollection()->remove($keys);
5455
}
5556

5657
/**
57-
* @param CacheElement $cacheElement
58-
* @return bool
58+
* {@inheritdoc}
5959
*/
60-
public function has(CacheElement $cacheElement)
60+
public function has(array $keys)
6161
{
62-
$keys = $cacheElement->getKeys();
6362
$keys['_timeout'] = array('$gt' => time());
6463

6564
return $this->getCollection()->count($keys) > 0;
@@ -68,55 +67,54 @@ public function has(CacheElement $cacheElement)
6867
/**
6968
* @return \MongoCollection
7069
*/
71-
public function getCollection()
70+
private function getCollection()
7271
{
7372
if (!$this->collection) {
74-
$mongo = new \Mongo(sprintf('mongodb://%s', implode(',', $this->settings['servers'])));
73+
$mongo = new \Mongo(sprintf('mongodb://%s', implode(',', $this->servers)));
7574

7675
$this->collection = $mongo
77-
->selectDB($this->settings['database'])
78-
->selectCollection($this->settings['collection']);
76+
->selectDB($this->databaseName)
77+
->selectCollection($this->collectionName);
7978
}
8079

8180
return $this->collection;
8281
}
8382

8483
/**
85-
* @param CacheElement $cacheElement
86-
* @return mixed
84+
* {@inheritdoc}
8785
*/
88-
public function set(CacheElement $cacheElement)
86+
public function set(array $keys, $data, $ttl = 84600, array $contextualKeys = array())
8987
{
9088
$time = time();
9189

90+
$cacheElement = new CacheElement($keys, $data, $ttl, $contextualKeys);
91+
9292
$keys = $cacheElement->getContextualKeys() + $cacheElement->getKeys();
93-
$keys['_value'] = new \MongoBinData(serialize($cacheElement->getValue()));
93+
$keys['_value'] = new \MongoBinData(serialize($cacheElement));
9494
$keys['_updated_at'] = $time;
9595
$keys['_timeout'] = $time + $cacheElement->getTtl();
9696

97-
$return = $this->getCollection()->save($keys);
97+
$this->getCollection()->save($keys);
9898

99-
return $return;
99+
return $cacheElement;
100100
}
101101

102102
/**
103-
* @param CacheElement $cacheElement
104-
* @return mixed
103+
* {@inheritdoc}
105104
*/
106-
public function get(CacheElement $cacheElement)
105+
public function get(array $keys)
107106
{
108-
$record = $this->getRecord($cacheElement);
107+
$record = $this->getRecord($keys);
109108

110109
return $record ? unserialize($record['_value']->bin) : null;
111110
}
112111

113112
/**
114-
* @param CacheElement $cacheElement
113+
* @param array $keys
115114
* @return array|null
116115
*/
117-
public function getRecord(CacheElement $cacheElement)
116+
private function getRecord(array $keys)
118117
{
119-
$keys = $cacheElement->getKeys();
120118
$keys['_timeout'] = array('$gt' => time());
121119

122120
$results = $this->getCollection()->find($keys);
@@ -129,7 +127,7 @@ public function getRecord(CacheElement $cacheElement)
129127
}
130128

131129
/**
132-
* @return bool
130+
* {@inheritdoc}
133131
*/
134132
public function isContextual()
135133
{

0 commit comments

Comments
 (0)