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

Commit221cbb5

Browse files
[Cache] add CacheInterface::delete() + improve CacheTrait
1 parent0f653d8 commit221cbb5

15 files changed

+201
-119
lines changed

‎src/Symfony/Component/Cache/Adapter/AbstractAdapter.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
useSymfony\Component\Cache\Exception\InvalidArgumentException;
2020
useSymfony\Component\Cache\ResettableInterface;
2121
useSymfony\Component\Cache\Traits\AbstractTrait;
22-
useSymfony\Component\Cache\Traits\GetTrait;
22+
useSymfony\Component\Cache\Traits\ContractsTrait;
2323
useSymfony\Contracts\Cache\CacheInterface;
2424

2525
/**
@@ -28,7 +28,7 @@
2828
abstractclass AbstractAdapterimplements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
2929
{
3030
use AbstractTrait;
31-
useGetTrait;
31+
useContractsTrait;
3232

3333
privatestatic$apcuSupported;
3434
privatestatic$phpFilesSupported;

‎src/Symfony/Component/Cache/Adapter/ArrayAdapter.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,12 @@ public function commit()
151151
{
152152
returntrue;
153153
}
154+
155+
/**
156+
* {@inheritdoc}
157+
*/
158+
publicfunctiondelete(string$key):bool
159+
{
160+
return$this->deleteItem($key);
161+
}
154162
}

‎src/Symfony/Component/Cache/Adapter/ChainAdapter.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
useSymfony\Component\Cache\Exception\InvalidArgumentException;
1818
useSymfony\Component\Cache\PruneableInterface;
1919
useSymfony\Component\Cache\ResettableInterface;
20-
useSymfony\Component\Cache\Traits\GetTrait;
20+
useSymfony\Component\Cache\Traits\ContractsTrait;
2121
useSymfony\Contracts\Cache\CacheInterface;
2222
useSymfony\Contracts\Service\ResetInterface;
2323

@@ -31,7 +31,7 @@
3131
*/
3232
class ChainAdapterimplements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
3333
{
34-
useGetTrait;
34+
useContractsTrait;
3535

3636
private$adapters =array();
3737
private$adapterCount;

‎src/Symfony/Component/Cache/Adapter/NullAdapter.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public function commit()
119119
returnfalse;
120120
}
121121

122+
/**
123+
* {@inheritdoc}
124+
*/
125+
publicfunctiondelete(string$key):bool
126+
{
127+
return$this->deleteItem($key);
128+
}
129+
122130
privatefunctiongenerateItems(array$keys)
123131
{
124132
$f =$this->createCacheItem;

‎src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
useSymfony\Component\Cache\Exception\InvalidArgumentException;
1818
useSymfony\Component\Cache\PruneableInterface;
1919
useSymfony\Component\Cache\ResettableInterface;
20-
useSymfony\Component\Cache\Traits\GetTrait;
20+
useSymfony\Component\Cache\Traits\ContractsTrait;
2121
useSymfony\Component\Cache\Traits\PhpArrayTrait;
2222
useSymfony\Contracts\Cache\CacheInterface;
2323

@@ -31,7 +31,7 @@
3131
class PhpArrayAdapterimplements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
3232
{
3333
use PhpArrayTrait;
34-
useGetTrait;
34+
useContractsTrait;
3535

3636
private$createCacheItem;
3737

‎src/Symfony/Component/Cache/Adapter/ProxyAdapter.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
useSymfony\Component\Cache\CacheItem;
1717
useSymfony\Component\Cache\PruneableInterface;
1818
useSymfony\Component\Cache\ResettableInterface;
19-
useSymfony\Component\Cache\Traits\GetTrait;
19+
useSymfony\Component\Cache\Traits\ContractsTrait;
2020
useSymfony\Component\Cache\Traits\ProxyTrait;
2121
useSymfony\Contracts\Cache\CacheInterface;
2222

@@ -26,7 +26,7 @@
2626
class ProxyAdapterimplements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
2727
{
2828
use ProxyTrait;
29-
useGetTrait;
29+
useContractsTrait;
3030

3131
private$namespace;
3232
private$namespaceLen;

‎src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
useSymfony\Component\Cache\CacheItem;
1717
useSymfony\Component\Cache\PruneableInterface;
1818
useSymfony\Component\Cache\ResettableInterface;
19-
useSymfony\Component\Cache\Traits\GetTrait;
19+
useSymfony\Component\Cache\Traits\ContractsTrait;
2020
useSymfony\Component\Cache\Traits\ProxyTrait;
2121
useSymfony\Contracts\Cache\TagAwareCacheInterface;
2222

@@ -28,7 +28,7 @@ class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterfac
2828
constTAGS_PREFIX ="\0tags\0";
2929

3030
use ProxyTrait;
31-
useGetTrait;
31+
useContractsTrait;
3232

3333
private$deferred =array();
3434
private$createCacheItem;

‎src/Symfony/Component/Cache/Adapter/TraceableAdapter.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ public function reset()
237237
}
238238
}
239239

240+
/**
241+
* {@inheritdoc}
242+
*/
243+
publicfunctiondelete(string$key):bool
244+
{
245+
$event =$this->start(__FUNCTION__);
246+
try {
247+
return$event->result[$key] =$this->pool->deleteItem($key);
248+
}finally {
249+
$event->end =microtime(true);
250+
}
251+
}
252+
240253
publicfunctiongetCalls()
241254
{
242255
return$this->calls;

‎src/Symfony/Component/Cache/Traits/GetTrait.php‎renamed to ‎src/Symfony/Component/Cache/Traits/ContractsTrait.php‎

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616
useSymfony\Component\Cache\Exception\InvalidArgumentException;
1717
useSymfony\Component\Cache\LockRegistry;
1818
useSymfony\Contracts\Cache\CacheInterface;
19+
useSymfony\Contracts\Cache\CacheTrait;
1920
useSymfony\Contracts\Cache\ItemInterface;
2021

2122
/**
2223
* @author Nicolas Grekas <p@tchwork.com>
2324
*
2425
* @internal
2526
*/
26-
traitGetTrait
27+
traitContractsTrait
2728
{
29+
use CacheTrait {
30+
doGetasprivate contractsGet;
31+
}
32+
2833
private$callbackWrapper =array(LockRegistry::class,'compute');
2934

3035
/**
@@ -42,47 +47,12 @@ public function setCallbackWrapper(callable $callbackWrapper): callable
4247
return$previousWrapper;
4348
}
4449

45-
/**
46-
* {@inheritdoc}
47-
*/
48-
publicfunctionget(string$key,callable$callback,float$beta =null)
49-
{
50-
return$this->doGet($this,$key,$callback,$beta);
51-
}
52-
5350
privatefunctiondoGet(AdapterInterface$pool,string$key,callable$callback, ?float$beta)
5451
{
5552
if (0 >$beta =$beta ??1.0) {
5653
thrownewInvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.',\get_class($this),$beta));
5754
}
5855

59-
$t =0;
60-
$item =$pool->getItem($key);
61-
$recompute = !$item->isHit() ||INF ===$beta;
62-
63-
if (0 <$beta) {
64-
if ($recompute) {
65-
$t =microtime(true);
66-
}else {
67-
$metadata =$item->getMetadata();
68-
$expiry =$metadata[ItemInterface::METADATA_EXPIRY] ??false;
69-
$ctime =$metadata[ItemInterface::METADATA_CTIME] ??false;
70-
71-
if ($ctime &&$expiry) {
72-
$t =microtime(true);
73-
$recompute =$expiry <=$t -$ctime /1000 *$beta *log(random_int(1,PHP_INT_MAX) /PHP_INT_MAX);
74-
}
75-
}
76-
if ($recompute) {
77-
// force applying defaultLifetime to expiry
78-
$item->expiresAt(null);
79-
}
80-
}
81-
82-
if (!$recompute) {
83-
return$item->get();
84-
}
85-
8656
static$save;
8757

8858
$save =$save ?? \Closure::bind(
@@ -99,16 +69,19 @@ function (AdapterInterface $pool, ItemInterface $item, $value, float $startTime)
9969
CacheItem::class
10070
);
10171

102-
// don't wrap nor save recursive calls
103-
if (null ===$callbackWrapper =$this->callbackWrapper) {
104-
return$callback($item);
105-
}
106-
$this->callbackWrapper =null;
72+
return$this->contractsGet($pool,$key,function (CacheItem$item)use ($pool,$callback,$save) {
73+
// don't wrap nor save recursive calls
74+
if (null ===$callbackWrapper =$this->callbackWrapper) {
75+
return$callback($item);
76+
}
77+
$this->callbackWrapper =null;
78+
$t =microtime(true);
10779

108-
try {
109-
return$save($pool,$item,$callbackWrapper($item,$callback,$pool),$t);
110-
}finally {
111-
$this->callbackWrapper =$callbackWrapper;
112-
}
80+
try {
81+
return$save($pool,$item,$callbackWrapper($item,$callback,$pool),$t);
82+
}finally {
83+
$this->callbackWrapper =$callbackWrapper;
84+
}
85+
},$beta);
11386
}
11487
}

‎src/Symfony/Contracts/Cache/CacheInterface.php‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,46 @@
1111

1212
namespaceSymfony\Contracts\Cache;
1313

14+
usePsr\Cache\CacheItemInterface;
1415
usePsr\Cache\InvalidArgumentException;
1516

1617
/**
17-
*Gets/Stores items from/toa cache.
18+
*Covers most simpletoadvanced caching needs.
1819
*
1920
* On cache misses, a callback is called that should return the missing value.
20-
* This callback is given an ItemInterface object corresponding to the needed key,
21-
* that could be used e.g. for expiration control.
21+
* This callback is given a PSR-6 CacheItemInterface instance corresponding to the
22+
* requested key, that could be used e.g. for expiration control. It could also
23+
* be an ItemInterface instance when its additional features are needed.
2224
*
2325
* @author Nicolas Grekas <p@tchwork.com>
2426
*/
2527
interface CacheInterface
2628
{
2729
/**
28-
* @param string $key The key of the item to retrieve from the cache
29-
* @param callable(ItemInterface):mixed $callback Should return the computed value for the given key/item
30-
* @param float|null $beta A float that, as it grows, controls the likeliness of triggering
31-
* early expiration. 0 disables it, INF forces immediate expiration.
32-
* The default (or providing null) is implementation dependent but should
33-
* typically be 1.0, which should provide optimal stampede protection.
34-
* See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
30+
* Fetches a value from the pool or computes it if not found.
31+
*
32+
* @param string $key The key of the item to retrieve from the cache
33+
* @param callable(CacheItemInterface):mixed $callback Should return the computed value for the given key/item
34+
* @param float|null $beta A float that, as it grows, controls the likeliness of triggering
35+
* early expiration. 0 disables it, INF forces immediate expiration.
36+
* The default (or providing null) is implementation dependent but should
37+
* typically be 1.0, which should provide optimal stampede protection.
38+
* See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
3539
*
3640
* @return mixed The value corresponding to the provided key
3741
*
3842
* @throws InvalidArgumentException When $key is not valid or when $beta is negative
3943
*/
4044
publicfunctionget(string$key,callable$callback,float$beta =null);
45+
46+
/**
47+
* Removes the item from the pool.
48+
*
49+
* @param string $key The key to delete
50+
*
51+
* @throws InvalidArgumentException When $key is not valid
52+
*
53+
* @return bool True if the item was successfully removed, false if there was any error
54+
*/
55+
publicfunctiondelete(string$key):bool;
4156
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp