1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\SimpleTemplateCaching\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
6
|
|
|
use SilverStripe\Forms\CheckboxField; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\GridField\GridField; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer; |
10
|
|
|
use SilverStripe\Forms\NumericField; |
11
|
|
|
use SilverStripe\Forms\ReadonlyField; |
12
|
|
|
use SilverStripe\ORM\DataExtension; |
13
|
|
|
use SilverStripe\ORM\DB; |
14
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
15
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
16
|
|
|
use Sunnysideup\SimpleTemplateCaching\Model\ObjectsUpdated; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class \Sunnysideup\SimpleTemplateCaching\Extensions\SimpleTemplateCachingSiteConfigExtension. |
20
|
|
|
* |
21
|
|
|
* @property SiteConfig|SimpleTemplateCachingSiteConfigExtension $owner |
22
|
|
|
* @property bool $HasCaching |
23
|
|
|
* @property int $PublicCacheDurationInSeconds |
24
|
|
|
* @property bool $RecordCacheUpdates |
25
|
|
|
* @property string $CacheKeyLastEdited |
26
|
|
|
* @property string $ClassNameLastEdited |
27
|
|
|
*/ |
28
|
|
|
class SimpleTemplateCachingSiteConfigExtension extends DataExtension |
29
|
|
|
{ |
30
|
|
|
private const MAX_OBJECTS_UPDATED = 1000; |
31
|
|
|
|
32
|
|
|
private static $db = [ |
33
|
|
|
'HasCaching' => 'Boolean(1)', |
34
|
|
|
'PublicCacheDurationInSeconds' => 'Int', |
35
|
|
|
'RecordCacheUpdates' => 'Boolean(0)', |
36
|
|
|
'CacheKeyLastEdited' => 'DBDatetime', |
37
|
|
|
'ClassNameLastEdited' => 'Varchar(200)', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
public function updateCMSFields(FieldList $fields) |
41
|
|
|
{ |
42
|
|
|
$name = ''; |
43
|
|
|
if (class_exists($this->getOwner()->ClassNameLastEdited)) { |
44
|
|
|
$name = Injector::inst()->get($this->getOwner()->ClassNameLastEdited)->i18n_singular_name(); |
45
|
|
|
} |
46
|
|
|
$fields->addFieldsToTab( |
47
|
|
|
'Root.Caching', |
48
|
|
|
[ |
49
|
|
|
CheckboxField::create('HasCaching', 'Use caching'), |
50
|
|
|
NumericField::create('PublicCacheDurationInSeconds', 'Cache time for ALL pages') |
51
|
|
|
->setDescription( |
52
|
|
|
'This will apply caching to ALL pages on the site.' |
53
|
|
|
), |
54
|
|
|
CheckboxField::create('RecordCacheUpdates', 'Record every change?') |
55
|
|
|
->setDescription('To work out when the cache is being updated, you can track every change. This will slow down all your edits, so it is recommend only to turn this on temporarily - for tuning purposes.'), |
56
|
|
|
ReadonlyField::create('CacheKeyLastEdited', 'Content Last Edited') |
57
|
|
|
->setRightTitle('The frontend template cache will be invalidated every time this value changes. It changes every time anything is changed in the database.'), |
58
|
|
|
ReadonlyField::create('ClassNameLastEdited', 'Last class updated') |
59
|
|
|
->setRightTitle('Last object updated. The name of this object is: ' . $name), |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
if ($this->getOwner()->RecordCacheUpdates) { |
63
|
|
|
$fields->addFieldsToTab( |
64
|
|
|
'Root.Caching', |
65
|
|
|
[ |
66
|
|
|
GridField::create( |
67
|
|
|
'ObjectsUpdated', |
68
|
|
|
'Last ' . self::MAX_OBJECTS_UPDATED . ' objects updated', |
69
|
|
|
ObjectsUpdated::get()->limit(self::MAX_OBJECTS_UPDATED), |
70
|
|
|
GridFieldConfig_RecordViewer::create() |
71
|
|
|
), |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function site_cache_key(): string |
78
|
|
|
{ |
79
|
|
|
$obj = SiteConfig::current_site_config(); |
80
|
|
|
if ($obj->HasCaching) { |
81
|
|
|
return (string) 'ts_' . strtotime($obj->CacheKeyLastEdited); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return (string) 'ts_' . time(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function update_cache_key(?string $className = '') |
88
|
|
|
{ |
89
|
|
|
// important - avoid endless loop! |
90
|
|
|
if(SiteConfig::get()->exists()) { |
91
|
|
|
$howOldIsIt = DB::query('SELECT Created FROM SiteConfig LIMIT 1')->value(); |
92
|
|
|
if ($howOldIsIt && strtotime((string) $howOldIsIt) > strtotime('-5 minutes')) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
try { |
99
|
|
|
$obj = SiteConfig::current_site_config(); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
$obj = null; |
102
|
|
|
} |
103
|
|
|
if ($obj && $obj->HasCaching) { |
104
|
|
|
DB::query(' |
105
|
|
|
UPDATE "SiteConfig" |
106
|
|
|
SET |
107
|
|
|
"CacheKeyLastEdited" = \'' . DBDatetime::now()->Rfc2822() . '\', |
108
|
|
|
"ClassNameLastEdited" = \'' . addslashes((string) $className) . '\' |
109
|
|
|
WHERE ID = ' . $obj->ID . ' |
110
|
|
|
LIMIT 1 |
111
|
|
|
;'); |
112
|
|
|
} |
113
|
|
|
if ($obj && $obj->RecordCacheUpdates) { |
114
|
|
|
$recordId = Injector::inst() |
115
|
|
|
->create(ObjectsUpdated::class, ['ClassNameLastEdited' => $className]) |
116
|
|
|
->write() |
117
|
|
|
; |
118
|
|
|
DB::query('DELETE FROM ObjectsUpdated WHERE ID < ' . (int) ($recordId - self::MAX_OBJECTS_UPDATED)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function requireDefaultRecords() |
123
|
|
|
{ |
124
|
|
|
if((int) SiteConfig::get()->count() > 100) { |
125
|
|
|
$currentSiteConfig = SiteConfig::current_site_config(); |
126
|
|
|
if($currentSiteConfig) { |
|
|
|
|
127
|
|
|
DB::alteration_message('Deleting all SiteConfig records except for the current one.', 'deleted'); |
128
|
|
|
DB::query('DELETE FROM "SiteConfig" WHERE ID <> ' . $currentSiteConfig->ID); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|