Skip to content

Commit

Permalink
add refesh command
Browse files Browse the repository at this point in the history
  • Loading branch information
irfaardy committed Feb 23, 2020
1 parent 63f131b commit a5733f3
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
59 changes: 59 additions & 0 deletions console/commands/RORefresh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Irfa\RajaOngkir\Caching\ROCache;
use Irfa\RajaOngkir\Ongkir\Ongkir as RajaOngkir;

class RORefresh extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'raja-ongkir:refresh';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh Cache';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
echo "---------------------".PHP_EOL;
echo"Refresh Cache".PHP_EOL;
echo"---------------------".PHP_EOL;
ROCache::clearCache();
sleep(1);//Cooling Down
echo PHP_EOL."---------------------".PHP_EOL;
echo"Province Caching".PHP_EOL;
echo"---------------------".PHP_EOL;
RajaOngkir::cachingProvince();
echo PHP_EOL."---------------------".PHP_EOL;
sleep(1);//Cooling Down
echo"City Caching".PHP_EOL;
echo "---------------------".PHP_EOL;
RajaOngkir::cachingCity();
echo PHP_EOL."---------------------".PHP_EOL;

}
}
42 changes: 32 additions & 10 deletions src/Ongkir/Func/ROCache.php → src/Caching/ROCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Raja Ongkir API
Author: Irfa Ardiansyah <irfa.backend@protonmail.com>
*/
namespace Irfa\RajaOngkir\Ongkir\Func;
namespace Irfa\RajaOngkir\Caching;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Cache;
Expand All @@ -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){
Expand All @@ -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');
Expand Down

0 comments on commit a5733f3

Please sign in to comment.