From 3136f8e13754468f9305c3721325cebc6f6232f7 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 7 May 2021 21:34:58 -0600 Subject: [PATCH] Add ConfigCacheService --- app/Services/ConfigCacheService.php | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 app/Services/ConfigCacheService.php diff --git a/app/Services/ConfigCacheService.php b/app/Services/ConfigCacheService.php new file mode 100644 index 000000000..9914100c5 --- /dev/null +++ b/app/Services/ConfigCacheService.php @@ -0,0 +1,69 @@ +addHours(12); + return Cache::remember($cacheKey, $ttl, function() use($key) { + + $allowed = [ + 'app.name', + 'app.short_description', + 'app.description', + 'app.rules', + 'app.logo' + ]; + + if(!in_array($key, $allowed)) { + return config($key); + } + + $v = config($key); + $c = ConfigCacheModel::where('k', $key)->first(); + + if($c) { + return $c->v; + } + + if(!$v) { + return; + } + + $cc = new ConfigCacheModel; + $cc->k = $key; + $cc->v = $v; + $cc->save(); + + return $v; + }); + } + + public static function put($key, $val) + { + $exists = ConfigCacheModel::whereK($key)->first(); + + if($exists) { + $exists->v = $val; + $exists->save(); + Cache::forget(self::CACHE_KEY . $key); + return self::get($key); + } + + $cc = new ConfigCacheModel; + $cc->k = $key; + $cc->v = $val; + $cc->save(); + + return self::get($key); + } +}