mem_backend_impl.h 6.0 KB

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