aboutsummaryrefslogtreecommitdiff
path: root/src/cachehandler.cpp
blob: bd4c7e58f043979ce9828b89b074deb764f19e10 (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
#include "cachehandler.hpp"
#include "kristall.hpp"
#include "ioutil.hpp"

#include <QDebug>

void CacheHandler::push(const QUrl &u, const QByteArray &body, const MimeType &mime)
{
    QUrl url = IoUtil::uniformUrl(u);
    QString urlstr = url.toString(QUrl::FullyEncoded);;

    if (this->page_cache.find(urlstr) != this->page_cache.end())
    {
        qDebug() << "Updating cached page";
        auto pg = this->page_cache[urlstr];
        pg->body = body;
        pg->mime = mime;
        return;
    }

    this->page_cache[urlstr] = std::make_shared<CachedPage>(url, body, mime);

    qDebug() << "Pushed page to cache: " << url;

    return;
}

std::shared_ptr<CachedPage> CacheHandler::find(const QString &url)
{
    if (this->page_cache.find(url) != this->page_cache.end())
    {
        return 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) const
{
    return this->page_cache.find(url) != this->page_cache.end();
}

bool CacheHandler::contains(const QUrl &url) const
{
    return this->contains(IoUtil::uniformUrlString(url));
}

CacheMap const& CacheHandler::getPages() const
{
    return this->page_cache;
}