Template: Use MapCache, getPage(): Return value, not reference
This commit is contained in:
parent
e970ba1682
commit
8ffa64beea
@ -180,7 +180,9 @@ int main(int argc, char **argv)
|
|||||||
userdao->save(anon.value());
|
userdao->save(anon.value());
|
||||||
User::setAnon(anon.value());
|
User::setAnon(anon.value());
|
||||||
|
|
||||||
Template siteTemplate{config.templateprefix, config.templatepath, config.urls, config.configVarResolver};
|
MapCache<TemplatePage> mapCache;
|
||||||
|
Template siteTemplate{config.templateprefix, config.templatepath, config.urls, config.configVarResolver,
|
||||||
|
mapCache};
|
||||||
UrlProvider urlProvider{config.urls};
|
UrlProvider urlProvider{config.urls};
|
||||||
|
|
||||||
auto cache = createCache(config.configVarResolver);
|
auto cache = createCache(config.configVarResolver);
|
||||||
|
25
template.cpp
25
template.cpp
@ -24,12 +24,25 @@ SOFTWARE.
|
|||||||
#include "htmllink.h"
|
#include "htmllink.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
Template::Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls,
|
Template::Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls,
|
||||||
ConfigVariableResolver &configVarsResolver)
|
ConfigVariableResolver &configVarsResolver, MapCache<TemplatePage> &pageCache)
|
||||||
{
|
{
|
||||||
this->templateprefix = templateprefix;
|
this->templateprefix = templateprefix;
|
||||||
this->templatepath = templatepath;
|
this->templatepath = templatepath;
|
||||||
this->configUrls = &configUrls;
|
this->configUrls = &configUrls;
|
||||||
this->configVarResolver = &configVarsResolver;
|
this->configVarResolver = &configVarsResolver;
|
||||||
|
this->pageCache = &pageCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatePage Template::getPage(const std::string &pagename)
|
||||||
|
{
|
||||||
|
auto result = this->pageCache->find(pagename);
|
||||||
|
if(result)
|
||||||
|
{
|
||||||
|
return *result;
|
||||||
|
}
|
||||||
|
auto page = createPage(pagename);
|
||||||
|
this->pageCache->set(pagename, page);
|
||||||
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Template::getPartPath(std::string_view partname)
|
std::string Template::getPartPath(std::string_view partname)
|
||||||
@ -65,16 +78,6 @@ TemplatePage Template::createPage(std::string name)
|
|||||||
return TemplatePage(replacer.parse(content));
|
return TemplatePage(replacer.parse(content));
|
||||||
}
|
}
|
||||||
|
|
||||||
TemplatePage &Template::getPage(const std::string &pagename)
|
|
||||||
{
|
|
||||||
if(utils::hasKey(pagesMap, pagename))
|
|
||||||
{
|
|
||||||
return pagesMap[pagename];
|
|
||||||
}
|
|
||||||
pagesMap.insert(std::make_pair(pagename, createPage(pagename)));
|
|
||||||
return pagesMap[pagename];
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: this restricts template a bit
|
// TODO: this restricts template a bit
|
||||||
std::string Template::renderSearch(const std::vector<std::string> &results,
|
std::string Template::renderSearch(const std::vector<std::string> &results,
|
||||||
std::function<std::string(std::string)> callback) const
|
std::function<std::string(std::string)> callback) const
|
||||||
|
12
template.h
12
template.h
@ -8,16 +8,17 @@
|
|||||||
#include "response.h"
|
#include "response.h"
|
||||||
#include "searchresult.h"
|
#include "searchresult.h"
|
||||||
#include "revision.h"
|
#include "revision.h"
|
||||||
|
#include "cache/mapcache.h"
|
||||||
class Template
|
class Template
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
ConfigVariableResolver *configVarResolver;
|
ConfigVariableResolver *configVarResolver;
|
||||||
ConfigUrls *configUrls;
|
ConfigUrls *configUrls;
|
||||||
|
MapCache<TemplatePage> *pageCache;
|
||||||
|
|
||||||
std::string templateprefix;
|
std::string templateprefix;
|
||||||
std::string templatepath;
|
std::string templatepath;
|
||||||
|
|
||||||
std::map<std::string, TemplatePage> pagesMap;
|
|
||||||
std::string resolveIncludes(std::string_view content);
|
std::string resolveIncludes(std::string_view content);
|
||||||
|
|
||||||
std::string getPartPath(std::string_view partname);
|
std::string getPartPath(std::string_view partname);
|
||||||
@ -27,12 +28,9 @@ class Template
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls,
|
Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls,
|
||||||
ConfigVariableResolver &configVarsResolver);
|
ConfigVariableResolver &configVarsResolver, MapCache<TemplatePage> &pageCache);
|
||||||
/* TODO: returning this as a reference is by no means a risk free business,
|
|
||||||
because between requests, different vars can be set conditionally,
|
TemplatePage getPage(const std::string &pagename);
|
||||||
thus creating a mess
|
|
||||||
*/
|
|
||||||
TemplatePage &getPage(const std::string &pagename);
|
|
||||||
|
|
||||||
std::string renderSearch(const std::vector<std::string> &results,
|
std::string renderSearch(const std::vector<std::string> &results,
|
||||||
std::function<std::string(std::string)> callback) const;
|
std::function<std::string(std::string)> callback) const;
|
||||||
|
Loading…
Reference in New Issue
Block a user