Browse Source

20210818-improve-cache-with-fast-mode (#11)

Co-authored-by: Richard Dern <richard@athaliasoft.com>
Reviewed-on: #11
Co-authored-by: Richard Dern <richard@noreply.git.athaliasoft.com>
Co-committed-by: Richard Dern <richard@noreply.git.athaliasoft.com>
master 1.5.1
Richard Dern 2 months ago
parent
commit
a79e3760cb
  1. 24
      package/Analyzer.php
  2. 27
      tests/Feature/FastModeAndCacheTest.php

24
package/Analyzer.php

@ -64,8 +64,10 @@ class Analyzer extends GenericObject implements Cachable
* @var array
*/
protected $casts = [
'lastModified' => 'datetime',
'fastMode' => 'boolean'
'lastModified' => 'datetime',
'fastMode' => 'boolean',
'analyzisStartedAt' => 'datetime',
'analyzisDoneAt' => 'datetime'
];
/**
@ -154,9 +156,12 @@ class Analyzer extends GenericObject implements Cachable
{
$this->reset();
$this->fastMode = $fastMode;
$this->source = self::SOURCE_URL;
$this->sourceUrl = $url;
$this->forceFill([
'fastMode' => $fastMode,
'source' => self::SOURCE_URL,
'sourceUrl' => $url,
'analyzisStartedAt' => now()
]);
$client = Fetcher::fetch($url);
@ -205,7 +210,13 @@ class Analyzer extends GenericObject implements Cachable
$this->sourcePath = $file;
if ($this->hasCachedData()) {
return $this->loadFromCache();
$this->loadFromCache();
if ($fastMode) {
return $this;
}
$this->fastMode = false;
}
if (empty($this->fileSystem)) {
@ -275,6 +286,7 @@ class Analyzer extends GenericObject implements Cachable
}
$this->setAttribute('analyzers', data_get($attributes, 'analyzers', []));
$this->setAttribute('analyzisDoneAt', now());
}
/**

27
tests/Feature/FastModeAndCacheTest.php

@ -0,0 +1,27 @@
<?php
namespace Cyca\Analyzer\Tests\Feature;
use Cyca\Analyzer\Facades\Analyzer;
use Cyca\Analyzer\Tests\TestCase;
use Illuminate\Support\Facades\Http;
class FastModeAndCacheTest extends TestCase
{
public function test_cache_after_fast_mode()
{
$url = 'example.org';
Http::fake([
'*' => Http::response('<html><body>Success!</body></html>', 200)
]);
$analyzer = Analyzer::analyzeUrl('https://' . $url, true);
$this->assertTrue($analyzer->fastMode);
$analyzer = Analyzer::analyzeUrl('https://' . $url, false);
$this->assertFalse($analyzer->fastMode);
}
}
Loading…
Cancel
Save