From 557b096714189946485454eaa3a01c0473aaed15 Mon Sep 17 00:00:00 2001 From: psychon Date: Sat, 11 Apr 2009 19:24:55 +0000 Subject: [PATCH] Turn TCacheMap into more of a map This adds a second template parameter to TCacheMap which is the value which is saved in the map. This second parameter can be retrived with the new TCacheMap::GetItem() and set with a new second argument to TCacheMap::AddItem() git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1487 726aef4b-f618-498e-8847-2d620e286838 --- Utils.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/Utils.h b/Utils.h index f140597f..e781294e 100644 --- a/Utils.h +++ b/Utils.h @@ -21,6 +21,7 @@ using std::map; using std::vector; +using std::pair; using std::cout; using std::endl; @@ -167,7 +168,7 @@ private: * @author prozac * @brief Insert an object with a time-to-live and check later if it still exists */ -template +template class TCacheMap { public: TCacheMap(unsigned int uTTL = 5000) { @@ -180,7 +181,7 @@ public: * @brief This function adds an item to the cache using the default time-to-live value * @param Item the item to add to the cache */ - void AddItem(const T& Item) { + void AddItem(const K& Item) { AddItem(Item, m_uTTL); } @@ -189,13 +190,38 @@ public: * @param Item the item to add to the cache * @param uTTL the time-to-live for this specific item */ - void AddItem(const T& Item, unsigned int uTTL) { + void AddItem(const K& Item, unsigned int uTTL) { if (!uTTL) { // If time-to-live is zero we don't want to waste our time adding it RemItem(Item); // Remove the item incase it already exists return; } - m_mItems[Item] = CUtils::GetMillTime() + uTTL; + m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, V()); + Cleanup(); + } + + /** + * @brief This function adds an item to the cache using the default time-to-live value + * @param Item the item to add to the cache + * @param Val The value associated with the key Item + */ + void AddItem(const K& Item, const V& Val) { + AddItem(Item, Val, m_uTTL); + } + + /** + * @brief This function adds an item to the cache using a custom time-to-live value + * @param Item the item to add to the cache + * @param Val The value associated with the key Item + * @param uTTL the time-to-live for this specific item + */ + void AddItem(const K& Item, const V& Val, unsigned int uTTL) { + if (!uTTL) { // If time-to-live is zero we don't want to waste our time adding it + RemItem(Item); // Remove the item incase it already exists + return; + } + + m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val); Cleanup(); } @@ -204,17 +230,30 @@ public: * @param Item The item to check for * @return true if item exists */ - bool HasItem(const T& Item) { + bool HasItem(const K& Item) { Cleanup(); return (m_mItems.find(Item) != m_mItems.end()); } + /** + * @brief Performs a Cleanup() and returns a pointer to the object, or NULL + * @param Item The item to check for + * @return Pointer to the item or NULL if there is no suitable one + */ + V* GetItem(const K& Item) { + Cleanup(); + iterator it = m_mItems.find(Item); + if (it == m_mItems.end()) + return NULL; + return &it->second.second; + } + /** * @brief Removes a specific item from the cache * @param Item The item to be removed * @return true if item existed and was removed, false if it never existed */ - bool RemItem(const T& Item) { + bool RemItem(const K& Item) { return m_mItems.erase(Item); } @@ -222,10 +261,10 @@ public: * @brief Cycles through the queue removing all of the stale entries */ void Cleanup() { - typename map::iterator it = m_mItems.begin(); + iterator it = m_mItems.begin(); while (it != m_mItems.end()) { - if (CUtils::GetMillTime() > (it->second)) { + if (CUtils::GetMillTime() > (it->second.first)) { m_mItems.erase(it++); } else { ++it; @@ -237,7 +276,9 @@ public: void SetTTL(unsigned int u) { m_uTTL = u; } // !Setters private: - map m_mItems; //!< Map of cached items. The value portion of the map is for the expire time + typedef pair value; + typedef typename map::iterator iterator; + map m_mItems; //!< Map of cached items. The value portion of the map is for the expire time unsigned int m_uTTL; //!< Default time-to-live duration };