-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from skybluesofa/Atomic-Caching
Atomic caching
- Loading branch information
Showing
8 changed files
with
165 additions
and
84 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Apis\ColorNames; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class ColorNames | ||
{ | ||
protected $ttl = 60 * 60 * 24; | ||
|
||
public function get(string $hex): string | ||
{ | ||
return Cache::remember('color-name.'.$hex, $this->ttl, function () use ($hex) { | ||
return Http::get('https://colornames.org/search/json/?hex='.$hex)['name'] ?? $hex; | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Apis\GeoNames; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class GeoNames | ||
{ | ||
protected $ttl = 60 * 60 * 24; | ||
|
||
public function get(string $geonameId): array | ||
{ | ||
return Cache::remember('geoName.'.$geonameId, $this->ttl, function () use ($geonameId) { | ||
$url = 'https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/geonames-all-cities-with-a-population-500/records?select=coordinates,timezone&where=geoname_id%3D'.$geonameId.'&limit=1' ?? null; | ||
$response = Http::get($url)->body(); | ||
|
||
$json = json_decode($response, true); | ||
if (! isset($json['results'][0])) { | ||
return null; | ||
} | ||
|
||
return $json['results'][0]; | ||
}); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Facades; | ||
|
||
use App\Apis\ColorNames\ColorNames as ColorNamesApi; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
class ColorNames extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return ColorNamesApi::class; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Facades; | ||
|
||
use App\Apis\GeoNames\GeoNames as GeoNamesApi; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
class GeoNames extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return GeoNamesApi::class; | ||
} | ||
} |
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
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