Akonadi

querycache.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "querycache.h"
8#include "datastore.h"
9#include "dbtype.h"
10
11#include <QHash>
12#include <QSqlDriver>
13#include <QSqlQuery>
14#include <QThreadStorage>
15#include <QTimer>
16
17#include <chrono>
18#include <list>
19
20using namespace std::chrono_literals;
21using namespace Akonadi;
22using namespace Akonadi::Server;
23
24namespace
25{
26// After these seconds without activity the cache is cleaned
27constexpr auto CleanupTimeout = 60s;
28constexpr int MaxCacheSize = 50;
29
30/// LRU cache with limited size and auto-cleanup after given
31/// period of time
32class Cache
33{
34public:
35 Cache()
36 {
37 QObject::connect(&m_cleanupTimer, &QTimer::timeout, &m_cleanupTimer, std::bind(&Cache::cleanup, this));
38 m_cleanupTimer.setSingleShot(true);
39 }
40
41 std::optional<QSqlQuery> query(const QString &queryStatement)
42 {
43 m_cleanupTimer.start(CleanupTimeout);
44 auto it = m_keys.find(queryStatement);
45 if (it == m_keys.end()) {
46 return std::nullopt;
47 }
48
49 auto node = **it;
50 m_queries.erase(*it);
51 m_queries.push_front(node);
52 *it = m_queries.begin();
53 return node.query;
54 }
55
56 void insert(const QString &queryStatement, const QSqlQuery &query)
57 {
58 if (m_queries.size() >= MaxCacheSize) {
59 m_keys.remove(m_queries.back().queryStatement);
60 m_queries.pop_back();
61 }
62
63 m_queries.emplace_front(Node{queryStatement, query});
64 m_keys.insert(queryStatement, m_queries.begin());
65 }
66
67 void cleanup()
68 {
69 m_keys.clear();
70 m_queries.clear();
71 }
72
73public: // public, this is just a helper class
74 struct Node {
75 QString queryStatement;
77 };
78 std::list<Node> m_queries;
80 QTimer m_cleanupTimer;
81};
82
83QThreadStorage<Cache *> g_queryCache;
84
85Cache *perThreadCache()
86{
87 if (!g_queryCache.hasLocalData()) {
88 g_queryCache.setLocalData(new Cache());
89 }
90
91 return g_queryCache.localData();
92}
93
94} // namespace
95
96std::optional<QSqlQuery> QueryCache::query(const QString &queryStatement)
97{
98 return perThreadCache()->query(queryStatement);
99}
100
101void QueryCache::insert(const QSqlDatabase &db, const QString &queryStatement, const QSqlQuery &query)
102{
103 if (DbType::type(db) != DbType::Sqlite) {
104 perThreadCache()->insert(queryStatement, query);
105 }
106}
107
109{
110 if (!g_queryCache.hasLocalData()) {
111 return;
112 }
113
114 g_queryCache.localData()->cleanup();
115}
Type type(const QSqlDatabase &db)
Returns the type of the given database object.
Definition dbtype.cpp:11
void clear()
Clears all queries from current thread.
void insert(const QSqlDatabase &db, const QString &queryStatement, const QSqlQuery &query)
Insert query into the cache for queryStatement.
std::optional< QSqlQuery > query(const QString &queryStatement)
Returns the cached (and prepared) query for queryStatement.
Helper integration between Akonadi and Qt.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KGuiItem insert()
iterator erase(const_iterator begin, const_iterator end)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool hasLocalData() const const
void setLocalData(T data)
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.