00001 #ifndef _CACHE_TIMED_CACHE_H_
00002 #define _CACHE_TIMED_CACHE_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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();
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
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