Home | History | Annotate | Download | only in disk_cache
      1 // Copyright (c) 2006-2008 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 
      5 #ifndef NET_DISK_CACHE_EVICTION_H_
      6 #define NET_DISK_CACHE_EVICTION_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/task.h"
     11 #include "net/disk_cache/disk_format.h"
     12 #include "net/disk_cache/rankings.h"
     13 
     14 namespace disk_cache {
     15 
     16 class BackendImpl;
     17 class EntryImpl;
     18 
     19 // This class implements the eviction algorithm for the cache and it is tightly
     20 // integrated with BackendImpl.
     21 class Eviction {
     22  public:
     23   Eviction() : backend_(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {}
     24   ~Eviction() {}
     25 
     26   void Init(BackendImpl* backend);
     27 
     28   // Deletes entries from the cache until the current size is below the limit.
     29   // If empty is true, the whole cache will be trimmed, regardless of being in
     30   // use.
     31   void TrimCache(bool empty);
     32 
     33   // Updates the ranking information for an entry.
     34   void UpdateRank(EntryImpl* entry, bool modified);
     35 
     36   // Notifications of interesting events for a given entry.
     37   void OnOpenEntry(EntryImpl* entry);
     38   void OnCreateEntry(EntryImpl* entry);
     39   void OnDoomEntry(EntryImpl* entry);
     40   void OnDestroyEntry(EntryImpl* entry);
     41 
     42  private:
     43   void PostDelayedTrim();
     44   void DelayedTrim();
     45   void ReportTrimTimes(EntryImpl* entry);
     46   Rankings::List GetListForEntry(EntryImpl* entry);
     47   bool EvictEntry(CacheRankingsBlock* node, bool empty);
     48 
     49   // We'll just keep for a while a separate set of methods that implement the
     50   // new eviction algorithm. This code will replace the original methods when
     51   // finished.
     52   void TrimCacheV2(bool empty);
     53   void UpdateRankV2(EntryImpl* entry, bool modified);
     54   void OnOpenEntryV2(EntryImpl* entry);
     55   void OnCreateEntryV2(EntryImpl* entry);
     56   void OnDoomEntryV2(EntryImpl* entry);
     57   void OnDestroyEntryV2(EntryImpl* entry);
     58   Rankings::List GetListForEntryV2(EntryImpl* entry);
     59   void TrimDeleted(bool empty);
     60   bool RemoveDeletedNode(CacheRankingsBlock* node);
     61 
     62   bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
     63   int SelectListByLenght();
     64   void ReportListStats();
     65 
     66   BackendImpl* backend_;
     67   Rankings* rankings_;
     68   IndexHeader* header_;
     69   int max_size_;
     70   bool new_eviction_;
     71   bool first_trim_;
     72   bool trimming_;
     73   bool delay_trim_;
     74   ScopedRunnableMethodFactory<Eviction> factory_;
     75 
     76   DISALLOW_COPY_AND_ASSIGN(Eviction);
     77 };
     78 
     79 }  // namespace disk_cache
     80 
     81 #endif  // NET_DISK_CACHE_EVICTION_H_
     82