Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit085ca60

Browse files
committed
[Cache] Create NullAdapter to disable cache if needed
1 parentea9d6e7 commit085ca60

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Cache\Adapter;
13+
14+
usePsr\Cache\CacheItemInterface;
15+
useSymfony\Component\Cache\CacheItem;
16+
17+
/**
18+
* @author Titouan Galopin <galopintitouan@gmail.com>
19+
*/
20+
class NullAdapterimplements AdapterInterface
21+
{
22+
private$createCacheItem;
23+
24+
publicfunction__construct()
25+
{
26+
$this->createCacheItem = \Closure::bind(
27+
function ($key) {
28+
$item =newCacheItem();
29+
$item->key =$key;
30+
$item->isHit =false;
31+
32+
return$item;
33+
},
34+
$this,
35+
CacheItem::class
36+
);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
publicfunctiongetItem($key)
43+
{
44+
$f =$this->createCacheItem;
45+
46+
return$f($key);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
publicfunctiongetItems(array$keys =array())
53+
{
54+
return$this->generateItems($keys);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
publicfunctionhasItem($key)
61+
{
62+
returnfalse;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
publicfunctionclear()
69+
{
70+
returntrue;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
publicfunctiondeleteItem($key)
77+
{
78+
returntrue;
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
publicfunctiondeleteItems(array$keys)
85+
{
86+
returntrue;
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
publicfunctionsave(CacheItemInterface$item)
93+
{
94+
returnfalse;
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
publicfunctionsaveDeferred(CacheItemInterface$item)
101+
{
102+
returnfalse;
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
publicfunctioncommit()
109+
{
110+
returnfalse;
111+
}
112+
113+
privatefunctiongenerateItems(array$keys)
114+
{
115+
$f =$this->createCacheItem;
116+
117+
foreach ($keysas$key) {
118+
yield$key =>$f($key);
119+
}
120+
}
121+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Cache\Tests\Adapter;
13+
14+
usePsr\Cache\CacheItemInterface;
15+
useSymfony\Component\Cache\Adapter\NullAdapter;
16+
17+
class NullAdapterTestextends \PHPUnit_Framework_TestCase
18+
{
19+
publicfunctioncreateCachePool()
20+
{
21+
returnnewNullAdapter();
22+
}
23+
24+
publicfunctiontestGetItem()
25+
{
26+
$adapter =$this->createCachePool();
27+
28+
$item =$adapter->getItem('key');
29+
$this->assertFalse($item->isHit());
30+
$this->assertNull($item->get(),"Item's value must be null when isHit is false.");
31+
}
32+
33+
publicfunctiontestHasItem()
34+
{
35+
$this->assertFalse($this->createCachePool()->hasItem('key'));
36+
}
37+
38+
publicfunctiontestGetItems()
39+
{
40+
$adapter =$this->createCachePool();
41+
42+
$keys =array('foo','bar','baz','biz');
43+
44+
/** @var CacheItemInterface[] $items */
45+
$items =$adapter->getItems($keys);
46+
$count =0;
47+
48+
foreach ($itemsas$key =>$item) {
49+
$itemKey =$item->getKey();
50+
51+
$this->assertEquals($itemKey,$key,'Keys must be preserved when fetching multiple items');
52+
$this->assertTrue(in_array($key,$keys),'Cache key can not change.');
53+
$this->assertFalse($item->isHit());
54+
55+
// Remove $key for $keys
56+
foreach ($keysas$k =>$v) {
57+
if ($v ===$key) {
58+
unset($keys[$k]);
59+
}
60+
}
61+
62+
++$count;
63+
}
64+
65+
$this->assertSame(4,$count);
66+
}
67+
68+
publicfunctiontestIsHit()
69+
{
70+
$adapter =$this->createCachePool();
71+
72+
$item =$adapter->getItem('key');
73+
$this->assertFalse($item->isHit());
74+
}
75+
76+
publicfunctiontestClear()
77+
{
78+
$this->assertTrue($this->createCachePool()->clear());
79+
}
80+
81+
publicfunctiontestDeleteItem()
82+
{
83+
$this->assertTrue($this->createCachePool()->deleteItem('key'));
84+
}
85+
86+
publicfunctiontestDeleteItems()
87+
{
88+
$this->assertTrue($this->createCachePool()->deleteItems(array('key','foo','bar')));
89+
}
90+
91+
publicfunctiontestSave()
92+
{
93+
$adapter =$this->createCachePool();
94+
95+
$item =$adapter->getItem('key');
96+
$this->assertFalse($item->isHit());
97+
$this->assertNull($item->get(),"Item's value must be null when isHit is false.");
98+
99+
$this->assertFalse($adapter->save($item));
100+
}
101+
102+
publicfunctiontestDeferredSave()
103+
{
104+
$adapter =$this->createCachePool();
105+
106+
$item =$adapter->getItem('key');
107+
$this->assertFalse($item->isHit());
108+
$this->assertNull($item->get(),"Item's value must be null when isHit is false.");
109+
110+
$this->assertFalse($adapter->saveDeferred($item));
111+
}
112+
113+
publicfunctiontestCommit()
114+
{
115+
$adapter =$this->createCachePool();
116+
117+
$item =$adapter->getItem('key');
118+
$this->assertFalse($item->isHit());
119+
$this->assertNull($item->get(),"Item's value must be null when isHit is false.");
120+
121+
$this->assertFalse($adapter->saveDeferred($item));
122+
$this->assertFalse($this->createCachePool()->commit());
123+
}
124+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp