diff options
author | simonjam@chromium.org <simonjam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-02 20:13:28 +0000 |
---|---|---|
committer | simonjam@chromium.org <simonjam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-02 20:13:28 +0000 |
commit | 704359693f5c8507635155ae117c466a7149065a (patch) | |
tree | 55778bdae67f960440441085372fc98c1f974c80 /net | |
parent | 89b2312b5aca51da726b93e966842ffd5f642402 (diff) | |
download | chromium_src-704359693f5c8507635155ae117c466a7149065a.zip chromium_src-704359693f5c8507635155ae117c466a7149065a.tar.gz chromium_src-704359693f5c8507635155ae117c466a7149065a.tar.bz2 |
Inform disk cache of WebKit memory cache hits.
BUG=37112
TEST=net_unittests
Review URL: http://codereview.chromium.org/7461106
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95145 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/disk_cache/backend_impl.cc | 16 | ||||
-rw-r--r-- | net/disk_cache/backend_impl.h | 2 | ||||
-rw-r--r-- | net/disk_cache/backend_unittest.cc | 23 | ||||
-rw-r--r-- | net/disk_cache/disk_cache.h | 4 | ||||
-rw-r--r-- | net/disk_cache/in_flight_backend_io.cc | 17 | ||||
-rw-r--r-- | net/disk_cache/in_flight_backend_io.h | 5 | ||||
-rw-r--r-- | net/disk_cache/mem_backend_impl.cc | 7 | ||||
-rw-r--r-- | net/disk_cache/mem_backend_impl.h | 1 | ||||
-rw-r--r-- | net/http/http_cache.cc | 12 | ||||
-rw-r--r-- | net/http/http_cache.h | 5 | ||||
-rw-r--r-- | net/http/http_cache_unittest.cc | 10 |
11 files changed, 95 insertions, 7 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc index b03ecda..deacdd3 100644 --- a/net/disk_cache/backend_impl.cc +++ b/net/disk_cache/backend_impl.cc @@ -682,6 +682,18 @@ void BackendImpl::SyncEndEnumeration(void* iter) { reinterpret_cast<Rankings::Iterator*>(iter)); } +void BackendImpl::SyncOnExternalCacheHit(const std::string& key) { + uint32 hash = Hash(key); + bool error; + EntryImpl* cache_entry = MatchEntry(key, hash, false, Addr(), &error); + if (cache_entry) { + if (ENTRY_NORMAL == cache_entry->entry()->Data()->state) { + UpdateRank(cache_entry, false); + } + cache_entry->Release(); + } +} + EntryImpl* BackendImpl::OpenEntryImpl(const std::string& key) { if (disabled_) return NULL; @@ -1356,6 +1368,10 @@ void BackendImpl::GetStats(StatsItems* stats) { stats_.GetItems(stats); } +void BackendImpl::OnExternalCacheHit(const std::string& key) { + background_queue_.OnExternalCacheHit(key); +} + // ------------------------------------------------------------------------ // We just created a new file so we're going to write the header and set the diff --git a/net/disk_cache/backend_impl.h b/net/disk_cache/backend_impl.h index cbf6b86..1ead317 100644 --- a/net/disk_cache/backend_impl.h +++ b/net/disk_cache/backend_impl.h @@ -79,6 +79,7 @@ class NET_TEST BackendImpl : public Backend { int SyncOpenNextEntry(void** iter, Entry** next_entry); int SyncOpenPrevEntry(void** iter, Entry** prev_entry); void SyncEndEnumeration(void* iter); + void SyncOnExternalCacheHit(const std::string& key); // Open or create an entry for the given |key| or |iter|. EntryImpl* OpenEntryImpl(const std::string& key); @@ -263,6 +264,7 @@ class NET_TEST BackendImpl : public Backend { CompletionCallback* callback); virtual void EndEnumeration(void** iter); virtual void GetStats(StatsItems* stats); + virtual void OnExternalCacheHit(const std::string& key); private: typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc index 1cde9b6..3bf1aac 100644 --- a/net/disk_cache/backend_unittest.cc +++ b/net/disk_cache/backend_unittest.cc @@ -2118,3 +2118,26 @@ TEST_F(DiskCacheBackendTest, FileSharing) { EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); } + +TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) { + SetDirectMode(); + InitCache(); + + disk_cache::Entry* entry; + + for (int i = 0; i < 2; ++i) { + std::string key = StringPrintf("key%d", i); + ASSERT_EQ(net::OK, CreateEntry(key, &entry)); + entry->Close(); + } + + // Ping the oldest entry. + cache_->OnExternalCacheHit("key0"); + + TrimForTest(false); + + // Make sure the older key remains. + EXPECT_EQ(1, cache_->GetEntryCount()); + ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); + entry->Close(); +} diff --git a/net/disk_cache/disk_cache.h b/net/disk_cache/disk_cache.h index 973b6e6..dc320cc 100644 --- a/net/disk_cache/disk_cache.h +++ b/net/disk_cache/disk_cache.h @@ -137,6 +137,10 @@ class NET_API Backend { // Return a list of cache statistics. virtual void GetStats( std::vector<std::pair<std::string, std::string> >* stats) = 0; + + // Called whenever an external cache in the system reuses the resource + // referred to by |key|. + virtual void OnExternalCacheHit(const std::string& key) = 0; }; // This interface represents an entry in the disk cache. diff --git a/net/disk_cache/in_flight_backend_io.cc b/net/disk_cache/in_flight_backend_io.cc index 5d4be18..259d888 100644 --- a/net/disk_cache/in_flight_backend_io.cc +++ b/net/disk_cache/in_flight_backend_io.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -105,6 +105,11 @@ void BackendIO::EndEnumeration(void* iterator) { iter_ = iterator; } +void BackendIO::OnExternalCacheHit(const std::string& key) { + operation_ = OP_ON_EXTERNAL_CACHE_HIT; + key_ = key; +} + void BackendIO::CloseEntryImpl(EntryImpl* entry) { operation_ = OP_CLOSE_ENTRY; entry_ = entry; @@ -218,6 +223,10 @@ void BackendIO::ExecuteBackendOperation() { backend_->SyncEndEnumeration(iter_); result_ = net::OK; break; + case OP_ON_EXTERNAL_CACHE_HIT: + backend_->SyncOnExternalCacheHit(key_); + result_ = net::OK; + break; case OP_CLOSE_ENTRY: entry_->Release(); result_ = net::OK; @@ -358,6 +367,12 @@ void InFlightBackendIO::EndEnumeration(void* iterator) { PostOperation(operation); } +void InFlightBackendIO::OnExternalCacheHit(const std::string& key) { + scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, NULL)); + operation->OnExternalCacheHit(key); + PostOperation(operation); +} + void InFlightBackendIO::CloseEntryImpl(EntryImpl* entry) { scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, NULL)); operation->CloseEntryImpl(entry); diff --git a/net/disk_cache/in_flight_backend_io.h b/net/disk_cache/in_flight_backend_io.h index 659a33a..a8a86eb 100644 --- a/net/disk_cache/in_flight_backend_io.h +++ b/net/disk_cache/in_flight_backend_io.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -55,6 +55,7 @@ class BackendIO : public BackgroundIO { void OpenNextEntry(void** iter, Entry** next_entry); void OpenPrevEntry(void** iter, Entry** prev_entry); void EndEnumeration(void* iterator); + void OnExternalCacheHit(const std::string& key); void CloseEntryImpl(EntryImpl* entry); void DoomEntryImpl(EntryImpl* entry); void FlushQueue(); // Dummy operation. @@ -89,6 +90,7 @@ class BackendIO : public BackgroundIO { OP_OPEN_NEXT, OP_OPEN_PREV, OP_END_ENUMERATION, + OP_ON_EXTERNAL_CACHE_HIT, OP_CLOSE_ENTRY, OP_DOOM_ENTRY, OP_FLUSH_QUEUE, @@ -159,6 +161,7 @@ class InFlightBackendIO : public InFlightIO { void OpenPrevEntry(void** iter, Entry** prev_entry, net::CompletionCallback* callback); void EndEnumeration(void* iterator); + void OnExternalCacheHit(const std::string& key); void CloseEntryImpl(EntryImpl* entry); void DoomEntryImpl(EntryImpl* entry); void FlushQueue(net::CompletionCallback* callback); diff --git a/net/disk_cache/mem_backend_impl.cc b/net/disk_cache/mem_backend_impl.cc index 6915ede..2f7713b 100644 --- a/net/disk_cache/mem_backend_impl.cc +++ b/net/disk_cache/mem_backend_impl.cc @@ -188,6 +188,13 @@ void MemBackendImpl::EndEnumeration(void** iter) { *iter = NULL; } +void MemBackendImpl::OnExternalCacheHit(const std::string& key) { + EntryMap::iterator it = entries_.find(key); + if (it != entries_.end()) { + UpdateRank(it->second); + } +} + bool MemBackendImpl::OpenEntry(const std::string& key, Entry** entry) { EntryMap::iterator it = entries_.find(key); if (it == entries_.end()) diff --git a/net/disk_cache/mem_backend_impl.h b/net/disk_cache/mem_backend_impl.h index 52a78f6..26a2556 100644 --- a/net/disk_cache/mem_backend_impl.h +++ b/net/disk_cache/mem_backend_impl.h @@ -80,6 +80,7 @@ class NET_TEST MemBackendImpl : public Backend { virtual void EndEnumeration(void** iter); virtual void GetStats( std::vector<std::pair<std::string, std::string> >* stats) {} + virtual void OnExternalCacheHit(const std::string& key); private: typedef base::hash_map<std::string, MemEntryImpl*> EntryMap; diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc index 157059df..c1bb0d8 100644 --- a/net/http/http_cache.cc +++ b/net/http/http_cache.cc @@ -463,6 +463,18 @@ void HttpCache::CloseAllConnections() { session->CloseAllConnections(); } +void HttpCache::OnExternalCacheHit(const GURL& url, + const std::string& http_method) { + if (!disk_cache_.get()) + return; + + HttpRequestInfo request_info; + request_info.url = url; + request_info.method = http_method; + std::string key = GenerateCacheKey(&request_info); + disk_cache_->OnExternalCacheHit(key); +} + int HttpCache::CreateTransaction(scoped_ptr<HttpTransaction>* trans) { // Do lazy initialization of disk cache if needed. if (!disk_cache_.get()) diff --git a/net/http/http_cache.h b/net/http/http_cache.h index 861d36b..1f5fa5f1 100644 --- a/net/http/http_cache.h +++ b/net/http/http_cache.h @@ -179,6 +179,10 @@ class NET_API HttpCache : public HttpTransactionFactory, // immediately, but they will not be reusable. This is for debugging. void CloseAllConnections(); + // Called whenever an external cache in the system reuses the resource + // referred to by |url| and |http_method|. + void OnExternalCacheHit(const GURL& url, const std::string& http_method); + // HttpTransactionFactory implementation: virtual int CreateTransaction(scoped_ptr<HttpTransaction>* trans); virtual HttpCache* GetCache(); @@ -345,7 +349,6 @@ class NET_API HttpCache : public HttpTransactionFactory, // Processes the backend creation notification. void OnBackendCreated(int result, PendingOp* pending_op); - // Variables ---------------------------------------------------------------- NetLog* net_log_; diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc index 493c4e6..b467dc5 100644 --- a/net/http/http_cache_unittest.cc +++ b/net/http/http_cache_unittest.cc @@ -72,13 +72,13 @@ class MockDiskEntry : public disk_cache::Entry, public base::RefCounted<MockDiskEntry> { public: MockDiskEntry() - : test_mode_(0), doomed_(false), sparse_(false), fail_requests_(false), - busy_(false), delayed_(false) { + : test_mode_(0), doomed_(false), sparse_(false), + fail_requests_(false), busy_(false), delayed_(false) { } explicit MockDiskEntry(const std::string& key) - : key_(key), doomed_(false), sparse_(false), fail_requests_(false), - busy_(false), delayed_(false) { + : key_(key), doomed_(false), sparse_(false), + fail_requests_(false), busy_(false), delayed_(false) { test_mode_ = GetTestModeForEntry(key); } @@ -487,6 +487,8 @@ class MockDiskCache : public disk_cache::Backend { std::vector<std::pair<std::string, std::string> >* stats) { } + virtual void OnExternalCacheHit(const std::string& key) {} + // returns number of times a cache entry was successfully opened int open_count() const { return open_count_; } |