it_bus/timed_cache.h

00001 #ifndef _CACHE_TIMED_CACHE_H_
00002 #define _CACHE_TIMED_CACHE_H_
00003 
00004 // @Copyright 2002 IONA Technologies, Plc. All Rights Reserved.
00005 //
00006 // A hashtable that enforces a maximum and a timeout.
00007 //
00008 // Maximum: if adding an element causes the size to grow beyond the
00009 // maximum, the least recently cached values are removed. max==0
00010 // means the cache is unbounded.
00011 //
00012 // Timeout: Each time the cache is searched or updated, all elements
00013 // that have been in the cache for longer than the timeout are removed.
00014 // The timeout is calculated from the time the value was cached,
00015 // not from the last time it was accessed. timeout == 0 disables timeout.
00016 //
00017 // IMPORTANT NOTE: You must use the cache_insert and cache_find functions
00018 // *not* the hashtable insert and find functions in order for the
00019 // cache to work.
00020 
00021 #include <it_dsa/hash_map.h>
00022 #include <it_dsa/list.h>
00023 #include <it_dsa/types.h>
00024 #include <it_oss/time.h>
00025 
00026 template<class Key, class Data, class HashFn, class Equal>
00027 class IT_TimedCache 
00028 {
00029 public:
00030     IT_TimedCache(size_t max=0, size_t timeout=0);
00031     ~IT_TimedCache();
00032     
00033     IT_Bool insert(const Key& key, Data& data);
00034 
00035     Data* find(const Key& key);
00036     const Data* find(const Key& key) const;
00037 
00038     size_t size() const;
00039 
00040     size_t max() const;
00041     void max(size_t count);
00042 
00043     unsigned long timeout() const;
00044     void timeout(unsigned long milliseconds);
00045 
00046 private:
00047     typedef IT_HashMap<Key, Data, HashFn, Equal> HashMap;
00048     typedef IT_TYPENAME HashMap::iterator MapIterator;
00049     typedef IT_Pair<MapIterator, IT_Time> ItemTime;
00050     typedef IT_List<ItemTime> Queue;
00051 
00052     void enforce_max();
00053     void enforce_timeout();
00054     void enforce();             // Both
00055     IT_Bool item_is_stale(IT_TYPENAME Queue::iterator item);
00056 
00057     HashMap m_map;
00058     Queue   m_queue;
00059     size_t  m_max;
00060     size_t  m_timeout;
00061     IT_Long m_timeout_secs;
00062     IT_Long m_timeout_msecs;
00063 
00064     // Not defined prevent accidental use.
00065     IT_TimedCache(const IT_TimedCache&);
00066     IT_Bool operator == (const IT_TimedCache&);
00067 };
00068 
00069 
00070 template<class Key, class Data, class HashFn, class Equal>
00071 inline size_t IT_TimedCache<Key, Data, HashFn, Equal>::max() const
00072 {
00073     return m_max;
00074 }    
00075 
00076 template<class Key, class Data, class HashFn, class Equal>
00077 inline unsigned long IT_TimedCache<Key, Data, HashFn, Equal>::timeout() const
00078 {
00079     return m_timeout;
00080 }    
00081 
00082 template<class Key, class Data, class HashFn, class Equal>
00083 inline size_t IT_TimedCache<Key, Data, HashFn, Equal>::size() const
00084 {
00085     return m_map.size();
00086 }    
00087 
00088 template<class Key, class Data, class HashFn, class Equal>
00089 void IT_TimedCache<Key, Data, HashFn, Equal>::enforce()
00090 {
00091     enforce_timeout();
00092     enforce_max();
00093 }    
00094 
00095 #if IT_SUPPORTS_COMPILE_TIME_INSTANTIATION
00096 #include "timed_cache.cxx"
00097 #endif
00098 
00099 #endif  

Generated on Tue Mar 20 15:27:44 2007 for Artix by  doxygen 1.5.1-p1