00001 #include "templatecache.h"
00002 #include <QDateTime>
00003 #include <QStringList>
00004 #include <QSet>
00005
00006 TemplateCache::TemplateCache(QSettings* settings, QObject* parent)
00007 :TemplateLoader(settings,parent)
00008 {
00009 cache.setMaxCost(settings->value("cacheSize","1000000").toInt());
00010 cacheTimeout=settings->value("cacheTime","60000").toInt();
00011 qDebug("TemplateCache: timeout=%i, size=%i",cacheTimeout,cache.maxCost());
00012 }
00013
00014 QString TemplateCache::tryFile(QString localizedName) {
00015 qint64 now=QDateTime::currentMSecsSinceEpoch();
00016
00017 qDebug("TemplateCache: trying cached %s",qPrintable(localizedName));
00018 CacheEntry* entry=cache.object(localizedName);
00019 if (entry && (cacheTimeout==0 || entry->created>now-cacheTimeout)) {
00020 return entry->document;
00021 }
00022
00023 entry=new CacheEntry();
00024 entry->created=now;
00025 entry->document=TemplateLoader::tryFile(localizedName);
00026
00027 cache.insert(localizedName,entry,entry->document.size());
00028 return entry->document;
00029 }
00030