Browse Source

Cache analyzis results

Closes #1
pull/10/head 1.2
Richard Dern 2 months ago
parent
commit
194cef2a99
  1. 8
      config/analyzer.php
  2. 73
      package/Analyzer.php
  3. 2
      package/Providers/AnalyzerServiceProvider.php

8
config/analyzer.php

@ -12,6 +12,14 @@ return [
*/
'storage_disk' => env('ANALYZER_STORAGE_DISK', 'local'),
/**
* Amount of seconds after which data stored in cache is considered
* obsolete.
*
* Defaults to: 6hrs (60 * 60 * 6)
*/
'cache_ttl' => env('ANALYZER_CACHE_TTL', 60 * 60 * 6),
/**
* Analyzers per mimetype.
*/

73
package/Analyzer.php

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Cyca\Analyzer;
use Carbon\Carbon;
use Cyca\Fetcher\Facades\Fetcher;
use Cyca\Helpers\Contracts\Cachable;
use Cyca\Helpers\Helpers\GenericObject;
use Exception;
use Illuminate\Contracts\Filesystem\Filesystem;
@ -15,7 +17,7 @@ use League\Flysystem\FileNotFoundException;
/**
* Analyze a document
*/
class Analyzer extends GenericObject
class Analyzer extends GenericObject implements Cachable
{
/**
* Analyze a URL
@ -36,29 +38,15 @@ class Analyzer extends GenericObject
# ----| Properties |--------------------------------------------------------
# --------------------------------------------------------------------------
/**
* The attributes that should be visible in serialization.
*
* @var array
*/
protected $visible = [
'source',
'sourceUrl',
'sourcePath',
'mimeType',
'fileSize',
'lastModified',
'fetcher',
'analyzers',
'fastMode'
];
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'cacheKey',
'cacheTtl',
'cached',
'source',
'sourceUrl',
'sourcePath',
@ -87,6 +75,15 @@ class Analyzer extends GenericObject
*/
protected ?Filesystem $fileSystem = null;
/**
* The value of this attribute will be used to build the cache key. If you
* want to use the "title" attribute of this object to build the cache key
* from, set "title" here.
*
* @var string
*/
public ?string $buildCacheKeyFrom = 'sourcePath';
# ----| Accessors |---------------------------------------------------------
/**
@ -99,6 +96,20 @@ class Analyzer extends GenericObject
return $this->fileSystem;
}
/**
* Return the amount of seconds to keep data in cache for.
*
* @return int
*/
public function getCacheTtlAttribute(): int
{
if (empty($this->attributes['cacheTtl'])) {
$this->attributes['cacheTtl'] = $this->findCacheTtl();
}
return $this->attributes['cacheTtl'];
}
# ----| Mutators |----------------------------------------------------------
/**
@ -193,6 +204,10 @@ class Analyzer extends GenericObject
$this->source = self::SOURCE_FILE;
$this->sourcePath = $file;
if ($this->hasCachedData()) {
return $this->loadFromCache();
}
if (empty($this->fileSystem)) {
$this->setFilesystem(Storage::disk(config('analyzer.storage_disk')));
}
@ -207,6 +222,8 @@ class Analyzer extends GenericObject
$this->fileSystem->delete($file);
}
$this->writeToCache();
return $this;
}
@ -317,4 +334,24 @@ class Analyzer extends GenericObject
return $this;
}
/**
* Return expected cache TTL is seconds from document's expiration date or,
* if not found, from configuration.
*
* @return int
*/
protected function findCacheTtl(): int
{
$defaultCacheTtl = config('analyzer.cache_ttl', 60 * 60 * 6);
$expirationDateString = data_get(
$this->attributes,
'fetcher.expirationDate',
now()->addSeconds($defaultCacheTtl)
);
$expirationDate = Carbon::parse($expirationDateString);
return abs($expirationDate->diffInSeconds(now()));
}
}

2
package/Providers/AnalyzerServiceProvider.php

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Cyca\Analyzer\Providers;
use Cyca\Analyzer\Analyzer;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
@ -22,6 +23,7 @@ class AnalyzerServiceProvider extends ServiceProvider
$this->app->bind('Analyzer', function ($app) {
$client = new Analyzer();
$client->setCacheStore(Cache::store(config('analyzer.cache_store')));
$client->setFilesystem(Storage::disk(config('analyzer.storage_disk')));
return $client;

Loading…
Cancel
Save