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 {};
47 }
48
49 auto query = std::move((*it)->query);
50 m_queries.erase(*it);
51 m_keys.erase(it);
52
53 return query;
54 }
55
56 void insert(const QString &queryStatement, QSqlQuery query)
57 {
58 if (m_queries.size() >= MaxCacheSize) {
59 m_keys.remove(queryStatement);
60 m_queries.pop_back();
61 }
62
63 m_queries.emplace_front(std::move(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 {
76 };
77 std::list<Node> m_queries;
79 QTimer m_cleanupTimer;
80};
81
82QThreadStorage<Cache *> g_queryCache;
83
84Cache *perThreadCache()
85{
86 if (!g_queryCache.hasLocalData()) {
87 g_queryCache.setLocalData(new Cache());
88 }
89
90 return g_queryCache.localData();
91}
92
93} // namespace
94
95std::optional<QSqlQuery> QueryCache::query(const QString &queryStatement)
96{
97 return perThreadCache()->query(queryStatement);
98}
99
100void QueryCache::insert(const QSqlDatabase &db, const QString &queryStatement, QSqlQuery query)
101{
102 if (DbType::type(db) != DbType::Sqlite) {
103 perThreadCache()->insert(queryStatement, std::move(query));
104 }
105}
106
108{
109 if (!g_queryCache.hasLocalData()) {
110 return;
111 }
112
113 g_queryCache.localData()->cleanup();
114}
Type type(const QSqlDatabase &db)
Returns the type of the given database object.
Definition dbtype.cpp:11
void insert(const QSqlDatabase &db, const QString &queryStatement, QSqlQuery query)
Insert query into the cache for queryStatement.
void clear()
Clears all queries from current thread.
std::optional< QSqlQuery > query(const QString &queryStatement)
Return a cached QSqlQuery for given queryStatement.
Helper integration between Akonadi and Qt.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KGuiItem insert()
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 Fri Jul 26 2024 11:52:53 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.