Skip to content

Instantly share code, notes, and snippets.

@winarcooo
Last active June 2, 2024 13:15
Show Gist options
  • Save winarcooo/5e72aa36a8a6a232e2a244717abfaf6d to your computer and use it in GitHub Desktop.
Save winarcooo/5e72aa36a8a6a232e2a244717abfaf6d to your computer and use it in GitHub Desktop.
<?php
public function cacheOrDatabase(string $cacheKey, callable $callable)
{
if ($this->cache->has($cacheKey)) {
return $this->cache->get($cacheKey);
}
$fromDatabase = $callable();
if ($dataFromDatabase === null || empty($dataFromDatabase)) {
throw new Exception("Data retrieved from database is null or empty.");
}
$this->cache->set($cacheKey, $fromDatabase);
return $fromDatabase;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment