mirror of
https://github.com/silverstripe/silverstripe-textextraction
synced 2024-10-22 11:06:00 +02:00
API Only invalidate cache when file is changed
This commit is contained in:
parent
6cf09f26c8
commit
c9d74f83db
@ -16,6 +16,14 @@ interface FileTextCache {
|
||||
* @param File $file
|
||||
*/
|
||||
public function load(File $file);
|
||||
|
||||
/**
|
||||
* Invalidate the cache for a given file.
|
||||
* Invoked in onBeforeWrite on the file
|
||||
*
|
||||
* @param File $file
|
||||
*/
|
||||
public function invalidate(File $file);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,6 +42,13 @@ class FileTextCache_Database implements FileTextCache {
|
||||
$file->write();
|
||||
}
|
||||
|
||||
public function invalidate(File $file) {
|
||||
// To prevent writing to the cache from invalidating it
|
||||
if(!$file->isChanged('FileContentCache')) {
|
||||
$file->FileContentCache = '';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,12 +57,13 @@ class FileTextCache_Database implements FileTextCache {
|
||||
class FileTextCache_SSCache implements FileTextCache, Flushable {
|
||||
|
||||
/**
|
||||
* Default cache to 1 hour
|
||||
* Lifetime of cache in seconds
|
||||
* Null is indefinite
|
||||
*
|
||||
* @var int
|
||||
* @var int|null
|
||||
* @config
|
||||
*/
|
||||
private static $lifetime = 3600;
|
||||
private static $lifetime = null;
|
||||
|
||||
/**
|
||||
* @return SS_Cache
|
||||
@ -80,4 +96,10 @@ class FileTextCache_SSCache implements FileTextCache, Flushable {
|
||||
$cache->clean();
|
||||
}
|
||||
|
||||
public function invalidate(File $file) {
|
||||
$key = $this->getKey($file);
|
||||
$cache = self::get_cache();
|
||||
return $cache->remove($key);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -83,4 +83,9 @@ class FileTextExtractable extends DataExtension {
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
public function onBeforeWrite() {
|
||||
// Clear cache before changing file
|
||||
$this->getTextCache()->invalidate($this->owner);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user