summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/opimcache.h
authorzecke <zecke>2002-10-10 17:08:58 (UTC)
committer zecke <zecke>2002-10-10 17:08:58 (UTC)
commit1dcc1b1fc9fd35d959255452c8b5be1269ca4f44 (patch) (side-by-side diff)
tree469d239dec74f5751f3aced43c4bae1f0f3a42e3 /libopie2/opiepim/core/opimcache.h
parentaa38f642a07810515dcc18ea38bf520e26d7f88e (diff)
downloadopie-1dcc1b1fc9fd35d959255452c8b5be1269ca4f44.zip
opie-1dcc1b1fc9fd35d959255452c8b5be1269ca4f44.tar.gz
opie-1dcc1b1fc9fd35d959255452c8b5be1269ca4f44.tar.bz2
The Cache is finally in place
I tested it with my todolist and it 'works' for 10.000 todos the hits are awesome ;) The read ahead functionality does not make sense for XMLs backends because most of the stuff is already in memory. While using readahead on SQL makes things a lot faster.... I still have to fully implement read ahead This change is bic but sc
Diffstat (limited to 'libopie2/opiepim/core/opimcache.h') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/core/opimcache.h117
1 files changed, 117 insertions, 0 deletions
diff --git a/libopie2/opiepim/core/opimcache.h b/libopie2/opiepim/core/opimcache.h
new file mode 100644
index 0000000..067f6e7
--- a/dev/null
+++ b/libopie2/opiepim/core/opimcache.h
@@ -0,0 +1,117 @@
+#ifndef OPIE_PIM_CACHE_H
+#define OPIE_PIM_CACHE_H
+
+#include <qintcache.h>
+
+#include "opimrecord.h"
+
+template <class T = OPimRecord>
+class OPimCacheItem {
+public:
+ OPimCacheItem( const T& t = T() );
+ ~OPimCacheItem();
+
+ T record()const;
+ void setRecord( const T& );
+private:
+ T m_t;
+};
+
+/**
+ * OPimCache for caching the items
+ * We support adding, removing
+ * and finding
+ */
+template <class T = OPimRecord>
+class OPimCache {
+public:
+ typedef OPimCacheItem<T> Item;
+ OPimCache();
+ ~OPimCache();
+
+ bool contains(int uid)const;
+ void invalidate();
+ void setSize( int size );
+
+ T find(int uid )const;
+ void add( const T& );
+ void remove( int uid );
+ void replace( const T& );
+
+private:
+ QIntCache<Item> m_cache;
+};
+
+// Implementation
+template <class T>
+OPimCacheItem<T>::OPimCacheItem( const T& t )
+ : m_t(t) {
+}
+template <class T>
+OPimCacheItem<T>::~OPimCacheItem() {
+
+}
+template <class T>
+T OPimCacheItem<T>::record()const {
+ return m_t;
+}
+template <class T>
+void OPimCacheItem<T>::setRecord( const T& t ) {
+ m_t = t;
+}
+// Cache
+template <class T>
+OPimCache<T>::OPimCache() {
+ m_cache.setAutoDelete( TRUE );
+}
+template <class T>
+OPimCache<T>::~OPimCache() {
+
+}
+template <class T>
+bool OPimCache<T>::contains(int uid )const {
+ Item* it = m_cache.find( uid, FALSE );
+ if (!it)
+ return false;
+ return true;
+}
+template <class T>
+void OPimCache<T>::invalidate() {
+ m_cache.clear();
+}
+template <class T>
+void OPimCache<T>::setSize( int size ) {
+ m_cache.setMaxCost( size );
+}
+template <class T>
+T OPimCache<T>::find(int uid )const {
+ Item *it = m_cache.find( uid );
+ if (it)
+ return it->record();
+ return T();
+}
+template <class T>
+void OPimCache<T>::add( const T& t ) {
+ Item* it = 0l;
+ it = m_cache.find(t.uid(), FALSE );
+
+ if (it )
+ it->setRecord( t );
+
+ it = new Item( t );
+ if (!m_cache.insert( t.uid(), it ) )
+ delete it;
+}
+template <class T>
+void OPimCache<T>::remove( int uid ) {
+ m_cache.remove( uid );
+}
+template <class T>
+void OPimCache<T>::replace( const T& t) {
+ Item *it = m_cache.find( t.uid() );
+ if ( it ) {
+ it->setRecord( t );
+ }
+}
+
+#endif