This commit is contained in:
James Brooks
2015-01-29 10:46:34 +00:00
parent 930d0d2fb9
commit 283685ca40
+23 -14
View File
@@ -5,6 +5,7 @@ namespace CachetHQ\Cachet\Segment;
use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Setting as SettingModel; use CachetHQ\Cachet\Models\Setting as SettingModel;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\QueryException;
class CacheRepository implements RepositoryInterface class CacheRepository implements RepositoryInterface
{ {
@@ -30,24 +31,32 @@ class CacheRepository implements RepositoryInterface
*/ */
public function fetch() public function fetch()
{ {
// Firstly, does the setting exist? $writeKey = null;
if (false === ($writeKey = Setting::get('segment_write_key'))) {
// No, let's go fetch it.
$writeKey = $this->repository->fetch();
Setting::set('segment_write_key', $writeKey);
} else {
// It does, but how old is it?
$setting = SettingModel::where('name', 'segment_write_key')->first();
// It's older than an hour, let's refresh // We might not be setup yet.
if ($setting->updated_at->lt(Carbon::now()->subHour())) { try {
// Firstly, does the setting exist?
if (false === ($writeKey = Setting::get('segment_write_key'))) {
// No, let's go fetch it.
$writeKey = $this->repository->fetch(); $writeKey = $this->repository->fetch();
Setting::set('segment_write_key', $writeKey);
} else {
// It does, but how old is it?
$setting = SettingModel::where('name', 'segment_write_key')->first();
// Update the setting. This is done manual to make sure updated_at is overwritten. // It's older than an hour, let's refresh
$setting->value = $writeKey; if ($setting->updated_at->lt(Carbon::now()->subHour())) {
$setting->updated_at = Carbon::now(); $writeKey = $this->repository->fetch();
$setting->save();
// Update the setting. This is done manual to make sure updated_at is overwritten.
$setting->value = $writeKey;
$setting->updated_at = Carbon::now();
$setting->save();
}
} }
} catch (QueryException $e) {
// Just return it until we're setup.
$writeKey = $this->repository->fetch();
} }
return $writeKey; return $writeKey;