[core] Eliminate FileCache interface

There is only one implementation and we're unlikely to add more.
This commit is contained in:
John Firebaugh
2016-01-11 17:20:07 -08:00
parent f9574e350c
commit 7fb5e973ed
6 changed files with 28 additions and 46 deletions

View File

@@ -1,28 +0,0 @@
#ifndef MBGL_STORAGE_FILE_CACHE
#define MBGL_STORAGE_FILE_CACHE
#include <mbgl/util/noncopyable.hpp>
#include <functional>
#include <memory>
namespace mbgl {
struct Resource;
class Response;
class WorkRequest;
class FileCache : private util::noncopyable {
public:
virtual ~FileCache() = default;
enum class Hint : bool { Full, Refresh };
using Callback = std::function<void(std::unique_ptr<Response>)>;
virtual std::unique_ptr<WorkRequest> get(const Resource &resource, Callback callback) = 0;
virtual void put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint) = 0;
};
} // namespace mbgl
#endif

View File

@@ -2,17 +2,18 @@
#define MBGL_STORAGE_ONLINE_FILE_SOURCE
#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/file_cache.hpp>
namespace mbgl {
class SQLiteCache;
namespace util {
template <typename T> class Thread;
} // namespace util
class OnlineFileSource : public FileSource {
public:
OnlineFileSource(FileCache*);
OnlineFileSource(SQLiteCache*);
~OnlineFileSource() override;
void setAccessToken(const std::string& t) { accessToken = t; }

View File

@@ -1,6 +1,7 @@
#include <mbgl/storage/online_file_source.hpp>
#include <mbgl/storage/http_context_base.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/platform/log.hpp>
@@ -68,7 +69,7 @@ class OnlineFileSource::Impl {
public:
using Callback = std::function<void (Response)>;
Impl(FileCache*);
Impl(SQLiteCache*);
~Impl();
void networkIsReachableAgain();
@@ -80,12 +81,12 @@ private:
friend OnlineFileRequestImpl;
std::unordered_map<FileRequest*, std::unique_ptr<OnlineFileRequestImpl>> pending;
FileCache* const cache;
SQLiteCache* const cache;
const std::unique_ptr<HTTPContextBase> httpContext;
util::AsyncTask reachability;
};
OnlineFileSource::OnlineFileSource(FileCache* cache)
OnlineFileSource::OnlineFileSource(SQLiteCache* cache)
: thread(std::make_unique<util::Thread<Impl>>(
util::ThreadContext{ "OnlineFileSource", util::ThreadType::Unknown, util::ThreadPriority::Low },
cache)) {
@@ -134,7 +135,7 @@ void OnlineFileSource::cancel(FileRequest* req) {
// ----- Impl -----
OnlineFileSource::Impl::Impl(FileCache* cache_)
OnlineFileSource::Impl::Impl(SQLiteCache* cache_)
: cache(cache_),
httpContext(HTTPContextBase::createContext()),
reachability(std::bind(&Impl::networkIsReachableAgain, this)) {
@@ -251,9 +252,9 @@ void OnlineFileRequestImpl::scheduleRealRequest(OnlineFileSource::Impl& impl, bo
(!response_->error || (response_->error->reason == Response::Error::Reason::NotFound))) {
// Store response in database. Make sure we only refresh the expires column if the data
// didn't change.
FileCache::Hint hint = FileCache::Hint::Full;
SQLiteCache::Hint hint = SQLiteCache::Hint::Full;
if (response && response_->data == response->data) {
hint = FileCache::Hint::Refresh;
hint = SQLiteCache::Hint::Refresh;
}
impl.cache->put(resource, response_, hint);
}

View File

@@ -2,7 +2,6 @@
#define MBGL_STORAGE_REQUEST_BASE
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/storage/file_cache.hpp>
#include <memory>
#include <functional>

View File

@@ -1,27 +1,36 @@
#ifndef MBGL_STORAGE_DEFAULT_SQLITE_CACHE
#define MBGL_STORAGE_DEFAULT_SQLITE_CACHE
#include <mbgl/storage/file_cache.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/chrono.hpp>
#include <functional>
#include <memory>
#include <string>
namespace mbgl {
struct Resource;
class Response;
class WorkRequest;
namespace util {
template <typename T> class Thread;
} // namespace util
class SQLiteCache : public FileCache {
class SQLiteCache : private util::noncopyable {
public:
SQLiteCache(const std::string &path = ":memory:");
~SQLiteCache() override;
~SQLiteCache();
void setMaximumCacheSize(uint64_t size);
void setMaximumCacheEntrySize(uint64_t size);
// FileCache API
std::unique_ptr<WorkRequest> get(const Resource &resource, Callback callback) override;
void put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint) override;
enum class Hint : bool { Full, Refresh };
using Callback = std::function<void(std::unique_ptr<Response>)>;
std::unique_ptr<WorkRequest> get(const Resource&, Callback);
void put(const Resource&, std::shared_ptr<const Response> response, Hint hint);
class Impl;

View File

@@ -66,7 +66,7 @@ TEST_F(Storage, CacheNotFound) {
// Insert existing data into the cache that will be marked as stale.
auto response = std::make_shared<Response>();
response->data = std::make_shared<const std::string>("existing data");
cache.put(resource, response, FileCache::Hint::Full);
cache.put(resource, response, SQLiteCache::Hint::Full);
std::unique_ptr<FileRequest> req1;
std::unique_ptr<WorkRequest> req2;
@@ -124,7 +124,7 @@ TEST_F(Storage, DontCacheConnectionErrors) {
// Insert existing data into the cache that will be marked as stale.
auto response = std::make_shared<Response>();
response->data = std::make_shared<const std::string>("existing data");
cache.put(resource, response, FileCache::Hint::Full);
cache.put(resource, response, SQLiteCache::Hint::Full);
std::unique_ptr<FileRequest> req1;
std::unique_ptr<WorkRequest> req2;
@@ -180,7 +180,7 @@ TEST_F(Storage, DontCacheServerErrors) {
// Insert existing data into the cache that will be marked as stale.
auto response = std::make_shared<Response>();
response->data = std::make_shared<const std::string>("existing data");
cache.put(resource, response, FileCache::Hint::Full);
cache.put(resource, response, SQLiteCache::Hint::Full);
std::unique_ptr<FileRequest> req1;
std::unique_ptr<WorkRequest> req2;