Last active
June 2, 2024 13:15
-
-
Save winarcooo/5e72aa36a8a6a232e2a244717abfaf6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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