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