Skip to content

Commit

Permalink
feat: add disable cache flag
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Oct 25, 2020
1 parent 702ba1d commit ab1d451
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Flags:
-v --version - Get version number
--silent - No output and prompt
--disable-cache - Disable Cache
--disable-colors - Disable CLI colors
--limit="" - Set file mapping limit
Expand Down
12 changes: 12 additions & 0 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ protected function __construct()
*/
public function set($key, $value, $ttl = 3600)
{
if (!Scanner::isCacheEnabled()) {
return true;
}

$key = $this->key($key);

if ($data = json_encode(array('ttl' => $ttl > 0 ? time() + $ttl : $ttl, 'data' => $value))) {
Expand All @@ -63,6 +67,10 @@ public function set($key, $value, $ttl = 3600)
*/
public function touch($key, $ttl = 3600)
{
if (!Scanner::isCacheEnabled()) {
return true;
}

if ($data = $this->get($key)) {
return $this->set($key, $data, $ttl);
}
Expand All @@ -80,6 +88,10 @@ public function touch($key, $ttl = 3600)
*/
public function get($key, $default = null)
{
if (!Scanner::isCacheEnabled()) {
return $default;
}

$key = $this->key($key);
$file = $this->tempdir . $key;

Expand Down
1 change: 1 addition & 0 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ public static function helper()
-v --version - Get version number
--silent - No output and prompt
--disable-cache - Disable Cache
--disable-colors - Disable CLI colors
--limit="" - Set file mapping limit
Expand Down
26 changes: 25 additions & 1 deletion src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ private function arguments($args = null)
self::$argv->addFlag('path-quarantine', array('default' => false, 'has_value' => true));
self::$argv->addFlag('path-logs', array('default' => false, 'has_value' => true));
self::$argv->addFlag('path-report', array('default' => false, 'has_value' => true));
self::$argv->addFlag('disable-colors', array('default' => false));
self::$argv->addFlag('disable-colors', array('alias' => array('--no-colors', '--no-color'), 'default' => false));
self::$argv->addFlag('disable-cache', array('alias' => '--no-cache', 'default' => false));
self::$argv->addArgument('path', array('var_args' => true, 'default' => ''));
self::$argv->parse($args);

Expand Down Expand Up @@ -411,6 +412,11 @@ private function arguments($args = null)
}
}

// Cache
if (isset(self::$argv['disable-cache']) && self::$argv['disable-cache']) {
self::setCache(false);
}

// Max filesize
if (isset(self::$argv['max-filesize']) && is_numeric(self::$argv['max-filesize'])) {
self::setMaxFilesize(self::$argv['max-filesize']);
Expand Down Expand Up @@ -1435,6 +1441,24 @@ public static function isColorEnabled()
return isset(self::$settings['colors']) ? self::$settings['colors'] : true;
}

/**
* @return self
*/
public static function setCache($mode = true)
{
self::$settings['cache'] = $mode;

return new static();
}

/**
* @return bool
*/
public static function isCacheEnabled()
{
return isset(self::$settings['cache']) ? self::$settings['cache'] : true;
}

/**
* @return self
*/
Expand Down

0 comments on commit ab1d451

Please sign in to comment.