Skip to content

Commit

Permalink
programmesPROGRAMMES-6052 findOnNowByService cache issues (#217)
Browse files Browse the repository at this point in the history
- Set max cache time to 300s
- Add function flushOnNowByService so we can flush it when an stale object still in memory and is not longer valid
  • Loading branch information
Tasiobg authored Nov 21, 2017
1 parent 6cf8b69 commit 81744cd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public function getOrSet(string $key, $ttl, callable $function, array $arguments
return $result;
}

/**
* @param string $key
* @return bool True if the item was successfully removed. False if there was an error.
*/
public function deleteItem(string $key): bool
{
$key = $this->standardiseKey($key);
return $this->cachePool->deleteItem($key);
}

/**
* Helps you to construct good cache keys by prodding you in the correct direction.
* Entirely optional but you are encouraged to use it.
Expand Down
2 changes: 2 additions & 0 deletions src/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function setItem(CacheItemInterface $item, $value, $ttl): bool;

public function getOrSet(string $key, $ttl, callable $function, array $arguments = [], $nullTtl = CacheInterface::NONE);

public function deleteItem(string $key): bool;

public function setFlushCacheItems(bool $flushCacheItems): void;

public function keyHelper(string $className, string $functionName, ...$uniqueValues): string;
Expand Down
24 changes: 23 additions & 1 deletion src/Service/BroadcastsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,30 @@ public function findOnNowByService(
$entity = $this->mapSingleEntity($dbEntity);

if ($entity) {
$this->cache->setItem($cacheItem, $entity, $entity->getEndAt());
// cache $entity with dynamic TTL
$now = ApplicationTime::getTime();
$diffInSeconds = $entity->getEndAt()->getTimestamp() - $now->getTimestamp();
if ($diffInSeconds > 300) {
// limit TTL to 5 minutes max
$ttl = 300;
} else {
$ttl = $diffInSeconds;
}
$this->cache->setItem($cacheItem, $entity, $ttl);
}
return $entity;
}

public function flushOnNowByService(
Service $service,
$ttl = CacheInterface::NORMAL
): bool {
$key = $this->cache->keyHelper(
__CLASS__,
'findOnNowByService',
$service->getDbId(),
$ttl
);
return $this->cache->deleteItem($key);
}
}
7 changes: 7 additions & 0 deletions tests/Service/BroadcastsService/FindOnNowByServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ public function testFindOnNowByServiceEmptyResults()

$this->assertSame(null, $broadcast);
}

public function testFlushOnNowByService()
{
$service = $this->createConfiguredMock(Service::class, ['getDbId' => 1]);
$result = $this->service()->flushOnNowByService($service);
$this->assertTrue($result);
}
}

0 comments on commit 81744cd

Please sign in to comment.