Commit 726b957f authored by Teppo Koivula's avatar Teppo Koivula

Add support for WireCache + make cache method and TTL configurable

parent a65f7341
{
"title": "Sitemap",
"version": "0.4.1",
"version": "0.5.0",
"author": "Mike Rockett",
"summary": "Renders a sitemap for your ProcessWire powered site. Supports multi-language, multi-site, and image sub-elements. Adapted from MarkupSitemapXML.",
"href": "http://github.com/rockettpw/markup-sitemap",
......
......@@ -219,8 +219,9 @@ class MarkupSitemap extends WireData implements Module
* consideration to multi-site setups) and passing it to the
* first/parent recursive render-method (addPages).
*
* MarkupCache is used to cache the entire sitemap, and the cache
* is destroyed when settings are saved and, if set up, a page is saved.
* Depending on config settings entire sitemap is cached using MarkupCache or
* WireCache, and the cache is destroyed when settings are saved and, if set
* up, a page is saved.
*
* @param HookEvent $event
*/
......@@ -239,21 +240,10 @@ class MarkupSitemap extends WireData implements Module
// Make sure that the root page exists.
if (!$this->pages->get($rootPage) instanceof NullPage) {
// Check for cached sitemap or regenerate if it doesn't exist
$markupCache = $this->modules->MarkupCache;
if ((!$output = $markupCache->get('MarkupSitemap', 3600)) || $this->config->debug) {
$output = $this->buildNewSitemap($rootPage);
$markupCache->save($output);
header('X-Cached-Sitemap: no');
} else {
header('X-Cached-Sitemap: yes');
}
// Get cached sitemap
$event->return = $this->getCached($rootPage);
header('Content-Type: application/xml', true, 200);
$event->return = $output;
// Prevent further hooks. This stops
// SystemNotifications from displaying a 404 event
// when /sitemap.xml is requested. Additionally,
......@@ -263,6 +253,45 @@ class MarkupSitemap extends WireData implements Module
}
}
/**
* Get cached sitemap markup
*
* @param string $rootPage
* @return string
*/
protected function getCached($rootPage)
{
// Bail out early if debug mode is enabled
if ($this->config->debug) {
header('X-Cached-Sitemap: no');
return $this->buildNewSitemap($rootPage);
}
// Cache settings
$cacheTTL = $this->cache_ttl ?: 3600;
$cacheKey = 'MarkupSitemap';
$cacheMethod = $this->cache_method ?: 'MarkupCache';
// Attempt to fetch sitemap from cache
$cache = $cacheMethod == 'WireCache' ? $this->cache : $this->modules->MarkupCache;
$output = $cache->get($cacheKey, $cacheTTL);
// If output is empty, generate and cache new sitemap
if (empty($output)) {
header('X-Cached-Sitemap: no');
$output = $this->buildNewSitemap($rootPage);
if ($cacheMethod == 'WireCache') {
$cache->save($cacheKey, $output, $cacheTTL);
} else {
$cache->save($output);
}
return $output;
}
header('X-Cached-Sitemap: yes');
return $output;
}
/**
* Add sitemap fields to the Settings tab.
* Responds to ProcessPageEdit::buildFormSettings hook
......
......@@ -28,6 +28,8 @@ class MarkupSitemapConfig extends ModuleConfig
'sitemap_stylesheet' => true,
'sitemap_exclude_templates' => [],
'sitemap_include_hidden' => false,
'cache_method' => 'MarkupCache',
'cache_ttl' => 3600,
];
}
......@@ -150,6 +152,39 @@ class MarkupSitemapConfig extends ModuleConfig
// Add the stylesheet fieldset to the inputfields
$inputfields->add($stylesheetFieldset);
// Create the cache fieldset
$cacheFieldset = $this->buildInputField('Fieldset', [
'label' => $this->_('Cache'),
'collapsed' => Inputfield::collapsedBlank,
'icon' => 'database',
]);
// Add the cache method select field
$cacheFieldset->add($this->buildInputField('Select', [
'name+id' => 'cache_method',
'label' => $this->_('Choose the cache method'),
'description' => $this->_('MarkupCache (default) stores cached content as files on disk. WireCache stores cached content into database.'),
'icon' => 'floppy-o',
'options' => [
'MarkupCache' => 'MarkupCache',
'WireCache' => 'WireCache',
],
]));
// Add the cache expire time (TTL) input
$cacheFieldset->add($this->buildInputField('Integer', [
'name+id' => 'cache_ttl',
'label' => $this->_('Cache expire time (TTL)'),
'description' => $this->_('Type in the expire time for the cache as a number of seconds.'),
'notes' => $this->_('The default value is **3600**.'),
'min' => 1,
'icon' => 'clock-o',
'collapsed' => Inputfield::collapsedBlank,
]));
// Add the stylesheet fieldset to the inputfields
$inputfields->add($cacheFieldset);
// Add the support-development markup field
$supportText = $this->wire('sanitizer')->entitiesMarkdown($this->_('Sitemap is proudly [open-source](http://opensource.com/resources/what-open-source) and is [free to use](https://en.wikipedia.org/wiki/Free_software) for personal and commercial projects. Please consider [making a small donation](https://rockett.pw/donate) in support of the development of MarkupSitemap and other modules.'), ['fullMarkdown' => true]);
$inputfields->add($this->buildInputField('Markup', [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment