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.
5 // See net/disk_cache/disk_cache.h for the public interface of the cache.
7 #ifndef NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
8 #define NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
10 #include "base/compiler_specific.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/weak_ptr.h"
13 #include "net/disk_cache/disk_cache.h"
14 #include "net/disk_cache/memory/mem_rankings.h"
20 namespace disk_cache
{
24 // This class implements the Backend interface. An object of this class handles
25 // the operations of the cache without writing to disk.
26 class NET_EXPORT_PRIVATE MemBackendImpl
: public Backend
{
28 explicit MemBackendImpl(net::NetLog
* net_log
);
29 ~MemBackendImpl() override
;
31 // Returns an instance of a Backend implemented only in memory. The returned
32 // object should be deleted when not needed anymore. max_bytes is the maximum
33 // size the cache can grow to. If zero is passed in as max_bytes, the cache
34 // will determine the value to use based on the available memory. The returned
35 // pointer can be NULL if a fatal error is found.
36 static scoped_ptr
<Backend
> CreateBackend(int max_bytes
, net::NetLog
* net_log
);
38 // Performs general initialization for this current instance of the cache.
41 // Sets the maximum size for the total amount of data stored by this instance.
42 bool SetMaxSize(int max_bytes
);
44 // Permanently deletes an entry.
45 void InternalDoomEntry(MemEntryImpl
* entry
);
47 // Updates the ranking information for an entry.
48 void UpdateRank(MemEntryImpl
* node
);
50 // A user data block is being created, extended or truncated.
51 void ModifyStorageSize(int32 old_size
, int32 new_size
);
53 // Returns the maximum size for a file to reside on the cache.
54 int MaxFileSize() const;
56 // Insert an MemEntryImpl into the ranking list. This method is only called
57 // from MemEntryImpl to insert child entries. The reference can be removed
58 // by calling RemoveFromRankingList(|entry|).
59 void InsertIntoRankingList(MemEntryImpl
* entry
);
61 // Remove |entry| from ranking list. This method is only called from
62 // MemEntryImpl to remove a child entry from the ranking list.
63 void RemoveFromRankingList(MemEntryImpl
* entry
);
66 net::CacheType
GetCacheType() const override
;
67 int32
GetEntryCount() const override
;
68 int OpenEntry(const std::string
& key
,
70 const CompletionCallback
& callback
) override
;
71 int CreateEntry(const std::string
& key
,
73 const CompletionCallback
& callback
) override
;
74 int DoomEntry(const std::string
& key
,
75 const CompletionCallback
& callback
) override
;
76 int DoomAllEntries(const CompletionCallback
& callback
) override
;
77 int DoomEntriesBetween(base::Time initial_time
,
79 const CompletionCallback
& callback
) override
;
80 int DoomEntriesSince(base::Time initial_time
,
81 const CompletionCallback
& callback
) override
;
82 scoped_ptr
<Iterator
> CreateIterator() override
;
84 std::vector
<std::pair
<std::string
, std::string
>>* stats
) override
{}
85 void OnExternalCacheHit(const std::string
& key
) override
;
89 friend class MemIterator
;
91 typedef base::hash_map
<std::string
, MemEntryImpl
*> EntryMap
;
93 // Old Backend interface.
94 bool OpenEntry(const std::string
& key
, Entry
** entry
);
95 bool CreateEntry(const std::string
& key
, Entry
** entry
);
96 bool DoomEntry(const std::string
& key
);
97 bool DoomAllEntries();
98 bool DoomEntriesBetween(const base::Time initial_time
,
99 const base::Time end_time
);
100 bool DoomEntriesSince(const base::Time initial_time
);
102 // Deletes entries from the cache until the current size is below the limit.
103 // If empty is true, the whole cache will be trimmed, regardless of being in
105 void TrimCache(bool empty
);
107 // Handles the used storage count.
108 void AddStorageSize(int32 bytes
);
109 void SubstractStorageSize(int32 bytes
);
112 MemRankings rankings_
; // Rankings to be able to trim the cache.
113 int32 max_size_
; // Maximum data size for this instance.
116 net::NetLog
* net_log_
;
118 base::WeakPtrFactory
<MemBackendImpl
> weak_factory_
;
120 DISALLOW_COPY_AND_ASSIGN(MemBackendImpl
);
123 } // namespace disk_cache
125 #endif // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_