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

Commit54543ae

Browse files
[Cache] Prevent stampede at warmup using apcu_entry() for locking
1 parent5c338cc commit54543ae

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ public static function createConnection($dsn, array $options = array())
157157
thrownewInvalidArgumentException(sprintf('Unsupported DSN: %s.',$dsn));
158158
}
159159

160+
/**
161+
* {@inheritdoc}
162+
*/
163+
publicfunctionget(string$key,callable$callback,float$beta =null)
164+
{
165+
return$this->doGet($this,$key,$callback,$beta ??1.0,'' !==$this->namespace ?$this->getId($key) :null);
166+
}
167+
160168
/**
161169
* {@inheritdoc}
162170
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function (CacheItemInterface $innerItem, array $item) {
9494
publicfunctionget(string$key,callable$callback,float$beta =null)
9595
{
9696
if (!$this->poolinstanceof CacheInterface) {
97-
return$this->doGet($this,$key,$callback,$beta ??1.0);
97+
return$this->doGet($this,$key,$callback,$beta ??1.0,$this->namespaceLen ?$this->getId($key) :null);
9898
}
9999

100100
return$this->pool->get($this->getId($key),function ($innerItem)use ($key,$callback) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class TagAwareAdapter implements CacheInterface, TagAwareAdapterInterface, Prune
3535
private$setCacheItemTags;
3636
private$getTagsByKey;
3737
private$invalidateTags;
38+
private$getId;
3839
private$tags;
3940
private$knownTagVersions =array();
4041
private$knownTagVersionsTtl;
@@ -105,6 +106,13 @@ function (AdapterInterface $tagsAdapter, array $tags) {
105106
null,
106107
CacheItem::class
107108
);
109+
$this->getId = \Closure::bind(
110+
function (AbstractAdapter$pool,$key) {
111+
return$pool->getId($key);
112+
},
113+
null,
114+
AbstractAdapter::class
115+
);
108116
}
109117

110118
/**
@@ -150,6 +158,22 @@ public function invalidateTags(array $tags)
150158
return$ok;
151159
}
152160

161+
/**
162+
* {@inheritdoc}
163+
*/
164+
publicfunctionget(string$key,callable$callback,float$beta =null)
165+
{
166+
if ($this->poolinstanceof AbstractAdapter) {
167+
$id = ($this->getId)($this->pool,$key);
168+
}elseif ($this->poolinstanceof ProxyAdapter) {
169+
$id = ((array)$this->pool)["\0Symfony\\Component\\Cache\\Adapter\\ProxyAdapter\0namespace"].$key;
170+
}else {
171+
$id =null;
172+
}
173+
174+
return$this->doGet($this,$key,$callback,$beta ??1.0,$id !==$key ?$id :null);
175+
}
176+
153177
/**
154178
* {@inheritdoc}
155179
*/

‎src/Symfony/Component/Cache/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added`CacheInterface`, which provides stampede protection via probabilistic early expiration and should become the preferred way to use a cache
8+
* added warmup-time stampede protection using`apcu_entry()` for locking when available
89
* added sub-second expiry accuracy for backends that support it
910
* throw`LogicException` when`CacheItem::tag()` is called on an item coming from a non tag-aware pool
1011
* deprecated`CacheItem::getPreviousTags()`, use`CacheItem::getMetadata()` instead

‎src/Symfony/Component/Cache/Traits/GetTrait.php‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function get(string $key, callable $callback, float $beta = null)
3434
return$this->doGet($this,$key,$callback,$beta ??1.0);
3535
}
3636

37-
privatefunctiondoGet(CacheItemPoolInterface$pool,string$key,callable$callback,float$beta)
37+
privatefunctiondoGet(CacheItemPoolInterface$pool,string$key,callable$callback,float$beta,string$lockId =null)
3838
{
3939
$t =0;
4040
$item =$pool->getItem($key);
@@ -64,6 +64,7 @@ private function doGet(CacheItemPoolInterface $pool, string $key, callable $call
6464
}
6565

6666
static$save =null;
67+
static$useApcu =null;
6768

6869
if (null ===$save) {
6970
$save = \Closure::bind(
@@ -81,6 +82,26 @@ function (CacheItemPoolInterface $pool, CacheItemInterface $item, $value, float
8182
);
8283
}
8384

84-
return$save($pool,$item,$callback($item),$t);
85+
if (null ===$lockId || !$useApcu =$useApcu ??\function_exists('apcu_entry') &&ini_get('apc.enabled') && ('cli' !== \PHP_SAPI ||ini_get('apc.enable_cli'))) {
86+
return$save($pool,$item,$callback($item),$t);
87+
}
88+
89+
$t =$t ?:microtime(true);
90+
$lockId =':'.$lockId;
91+
$isHit =true;
92+
$value =apcu_entry($lockId,function ()use ($callback,$item, &$isHit) {
93+
$isHit =false;
94+
95+
return$callback($item);
96+
},2);
97+
98+
if (!$isHit) {
99+
$save($pool,$item,$value,$t);
100+
// give some time to concurrent processes to get the value from APCu
101+
usleep(min(300000, (microtime(true) -$t) *150000));
102+
apcu_delete($lockId);
103+
}
104+
105+
return$value;
85106
}
86107
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp