summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-01-14 15:25:56 +0100
committerKonstantin Käfer <mail@kkaefer.com>2016-01-15 10:44:16 +0100
commit957415823a003111f6efecd1a1552a30f999235a (patch)
treeef767f1e3557c8d9a8964087f32beda0b39e0f26 /platform
parent8939a7b7069124adac44ef822bfe6d97adcc14d6 (diff)
downloadqtlocation-mapboxgl-957415823a003111f6efecd1a1552a30f999235a.tar.gz
[core] Support multiple paths in SQLiteCache::getShared()
If you use many different caches, expired weak_ptrs will pile up in the unordered_map, but that is an edge case, and you probably shouldn't do that anyway.
Diffstat (limited to 'platform')
-rw-r--r--platform/default/sqlite_cache.cpp32
1 files changed, 25 insertions, 7 deletions
diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp
index 88bedec699..5816467aa5 100644
--- a/platform/default/sqlite_cache.cpp
+++ b/platform/default/sqlite_cache.cpp
@@ -12,6 +12,9 @@
#include "sqlite3.hpp"
#include <sqlite3.h>
+#include <unordered_map>
+#include <mutex>
+
namespace {
// The cache won't accept entries larger than this arbitrary size
@@ -472,16 +475,31 @@ void SQLiteCache::Impl::refresh(const Resource& resource, Seconds expires) {
}
}
+namespace {
+
+static std::mutex sharedMutex;
+static std::unordered_map<std::string, std::weak_ptr<SQLiteCache>> shared;
+
+} // namespace
+
std::shared_ptr<SQLiteCache> SQLiteCache::getShared(const std::string &path) {
- std::shared_ptr<SQLiteCache> temp = masterPtr.lock();
- if (!temp) {
- temp.reset(new SQLiteCache(path));
- masterPtr = temp;
+ std::lock_guard<std::mutex> lock(sharedMutex);
+
+ std::shared_ptr<SQLiteCache> cache;
+
+ auto it = shared.find(path);
+ if (it != shared.end()) {
+ cache = it->second.lock();
+ if (!cache) {
+ cache = std::make_shared<SQLiteCache>(path);
+ it->second = cache;
+ }
+ } else {
+ cache = std::make_shared<SQLiteCache>(path);
+ shared.emplace(path, cache);
}
- return temp;
+ return cache;
}
-std::weak_ptr<SQLiteCache> SQLiteCache::masterPtr;
-
} // namespace mbgl