From a5733f30c5f09da7fff3ebc5e2a6ecefb0035773 Mon Sep 17 00:00:00 2001 From: IRFA Date: Sun, 23 Feb 2020 10:03:20 +0700 Subject: [PATCH] add refesh command --- console/commands/RORefresh.php | 59 ++++++++++++++++++++++++ src/{Ongkir/Func => Caching}/ROCache.php | 42 +++++++++++++---- 2 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 console/commands/RORefresh.php rename src/{Ongkir/Func => Caching}/ROCache.php (60%) diff --git a/console/commands/RORefresh.php b/console/commands/RORefresh.php new file mode 100644 index 0000000..7743a99 --- /dev/null +++ b/console/commands/RORefresh.php @@ -0,0 +1,59 @@ + */ -namespace Irfa\RajaOngkir\Ongkir\Func; +namespace Irfa\RajaOngkir\Caching; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Cache; @@ -12,6 +12,9 @@ class ROCache { + private static $prov; + private static $city; + public static function cacheFile($name,$find = null){ $cache = Cache::get('cache-'.$name); if($find != null){ @@ -20,17 +23,36 @@ public static function cacheFile($name,$find = null){ return $cache; } public static function clearCache(){ - $prov = config('irfa.rajaongkir.province_table'); - $city = config('irfa.rajaongkir.city_table'); - if(Schema::hasTable($city) AND Schema::hasTable($prov)){ - echo "Clearing Cache...".PHP_EOL; - $count = DB::table($prov)->truncate(); - $count = DB::table($city)->truncate(); - echo "Cache Cleared."; + self::$prov = config('irfa.rajaongkir.province_table'); + self::$city = config('irfa.rajaongkir.city_table'); + $cache_type = strtolower(config('irfa.rajaongkir.cache_type')); + if($cache_type == "database"){ + if(Schema::hasTable(self::$city) AND Schema::hasTable(self::$prov)){ + echo "Clearing Cache...".PHP_EOL; + self::clearTable(); + echo "Cache Cleared."; + } else{ + echo "Failed. Cache table not found."; + return false; + } + } elseif($cache_type == "file"){ + echo "Clearing Cache...".PHP_EOL; + self::clearFile(); + echo "Cache Cleared."; } else{ - echo "Failed."; - return false; + echo "Failed. Cache type not support."; + return false; + } } + + + private static function clearTable(){ + DB::table(self::$prov)->truncate(); + DB::table(self::$city)->truncate(); + } + private static function clearFile(){ + Cache::forget('ro-cache-'.self::$city); + Cache::forget('ro-cache-'.self::$prov); } public static function checkProv(){ $table = config('irfa.rajaongkir.province_table');