TMemCachedProducerComp.h
Go to the documentation of this file.
1 #ifndef iproc_TMemCachedProducerComp_included
2 #define iproc_TMemCachedProducerComp_included
3 
4 
5 // Qt includes
6 #include <QtCore/QList>
7 
8 // ACF includes
10 
11 
12 namespace iproc
13 {
14 
15 
20 template <class Key, class CacheObject>
22  public icomp::CComponentBase,
23  virtual public TILockedProducer<Key, CacheObject>
24 {
25 public:
28 
29  I_BEGIN_BASE_COMPONENT(TMemCachedProducerComp);
30  I_REGISTER_INTERFACE(LockedProducerType);
31  I_TASSIGN(m_slaveCacheEngineCompPtr, "SlaveCacheEngine", "Slave cache engine providing access to cached object", true, "SlaveCacheEngine");
32  I_ASSIGN(m_maxCachedObjectsAttrPtr, "MaxCachedObjects", "Maximal number of cached objects", true, 20);
33  I_END_COMPONENT;
34 
35  // reimplemented (iproc::TILockedProducer)
36  virtual const CacheObject* ProduceLockedObject(const Key& key);
37  virtual void UnlockObject(const CacheObject* objectPtr);
38 
39 protected:
43  void CleanElementList();
44 
45 private:
46  I_TREF(LockedProducerType, m_slaveCacheEngineCompPtr);
47  I_ATTR(int, m_maxCachedObjectsAttrPtr);
48 
49  struct ListElement
50  {
51  bool operator==(const Key& key)
52  {
53  return this->key == key;
54  }
55 
56  Key key;
57  const CacheObject* objectPtr;
58  int lockedCount;
59  };
60 
61  typedef QList<ListElement> CachedList;
62  typedef QMap<const CacheObject*, typename CachedList::reverse_iterator> ObjectToListMap;
63 
64  CachedList m_cachedList;
65  ObjectToListMap m_objectToListMap;
66 };
67 
68 
69 // public methods
70 
71 // reimplemented (iproc::TILockedProducer)
72 
73 template <class Key, class CacheObject>
75 {
76  typename CachedList::iterator foundIter = qFind(m_cachedList.begin(), m_cachedList.end(), key);
77  if (foundIter != m_cachedList.end()){
78  foundIter->lockedCount++;
79 
80  return foundIter->objectPtr;
81  }
82 
83  if (m_slaveCacheEngineCompPtr.IsValid()){
84  const CacheObject* objectPtr = m_slaveCacheEngineCompPtr->ProduceLockedObject(key);
85  if (objectPtr != NULL){
86  m_cachedList.push_back(ListElement());
87 
88  ListElement& element = m_cachedList.back();
89  m_objectToListMap[objectPtr] = m_cachedList.rbegin();
90 
91  element.key = key;
92  element.objectPtr = objectPtr;
93  element.lockedCount = 1;
94 
95  CleanElementList();
96 
97  return objectPtr;
98  }
99  }
100 
101  return NULL;
102 }
103 
104 
105 template <class Key, class CacheObject>
107 {
108  typename ObjectToListMap::iterator foundIter = m_objectToListMap.find(objectPtr);
109  Q_ASSERT(foundIter != m_objectToListMap.end()); // if locked is done correctly, this element must exist.
110 
111  typename CachedList::reverse_iterator objectIter = foundIter.value();
112  Q_ASSERT(objectIter != m_cachedList.rend());
113 
114  objectIter->lockedCount--;
115 
116  CleanElementList();
117 }
118 
119 
120 // protected methods
121 
122 template <class Key, class CacheObject>
124 {
125  int maxCachedObjects = qMax(0, *m_maxCachedObjectsAttrPtr);
126 
127  typename CachedList::iterator iter = m_cachedList.begin();
128  while ( (int(m_cachedList.size()) > maxCachedObjects) &&
129  (iter != m_cachedList.end())){
130  Q_ASSERT(m_objectToListMap.find(iter->objectPtr) != m_objectToListMap.end()); // object is present in objects map
131 
132  if (iter->lockedCount <= 0){
133  Q_ASSERT(m_slaveCacheEngineCompPtr.IsValid());
134 
135  m_slaveCacheEngineCompPtr->UnlockObject(iter->objectPtr);
136 
137  m_objectToListMap.erase(iter->objectPtr);
138  iter = m_cachedList.erase(iter);
139  }
140  else{
141  ++iter;
142  }
143  }
144 
145  Q_ASSERT(m_cachedList.size() == m_objectToListMap.size()); // this both structures are coupled, the number of elements must be the same
146 }
147 
148 
149 } // namespace iproc
150 
151 
152 #endif // !iproc_TMemCachedProducerComp_included
153 
154 
void CleanElementList()
Remove elements from list if cumulated weight is above defined maximum.
virtual const CacheObject * ProduceLockedObject(const Key &key)
Begin of accessing to cached element.
Template interface for providers of cached data.
#define NULL
TILockedProducer< Key, CacheObject > LockedProducerType
Template implementation iproc::TILockedProducer buffering objects in memory cache.
virtual void UnlockObject(const CacheObject *objectPtr)
End of accessing to cached element.

© 2007-2011 Witold Gantzke and Kirill Lepskiy