OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // See net/disk_cache/disk_cache.h for the public interface of the cache. | 5 // See net/disk_cache/disk_cache.h for the public interface of the cache. |
6 | 6 |
7 #ifndef NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_ | 7 #ifndef NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_ |
8 #define NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_ | 8 #define NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_ |
9 | 9 |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 | 11 |
| 12 #include <string> |
| 13 |
12 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
13 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 16 #include "base/containers/linked_list.h" |
14 #include "base/macros.h" | 17 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
16 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 20 #include "base/time/time.h" |
17 #include "net/disk_cache/disk_cache.h" | 21 #include "net/disk_cache/disk_cache.h" |
18 #include "net/disk_cache/memory/mem_rankings.h" | 22 #include "net/disk_cache/memory/mem_entry_impl.h" |
19 | 23 |
20 namespace net { | 24 namespace net { |
21 class NetLog; | 25 class NetLog; |
22 } // namespace net | 26 } // namespace net |
23 | 27 |
24 namespace disk_cache { | 28 namespace disk_cache { |
25 | 29 |
26 class MemEntryImpl; | |
27 | |
28 // This class implements the Backend interface. An object of this class handles | 30 // This class implements the Backend interface. An object of this class handles |
29 // the operations of the cache without writing to disk. | 31 // the operations of the cache without writing to disk. |
30 class NET_EXPORT_PRIVATE MemBackendImpl : public Backend { | 32 class NET_EXPORT_PRIVATE MemBackendImpl final : public Backend { |
31 public: | 33 public: |
32 explicit MemBackendImpl(net::NetLog* net_log); | 34 explicit MemBackendImpl(net::NetLog* net_log); |
33 ~MemBackendImpl() override; | 35 ~MemBackendImpl() override; |
34 | 36 |
35 // Returns an instance of a Backend implemented only in memory. The returned | 37 // Returns an instance of a Backend implemented only in memory. The returned |
36 // object should be deleted when not needed anymore. max_bytes is the maximum | 38 // object should be deleted when not needed anymore. max_bytes is the maximum |
37 // size the cache can grow to. If zero is passed in as max_bytes, the cache | 39 // size the cache can grow to. If zero is passed in as max_bytes, the cache |
38 // will determine the value to use based on the available memory. The returned | 40 // will determine the value to use based on the available memory. The returned |
39 // pointer can be NULL if a fatal error is found. | 41 // pointer can be NULL if a fatal error is found. |
40 static scoped_ptr<Backend> CreateBackend(int max_bytes, net::NetLog* net_log); | 42 static scoped_ptr<Backend> CreateBackend(int max_bytes, net::NetLog* net_log); |
41 | 43 |
42 // Performs general initialization for this current instance of the cache. | 44 // Performs general initialization for this current instance of the cache. |
43 bool Init(); | 45 bool Init(); |
44 | 46 |
45 // Sets the maximum size for the total amount of data stored by this instance. | 47 // Sets the maximum size for the total amount of data stored by this instance. |
46 bool SetMaxSize(int max_bytes); | 48 bool SetMaxSize(int max_bytes); |
47 | 49 |
48 // Permanently deletes an entry. | |
49 void InternalDoomEntry(MemEntryImpl* entry); | |
50 | |
51 // Updates the ranking information for an entry. | |
52 void UpdateRank(MemEntryImpl* node); | |
53 | |
54 // A user data block is being created, extended or truncated. | |
55 void ModifyStorageSize(int32_t old_size, int32_t new_size); | |
56 | |
57 // Returns the maximum size for a file to reside on the cache. | 50 // Returns the maximum size for a file to reside on the cache. |
58 int MaxFileSize() const; | 51 int MaxFileSize() const; |
59 | 52 |
60 // Insert an MemEntryImpl into the ranking list. This method is only called | 53 // These next methods (before the implementation of the Backend interface) are |
61 // from MemEntryImpl to insert child entries. The reference can be removed | 54 // called by MemEntryImpl to update the state of the backend during the entry |
62 // by calling RemoveFromRankingList(|entry|). | 55 // lifecycle. |
63 void InsertIntoRankingList(MemEntryImpl* entry); | |
64 | 56 |
65 // Remove |entry| from ranking list. This method is only called from | 57 // Signals that new entry has been created, and should be placed in |
66 // MemEntryImpl to remove a child entry from the ranking list. | 58 // |lru_list_| so that it is eligable for eviction. |
67 void RemoveFromRankingList(MemEntryImpl* entry); | 59 void OnEntryInserted(MemEntryImpl* entry); |
| 60 |
| 61 // Signals that an entry has been updated, and thus should be moved to the end |
| 62 // of |lru_list_|. |
| 63 void OnEntryUpdated(MemEntryImpl* entry); |
| 64 |
| 65 // Signals that an entry has been doomed, and so it should be removed from the |
| 66 // list of active entries as appropriate, as well as removed from the |
| 67 // |lru_list_|. |
| 68 void OnEntryDoomed(MemEntryImpl* entry); |
| 69 |
| 70 // Adjust the current size of this backend by |delta|. This is used to |
| 71 // determine if eviction is neccessary and when eviction is finished. |
| 72 void ModifyStorageSize(int32_t delta); |
68 | 73 |
69 // Backend interface. | 74 // Backend interface. |
70 net::CacheType GetCacheType() const override; | 75 net::CacheType GetCacheType() const override; |
71 int32_t GetEntryCount() const override; | 76 int32_t GetEntryCount() const override; |
72 int OpenEntry(const std::string& key, | 77 int OpenEntry(const std::string& key, |
73 Entry** entry, | 78 Entry** entry, |
74 const CompletionCallback& callback) override; | 79 const CompletionCallback& callback) override; |
75 int CreateEntry(const std::string& key, | 80 int CreateEntry(const std::string& key, |
76 Entry** entry, | 81 Entry** entry, |
77 const CompletionCallback& callback) override; | 82 const CompletionCallback& callback) override; |
78 int DoomEntry(const std::string& key, | 83 int DoomEntry(const std::string& key, |
79 const CompletionCallback& callback) override; | 84 const CompletionCallback& callback) override; |
80 int DoomAllEntries(const CompletionCallback& callback) override; | 85 int DoomAllEntries(const CompletionCallback& callback) override; |
81 int DoomEntriesBetween(base::Time initial_time, | 86 int DoomEntriesBetween(base::Time initial_time, |
82 base::Time end_time, | 87 base::Time end_time, |
83 const CompletionCallback& callback) override; | 88 const CompletionCallback& callback) override; |
84 int DoomEntriesSince(base::Time initial_time, | 89 int DoomEntriesSince(base::Time initial_time, |
85 const CompletionCallback& callback) override; | 90 const CompletionCallback& callback) override; |
86 int CalculateSizeOfAllEntries(const CompletionCallback& callback) override; | 91 int CalculateSizeOfAllEntries(const CompletionCallback& callback) override; |
87 scoped_ptr<Iterator> CreateIterator() override; | 92 scoped_ptr<Iterator> CreateIterator() override; |
88 void GetStats(base::StringPairs* stats) override {} | 93 void GetStats(base::StringPairs* stats) override {} |
89 void OnExternalCacheHit(const std::string& key) override; | 94 void OnExternalCacheHit(const std::string& key) override; |
90 | 95 |
91 private: | 96 private: |
92 class MemIterator; | 97 class MemIterator; |
93 friend class MemIterator; | 98 friend class MemIterator; |
94 | 99 |
95 typedef base::hash_map<std::string, MemEntryImpl*> EntryMap; | 100 typedef base::hash_map<std::string, MemEntryImpl*> EntryMap; |
96 | 101 |
97 // Old Backend interface. | |
98 bool OpenEntry(const std::string& key, Entry** entry); | |
99 bool CreateEntry(const std::string& key, Entry** entry); | |
100 bool DoomEntry(const std::string& key); | |
101 bool DoomAllEntries(); | |
102 bool DoomEntriesBetween(const base::Time initial_time, | |
103 const base::Time end_time); | |
104 bool DoomEntriesSince(const base::Time initial_time); | |
105 | |
106 // Deletes entries from the cache until the current size is below the limit. | 102 // Deletes entries from the cache until the current size is below the limit. |
107 // If empty is true, the whole cache will be trimmed, regardless of being in | 103 void EvictIfNeeded(); |
108 // use. | |
109 void TrimCache(bool empty); | |
110 | |
111 // Handles the used storage count. | |
112 void AddStorageSize(int32_t bytes); | |
113 void SubstractStorageSize(int32_t bytes); | |
114 | 104 |
115 EntryMap entries_; | 105 EntryMap entries_; |
116 MemRankings rankings_; // Rankings to be able to trim the cache. | 106 |
| 107 // Stored in increasing order of last use time, from least recently used to |
| 108 // most recently used. |
| 109 base::LinkedList<MemEntryImpl> lru_list_; |
| 110 |
117 int32_t max_size_; // Maximum data size for this instance. | 111 int32_t max_size_; // Maximum data size for this instance. |
118 int32_t current_size_; | 112 int32_t current_size_; |
119 | 113 |
120 net::NetLog* net_log_; | 114 net::NetLog* net_log_; |
121 | 115 |
122 base::WeakPtrFactory<MemBackendImpl> weak_factory_; | 116 base::WeakPtrFactory<MemBackendImpl> weak_factory_; |
123 | 117 |
124 DISALLOW_COPY_AND_ASSIGN(MemBackendImpl); | 118 DISALLOW_COPY_AND_ASSIGN(MemBackendImpl); |
125 }; | 119 }; |
126 | 120 |
127 } // namespace disk_cache | 121 } // namespace disk_cache |
128 | 122 |
129 #endif // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_ | 123 #endif // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_ |
OLD | NEW |