Skip to content

Commit

Permalink
When checking if results are fresh on freshness policy, unstage tag t…
Browse files Browse the repository at this point in the history
…ag from cache if the result is empty
  • Loading branch information
karptonite committed Oct 23, 2024
1 parent e4be762 commit 1f08546
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Cache/SoftInvalidatableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function stage(string $key): void
parent::stage($key);
}


public function get($key, callable $regenerator = null, $ttl = 0)
{
$regeneratedByParent = false;
Expand Down
9 changes: 9 additions & 0 deletions src/Cache/TaggedFreshnessPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public function computeTtl($ttl)
{
return $ttl;
}

public function resultIsFresh($result)
{
if (!($result instanceof CacheData)) {
$this->tagset->unstage();
return false;
}
return parent::resultIsFresh($result);
}

protected function isFresh($freshnessData)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public function getVersion()
$stored = $this->cache->get($this->key);
return $stored && is_string($stored) ? $stored : $this->clear();
}

public function unstage()
{
if ($this->staged) {
$this->cache->get($this->key);
}
}


public function clear()
{
Expand Down
7 changes: 7 additions & 0 deletions src/Tag/TagSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public function stage()
$tag->stage();
});
}

public function unstage()
{
array_walk($this->tags, function ($tag) {
$tag->unstage();
});
}
}
1 change: 1 addition & 0 deletions tests/Cache/CacheBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testTaggedBuild()
->andReturnNull();
$tagset = m::mock('GeekCache\Tag\TagSet');
$tagset->shouldReceive('stage');
$tagset->shouldReceive('unstage')->once();
$this->tagsetfactory->shouldReceive('makeTagSet')
->once()
->with(array('footag', 'bartag'))
Expand Down
9 changes: 9 additions & 0 deletions tests/Cache/TaggedCacheMemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public function testTaggedCacheInvalidates()
$this->assertEquals(self::VALUE, $cache->get(self::KEY));
$tagSet->clearAll();
$this->assertFalse($cache->get(self::KEY));
$this->assertStageEmpty();
}

public function testTaggedCacheLookupMiss()
{
$getCount = $this->parentcache->getGetCount();
$this->cache->get(self::KEY);
$this->assertEquals($getCount + 1, $this->parentcache->getGetCount());
$this->assertStageEmpty();
}

public function testTaggedCacheLookupHitsCacheOnce()
Expand Down
1 change: 1 addition & 0 deletions tests/Cache/TaggedCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function setUp(): void
->andReturn('foo')
->byDefault();
$this->tagset->shouldReceive('stage');
$this->tagset->shouldReceive('unstage');
$policy = new GeekCache\Cache\TaggedFreshnessPolicy($this->tagset);
$this->cache = new GeekCache\Cache\SoftInvalidatableCache($this->parentcache, $policy);
}
Expand Down

0 comments on commit 1f08546

Please sign in to comment.