Quark  0.1
WriteBackCache.h++
Go to the documentation of this file.
1 
2 #ifndef __libquark_util_WriteBackCache_hxx
3 #define __libquark_util_WriteBackCache_hxx
4 
5 #include <QHash>
6 
7 namespace quark {
8 namespace util {
9 
21 template<typename K, class T> class WriteBackCache
22 {
23  public:
24 
26  virtual ~WriteBackCache();
27 
33  void insert(K key, T *value);
34 
41  T* lookup(K key);
42 
50  bool remove(K key);
51 
59  bool sync();
60 
68  bool flush();
69 
78  void setDirty(K key, bool dirty);
79 
84  bool isDirty(K key) const;
85 
86  protected:
87 
89  WriteBackCache(int maxCapacity);
90 
96  virtual void write(const T *value) = 0;
97 
98  private:
99 
100  struct Item
101  {
102  Item(K key, T *value)
103  : key(key),
104  value(value),
105  dirty(false),
106  next(NULL),
107  prev(NULL)
108  { }
109 
110  K key;
111  T *value;
112  bool dirty;
113  struct Item *next;
114  struct Item *prev;
115  };
116 
117  void link(Item *item);
118  void unlink(Item *item);
119  bool iterate(bool doWrite, bool doDelete);
120 
121  int _maxCapacity;
122  QHash<K, Item*> _map;
123  Item *_head; // MRU
124  Item *_tail;
125 };
126 
127 #include <quark/WriteBackCacheImpl.h++>
128 
129 } // namespace util
130 } // namespace quark
131 
132 #endif // __libquark_util_WriteBackCache_hxx
bool flush()
Flushes the cache.
bool isDirty(K key) const
virtual ~WriteBackCache()
Destructor.
void insert(K key, T *value)
Inserts a key/value pair into the cache.
virtual void write(const T *value)=0
Writes a cached value to the cache source.
Definition: BarChartView.h++:6
T * lookup(K key)
Looks up the value for a given key in the cache.
WriteBackCache(int maxCapacity)
Constructs a new WriteBackCache with the given maximum capacity.
An abstract LRU cache implementation that tracks modifications to the cached items and performs write...
Definition: WriteBackCache.h++:21
bool sync()
Synchronizes the cache contents with the cache source.
void setDirty(K key, bool dirty)
Sets the "dirty" flag for the value for a given key in the cache.