blob: 2d9ff6a803683df3759675b993d0249a0f66cca7 (
plain)
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
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include "../filepath.h"
#include <QCache>
#include <QMutex>
#include <QMutexLocker>
namespace Utils::Internal {
class FilePathInfoCache
{
public:
struct CachedData
{
FilePathInfo filePathInfo;
QDateTime timeout;
};
using RetrievalFunction = CachedData (*)(const FilePath &);
public:
FilePathInfoCache()
: m_cache(50000)
{}
CachedData cached(const FilePath &filePath, const RetrievalFunction &retrievalFunction)
{
QMutexLocker lk(&m_mutex);
CachedData *data = m_cache.object(filePath);
// If the cache entry is too old, don't use it ...
if (data && data->timeout < QDateTime::currentDateTime())
data = nullptr;
// If no data was found, retrieve it and store it in the cache ...
if (!data) {
data = new CachedData;
*data = retrievalFunction(filePath);
if (Q_UNLIKELY(!m_cache.insert(filePath, data))) {
// This path will never happen, but to silence coverity we
// have to check it since insert in theory could delete
// the object if a cost bigger than the cache size is
// specified.
return {};
}
}
// Return a copy of the data, so it cannot be deleted by the cache
return *data;
}
void cache(const FilePath &path, CachedData *data)
{
QMutexLocker lk(&m_mutex);
m_cache.insert(path, data);
}
void cache(const QList<QPair<FilePath, CachedData>> &fileDataList)
{
QMutexLocker lk(&m_mutex);
for (const auto &[path, data] : fileDataList)
m_cache.insert(path, new CachedData(data));
}
void invalidate(const FilePath &path)
{
QMutexLocker lk(&m_mutex);
m_cache.remove(path);
}
private:
QMutex m_mutex;
QCache<FilePath, CachedData> m_cache;
};
} // namespace Utils::Internal
|