1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// See net/disk_cache/disk_cache.h for the public interface of the cache.
#ifndef NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
#define NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
#include <stdint.h>
#include <string>
#include <unordered_map>
#include "base/compiler_specific.h"
#include "base/containers/linked_list.h"
#include "base/functional/callback_forward.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_split.h"
#include "base/time/time.h"
#include "net/base/net_export.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/memory/mem_entry_impl.h"
namespace base {
class Clock;
}
namespace net {
class NetLog;
} // namespace net
namespace disk_cache {
// This class implements the Backend interface. An object of this class handles
// the operations of the cache without writing to disk.
class NET_EXPORT_PRIVATE MemBackendImpl final : public Backend {
public:
explicit MemBackendImpl(net::NetLog* net_log);
MemBackendImpl(const MemBackendImpl&) = delete;
MemBackendImpl& operator=(const MemBackendImpl&) = delete;
~MemBackendImpl() override;
// Returns an instance of a Backend implemented only in memory. The returned
// object should be deleted when not needed anymore. max_bytes is the maximum
// size the cache can grow to. If zero is passed in as max_bytes, the cache
// will determine the value to use based on the available memory. The returned
// pointer can be NULL if a fatal error is found.
static std::unique_ptr<MemBackendImpl> CreateBackend(int64_t max_bytes,
net::NetLog* net_log);
// Performs general initialization for this current instance of the cache.
bool Init();
// Returns the maximum size for a file to reside on the cache.
int64_t MaxFileSize() const override;
// These next methods (before the implementation of the Backend interface) are
// called by MemEntryImpl to update the state of the backend during the entry
// lifecycle.
// Signals that new entry has been created, and should be placed in
// |lru_list_| so that it is eligable for eviction.
void OnEntryInserted(MemEntryImpl* entry);
// Signals that an entry has been updated, and thus should be moved to the end
// of |lru_list_|.
void OnEntryUpdated(MemEntryImpl* entry);
// Signals that an entry has been doomed, and so it should be removed from the
// list of active entries as appropriate, as well as removed from the
// |lru_list_|.
void OnEntryDoomed(MemEntryImpl* entry);
// Adjust the current size of this backend by |delta|. This is used to
// determine if eviction is necessary and when eviction is finished.
void ModifyStorageSize(int32_t delta);
// Returns true if the cache's size is greater than the maximum allowed
// size.
bool HasExceededStorageSize() const;
// Sets a callback to be posted after we are destroyed. Should be called at
// most once.
void SetPostCleanupCallback(base::OnceClosure cb);
static base::Time Now(const base::WeakPtr<MemBackendImpl>& self);
void SetClockForTesting(base::Clock* clock); // doesn't take ownership.
// Backend interface.
int32_t GetEntryCount(
net::Int32CompletionOnceCallback callback) const override;
EntryResult OpenOrCreateEntry(const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) override;
EntryResult OpenEntry(const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) override;
EntryResult CreateEntry(const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) override;
net::Error DoomEntry(const std::string& key,
net::RequestPriority priority,
CompletionOnceCallback callback) override;
net::Error DoomAllEntries(CompletionOnceCallback callback) override;
net::Error DoomEntriesBetween(base::Time initial_time,
base::Time end_time,
CompletionOnceCallback callback) override;
net::Error DoomEntriesSince(base::Time initial_time,
CompletionOnceCallback callback) override;
int64_t CalculateSizeOfAllEntries(
Int64CompletionOnceCallback callback) override;
int64_t CalculateSizeOfEntriesBetween(
base::Time initial_time,
base::Time end_time,
Int64CompletionOnceCallback callback) override;
std::unique_ptr<Iterator> CreateIterator() override;
void GetStats(base::StringPairs* stats) override {}
void OnExternalCacheHit(const std::string& key) override;
private:
class MemIterator;
friend class MemIterator;
using EntryMap =
std::unordered_map<std::string, raw_ptr<MemEntryImpl, CtnExperimental>>;
// Sets the maximum size for the total amount of data stored by this instance.
bool SetMaxSize(int64_t max_bytes);
// Deletes entries from the cache until the current size is below the limit.
void EvictIfNeeded();
// Deletes entries until the current size is below |goal|.
void EvictTill(int target_size);
// Called when we get low on memory.
void OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
raw_ptr<base::Clock> custom_clock_for_testing_ = nullptr; // usually nullptr.
EntryMap entries_;
// Stored in increasing order of last use time, from least recently used to
// most recently used.
base::LinkedList<MemEntryImpl> lru_list_;
int32_t max_size_ = 0; // Maximum data size for this instance.
int32_t current_size_ = 0;
raw_ptr<net::NetLog> net_log_;
base::OnceClosure post_cleanup_callback_;
base::MemoryPressureListener memory_pressure_listener_;
base::WeakPtrFactory<MemBackendImpl> weak_factory_{this};
};
} // namespace disk_cache
#endif // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
|