QtWebApp
templatecache.cpp
1 #include "templatecache.h"
2 #include <QDateTime>
3 #include <QStringList>
4 #include <QSet>
5 
6 using namespace stefanfrings;
7 
8 TemplateCache::TemplateCache(const QSettings* settings, QObject* parent)
9  :TemplateLoader(settings,parent)
10 {
11  cache.setMaxCost(settings->value("cacheSize","1000000").toInt());
12  cacheTimeout=settings->value("cacheTime","60000").toInt();
13  long int cacheMaxCost=(long int)cache.maxCost();
14  qDebug("TemplateCache: timeout=%i, size=%li",cacheTimeout,cacheMaxCost);
15 }
16 
17 QString TemplateCache::tryFile(const QString localizedName)
18 {
19  qint64 now=QDateTime::currentMSecsSinceEpoch();
20  mutex.lock();
21  // search in cache
22  qDebug("TemplateCache: trying cached %s",qPrintable(localizedName));
23  CacheEntry* entry=cache.object(localizedName);
24  if (entry && (cacheTimeout==0 || entry->created>now-cacheTimeout))
25  {
26  mutex.unlock();
27  return entry->document;
28  }
29  // search on filesystem
30  entry=new CacheEntry();
31  entry->created=now;
32  entry->document=TemplateLoader::tryFile(localizedName);
33  // Store in cache even when the file did not exist, to remember that there is no such file
34  cache.insert(localizedName,entry,entry->document.size());
35  mutex.unlock();
36  return entry->document;
37 }
38 
virtual QString tryFile(const QString localizedName)
Try to get a file from cache or filesystem.
TemplateCache(const QSettings *settings, QObject *parent=nullptr)
Constructor.
Loads localized versions of template files.
virtual QString tryFile(const QString localizedName)
Try to get a file from cache or filesystem.