blob: 33e69408bd48028dba8cc73e5dc32c20ce4e2467 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include "cachehandler.hpp"
#include "kristall.hpp"
#include "ioutil.hpp"
#include <QDebug>
void CacheHandler::push(const QUrl &u, const QByteArray &body, const MimeType &mime)
{
// Skip if this item is above the cached item size threshold
int bodysize = body.size();
if (bodysize > (kristall::options.cache_threshold * 1024))
{
qDebug() << "cache: item exceeds threshold (" << IoUtil::size_human(body.size()) << ")";
return;
}
// Pop cached items until we are below the cache limit
// TODO
//if ((bodysize + this->size()) > (kristall::options.cache_limit * 1024 * 1024))
//while ((bodysize + this->size()) > (kristall::options.cache_limit * 1024 * 1024))
//{
// qDebug() << "cache: adding item will exceed cache limit";
//}
QUrl url = IoUtil::uniformUrl(u);
QString urlstr = url.toString(QUrl::FullyEncoded);;
if (this->page_cache.find(urlstr) != this->page_cache.end())
{
qDebug() << "cache: updating page";
auto pg = this->page_cache[urlstr];
pg->body = body;
pg->mime = mime;
pg->time_cached = QDateTime::currentDateTime();
return;
}
this->page_cache[urlstr] = std::make_shared<CachedPage>(
u, body, mime, QDateTime::currentDateTime());
qDebug() << "cache: pushing url " << url;
return;
}
std::shared_ptr<CachedPage> CacheHandler::find(const QString &url)
{
if (this->page_cache.find(url) != this->page_cache.end())
{
return this->page_cache[url];
}
return nullptr;
}
std::shared_ptr<CachedPage> CacheHandler::find(const QUrl &url)
{
return this->find(IoUtil::uniformUrlString(url));
}
bool CacheHandler::contains(const QString &url)
{
return this->page_cache.find(url) != this->page_cache.end();
}
bool CacheHandler::contains(const QUrl &url)
{
return this->contains(IoUtil::uniformUrlString(url));
}
int CacheHandler::size()
{
int s = 0;
for (auto& i : this->page_cache)
s += i.second->body.size();
return s;
}
// Clears expired pages out of cache
void CacheHandler::clean()
{
// Find list of keys to delete
std::vector<QString> vec;
for (auto&& i : this->page_cache)
{
if (QDateTime::currentDateTime() > i.second->time_cached
.addSecs(kristall::options.cache_life * 60))
{
vec.emplace_back(std::move(i.first));
}
}
// Delete them
int count = 0;
for (auto&& key : vec)
{
this->page_cache.erase(key);
++count;
}
if (count) qDebug() << "cache: cleaned " << count << " expired pages out of cache";
}
CacheMap const& CacheHandler::getPages() const
{
return this->page_cache;
}
|