|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
interface FileTextCache |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* Save extracted content for a given File entity |
|
7
|
|
|
* |
|
8
|
|
|
* @param File $file |
|
9
|
|
|
* @param string $content |
|
10
|
|
|
*/ |
|
11
|
|
|
public function save(File $file, $content); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Return any cached extracted content for a given file entity |
|
15
|
|
|
* |
|
16
|
|
|
* @param File $file |
|
17
|
|
|
*/ |
|
18
|
|
|
public function load(File $file); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Invalidate the cache for a given file. |
|
22
|
|
|
* Invoked in onBeforeWrite on the file |
|
23
|
|
|
* |
|
24
|
|
|
* @param File $file |
|
25
|
|
|
*/ |
|
26
|
|
|
public function invalidate(File $file); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Caches the extracted content on the record for the file. |
|
31
|
|
|
* Limits the stored file content by default to avoid hitting query size limits. |
|
32
|
|
|
*/ |
|
33
|
|
|
class FileTextCache_Database implements FileTextCache |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
public function load(File $file) |
|
36
|
|
|
{ |
|
37
|
|
|
return $file->FileContentCache; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function save(File $file, $content) |
|
41
|
|
|
{ |
|
42
|
|
|
$maxLength = Config::inst()->get('FileTextCache_Database', 'max_content_length'); |
|
43
|
|
|
$file->FileContentCache = ($maxLength) ? substr($content, 0, $maxLength) : $content; |
|
|
|
|
|
|
44
|
|
|
$file->write(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function invalidate(File $file) |
|
48
|
|
|
{ |
|
49
|
|
|
// To prevent writing to the cache from invalidating it |
|
50
|
|
|
if (!$file->isChanged('FileContentCache')) { |
|
51
|
|
|
$file->FileContentCache = ''; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Uses SS_Cache with a lifetime to cache extracted content |
|
58
|
|
|
*/ |
|
59
|
|
|
class FileTextCache_SSCache implements FileTextCache, Flushable |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
/** |
|
62
|
|
|
* Lifetime of cache in seconds |
|
63
|
|
|
* Null is indefinite |
|
64
|
|
|
* |
|
65
|
|
|
* @var int|null |
|
66
|
|
|
* @config |
|
67
|
|
|
*/ |
|
68
|
|
|
private static $lifetime = null; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return SS_Cache |
|
72
|
|
|
*/ |
|
73
|
|
|
protected static function get_cache() |
|
74
|
|
|
{ |
|
75
|
|
|
$lifetime = Config::inst()->get(__CLASS__, 'lifetime'); |
|
76
|
|
|
$cache = SS_Cache::factory(__CLASS__); |
|
77
|
|
|
$cache->setLifetime($lifetime); |
|
78
|
|
|
return $cache; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function getKey(File $file) |
|
82
|
|
|
{ |
|
83
|
|
|
return md5($file->getFullPath()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function load(File $file) |
|
87
|
|
|
{ |
|
88
|
|
|
$key = $this->getKey($file); |
|
89
|
|
|
$cache = self::get_cache(); |
|
90
|
|
|
return $cache->load($key); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function save(File $file, $content) |
|
94
|
|
|
{ |
|
95
|
|
|
$key = $this->getKey($file); |
|
96
|
|
|
$cache = self::get_cache(); |
|
97
|
|
|
return $cache->save($content, $key); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public static function flush() |
|
101
|
|
|
{ |
|
102
|
|
|
$cache = self::get_cache(); |
|
103
|
|
|
$cache->clean(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function invalidate(File $file) |
|
107
|
|
|
{ |
|
108
|
|
|
$key = $this->getKey($file); |
|
109
|
|
|
$cache = self::get_cache(); |
|
110
|
|
|
return $cache->remove($key); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.