From 4b587603b3c80e1dbe42390f59d942e52cfb68a2 Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Wed, 16 Sep 2015 12:45:46 +1000 Subject: [PATCH] Create SharedSQLiteCache --- android/cpp/native_map_view.cpp | 4 ++-- include/mbgl/android/native_map_view.hpp | 2 +- include/mbgl/storage/sqlite_cache.hpp | 10 ++++++++++ platform/default/sqlite_cache.cpp | 12 ++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/android/cpp/native_map_view.cpp b/android/cpp/native_map_view.cpp index f38513b32..9ffb083ec 100644 --- a/android/cpp/native_map_view.cpp +++ b/android/cpp/native_map_view.cpp @@ -58,8 +58,8 @@ NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int a pixelRatio(pixelRatio_), availableProcessors(availableProcessors_), totalMemory(totalMemory_), - fileCache(mbgl::android::cachePath + "/mbgl-cache.db"), - fileSource(&fileCache), + fileCache(mbgl::SharedSQLiteCache::get(mbgl::android::cachePath + "/mbgl-cache.db")), + fileSource(fileCache.get()), map(*this, fileSource, MapMode::Continuous) { mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::NativeMapView"); diff --git a/include/mbgl/android/native_map_view.hpp b/include/mbgl/android/native_map_view.hpp index d5606ae37..4033c628b 100644 --- a/include/mbgl/android/native_map_view.hpp +++ b/include/mbgl/android/native_map_view.hpp @@ -90,7 +90,7 @@ private: size_t totalMemory = 0; // Ensure these are initialised last - mbgl::SQLiteCache fileCache; + std::shared_ptr fileCache; mbgl::DefaultFileSource fileSource; mbgl::Map map; diff --git a/include/mbgl/storage/sqlite_cache.hpp b/include/mbgl/storage/sqlite_cache.hpp index 65328652f..3054b6c73 100644 --- a/include/mbgl/storage/sqlite_cache.hpp +++ b/include/mbgl/storage/sqlite_cache.hpp @@ -26,6 +26,16 @@ private: const std::unique_ptr> thread; }; +class SharedSQLiteCache : util::noncopyable { +public: + static std::shared_ptr get(const std::string &path = ":memory:"); + +private: + SharedSQLiteCache() {} + + static std::weak_ptr masterPtr; +}; + } #endif diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp index 9d5d0541e..8863c151e 100644 --- a/platform/default/sqlite_cache.cpp +++ b/platform/default/sqlite_cache.cpp @@ -264,4 +264,16 @@ void SQLiteCache::Impl::refresh(const Resource& resource, int64_t expires) { } } +std::shared_ptr SharedSQLiteCache::get(const std::string &path) { + std::shared_ptr temp = masterPtr.lock(); + if (!temp) { + temp.reset(new SQLiteCache(path)); + masterPtr = temp; + } + + return temp; +} + +std::weak_ptr SharedSQLiteCache::masterPtr; + }