Merge pull request #11 from tractorcow/pulls/invalidate

API Only invalidate cache when file is changed
This commit is contained in:
Ingo Schommer 2015-05-12 16:08:45 +12:00
commit 15f9647bca
2 changed files with 30 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -83,4 +83,9 @@ class FileTextExtractable extends DataExtension {
return $text;
}
public function onBeforeWrite() {
// Clear cache before changing file
$this->getTextCache()->invalidate($this->owner);
}
}