[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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -37,7 +37,6 @@ public:
|
||||
|
||||
private:
|
||||
const std::unique_ptr<util::Thread<Impl>> thread;
|
||||
static std::weak_ptr<SQLiteCache> masterPtr;
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
34
test/storage/cache_shared.cpp
Normal file
34
test/storage/cache_shared.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "storage.hpp"
|
||||
|
||||
#include <mbgl/storage/sqlite_cache.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
|
||||
TEST_F(Storage, CacheShared) {
|
||||
SCOPED_TEST(CacheShared)
|
||||
using namespace mbgl;
|
||||
|
||||
util::RunLoop loop;
|
||||
|
||||
// Check that we're getting two different caches when we request different paths.
|
||||
auto memory = SQLiteCache::getShared();
|
||||
auto file = SQLiteCache::getShared("test/fixtures/database/cache.db");
|
||||
EXPECT_NE(memory.get(), file.get());
|
||||
EXPECT_EQ(memory.get(), SQLiteCache::getShared().get());
|
||||
|
||||
// Store a response into the memory file cache, then delete the last reference.
|
||||
const Resource resource { Resource::Kind::Unknown, "http://example.com" };
|
||||
memory->put(resource, Response());
|
||||
memory.reset();
|
||||
|
||||
// Now check that the original memory file cache has been destructed and that it doesn't contain
|
||||
// the information we put into it.
|
||||
memory = SQLiteCache::getShared();
|
||||
auto req = memory->get(resource, [&](std::unique_ptr<Response> res) {
|
||||
EXPECT_FALSE(res.get());
|
||||
CacheShared.finish();
|
||||
loop.stop();
|
||||
});
|
||||
|
||||
loop.run();
|
||||
}
|
||||
@@ -70,6 +70,7 @@
|
||||
'storage/storage.cpp',
|
||||
'storage/cache_response.cpp',
|
||||
'storage/cache_revalidate.cpp',
|
||||
'storage/cache_shared.cpp',
|
||||
'storage/cache_size.cpp',
|
||||
'storage/database.cpp',
|
||||
'storage/asset_file_source.cpp',
|
||||
|
||||
Reference in New Issue
Block a user