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_MEM_BACKEND_IMPL_H__
8 #define NET_DISK_CACHE_MEM_BACKEND_IMPL_H__
10 #include "base/compiler_specific.h"
11 #include "base/containers/hash_tables.h"
12 #include "net/disk_cache/disk_cache.h"
13 #include "net/disk_cache/mem_rankings.h"
19 namespace disk_cache
{
23 // This class implements the Backend interface. An object of this class handles
24 // the operations of the cache without writing to disk.
25 class NET_EXPORT_PRIVATE MemBackendImpl
: public Backend
{
27 explicit MemBackendImpl(net::NetLog
* net_log
);
28 virtual ~MemBackendImpl();
30 // Returns an instance of a Backend implemented only in memory. The returned
31 // object should be deleted when not needed anymore. max_bytes is the maximum
32 // size the cache can grow to. If zero is passed in as max_bytes, the cache
33 // will determine the value to use based on the available memory. The returned
34 // pointer can be NULL if a fatal error is found.
35 static Backend
* CreateBackend(int max_bytes
, net::NetLog
* net_log
);
37 // Performs general initialization for this current instance of the cache.
40 // Sets the maximum size for the total amount of data stored by this instance.
41 bool SetMaxSize(int max_bytes
);
43 // Permanently deletes an entry.
44 void InternalDoomEntry(MemEntryImpl
* entry
);
46 // Updates the ranking information for an entry.
47 void UpdateRank(MemEntryImpl
* node
);
49 // A user data block is being created, extended or truncated.
50 void ModifyStorageSize(int32 old_size
, int32 new_size
);
52 // Returns the maximum size for a file to reside on the cache.
53 int MaxFileSize() const;
55 // Insert an MemEntryImpl into the ranking list. This method is only called
56 // from MemEntryImpl to insert child entries. The reference can be removed
57 // by calling RemoveFromRankingList(|entry|).
58 void InsertIntoRankingList(MemEntryImpl
* entry
);
60 // Remove |entry| from ranking list. This method is only called from
61 // MemEntryImpl to remove a child entry from the ranking list.
62 void RemoveFromRankingList(MemEntryImpl
* entry
);
65 virtual net::CacheType
GetCacheType() const OVERRIDE
;
66 virtual int32
GetEntryCount() const OVERRIDE
;
67 virtual int OpenEntry(const std::string
& key
, Entry
** entry
,
68 const CompletionCallback
& callback
) OVERRIDE
;
69 virtual int CreateEntry(const std::string
& key
, Entry
** entry
,
70 const CompletionCallback
& callback
) OVERRIDE
;
71 virtual int DoomEntry(const std::string
& key
,
72 const CompletionCallback
& callback
) OVERRIDE
;
73 virtual int DoomAllEntries(const CompletionCallback
& callback
) OVERRIDE
;
74 virtual int DoomEntriesBetween(base::Time initial_time
,
76 const CompletionCallback
& callback
) OVERRIDE
;
77 virtual int DoomEntriesSince(base::Time initial_time
,
78 const CompletionCallback
& callback
) OVERRIDE
;
79 virtual int OpenNextEntry(void** iter
, Entry
** next_entry
,
80 const CompletionCallback
& callback
) OVERRIDE
;
81 virtual void EndEnumeration(void** iter
) OVERRIDE
;
82 virtual void GetStats(
83 std::vector
<std::pair
<std::string
, std::string
> >* stats
) OVERRIDE
{}
84 virtual void OnExternalCacheHit(const std::string
& key
) OVERRIDE
;
87 typedef base::hash_map
<std::string
, MemEntryImpl
*> EntryMap
;
89 // Old Backend interface.
90 bool OpenEntry(const std::string
& key
, Entry
** entry
);
91 bool CreateEntry(const std::string
& key
, Entry
** entry
);
92 bool DoomEntry(const std::string
& key
);
93 bool DoomAllEntries();
94 bool DoomEntriesBetween(const base::Time initial_time
,
95 const base::Time end_time
);
96 bool DoomEntriesSince(const base::Time initial_time
);
97 bool OpenNextEntry(void** iter
, Entry
** next_entry
);
99 // Deletes entries from the cache until the current size is below the limit.
100 // If empty is true, the whole cache will be trimmed, regardless of being in
102 void TrimCache(bool empty
);
104 // Handles the used storage count.
105 void AddStorageSize(int32 bytes
);
106 void SubstractStorageSize(int32 bytes
);
109 MemRankings rankings_
; // Rankings to be able to trim the cache.
110 int32 max_size_
; // Maximum data size for this instance.
113 net::NetLog
* net_log_
;
115 DISALLOW_COPY_AND_ASSIGN(MemBackendImpl
);
118 } // namespace disk_cache
120 #endif // NET_DISK_CACHE_MEM_BACKEND_IMPL_H__