Merge pull request #10 from chillu/pulls/truncate-db-cache

Truncate FileContentCache by default to avoid SQL query errors
This commit is contained in:
Damian Mooyman 2015-05-12 15:25:59 +12:00
commit 6c7ffa2c6f
4 changed files with 28 additions and 2 deletions

View File

@ -64,6 +64,10 @@ File:
``` ```
By default any extracted content will be cached against the database row. By default any extracted content will be cached against the database row.
In order to stay within common size constraints for SQL queries required in this operation,
the cache sets a maximum character length after which content gets truncated (default: 500000).
You can configure this value through `FileTextCache_Database.max_content_length` in your yaml configuration.
Alternatively, extracted content can be cached using SS_Cache to prevent excessive database growth. Alternatively, extracted content can be cached using SS_Cache to prevent excessive database growth.
In order to swap out the cache backend you can use the following yaml configuration. In order to swap out the cache backend you can use the following yaml configuration.

View File

@ -6,3 +6,6 @@ Injector:
SolrCellTextExtractor: SolrCellTextExtractor:
# base_url: 'http://localhost:8983/solr/update/extract' # base_url: 'http://localhost:8983/solr/update/extract'
FileTextCache_Database:
max_content_length: 500000

View File

@ -19,7 +19,8 @@ interface FileTextCache {
} }
/** /**
* Caches the extracted content on the record for the file * Caches the extracted content on the record for the file.
* Limits the stored file content by default to avoid hitting query size limits.
*/ */
class FileTextCache_Database implements FileTextCache { class FileTextCache_Database implements FileTextCache {
@ -28,7 +29,8 @@ class FileTextCache_Database implements FileTextCache {
} }
public function save(File $file, $content) { public function save(File $file, $content) {
$file->FileContentCache = $content; $maxLength = Config::inst()->get('FileTextCache_Database', 'max_content_length');
$file->FileContentCache = ($maxLength) ? substr($content, 0, $maxLength) : $content;
$file->write(); $file->write();
} }

View File

@ -0,0 +1,17 @@
<?php
class FileTextCacheDatabaseTest extends SapphireTest {
public function testTruncatesByMaxLength() {
Config::nest();
Config::inst()->update('FileTextCache_Database', 'max_content_length', 5);
$cache = new FileTextCache_Database();
$file = $this->getMock('File', array('write'));
$content = '0123456789';
$cache->save($file, $content);
$this->assertEquals($cache->load($file), '01234');
Config::unnest();
}
}