changeset 507:c4122c3a47c1

access to oldest item in LRUCache
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Aug 2013 10:24:49 +0200
parents f59e4518fd57
children e7841864c97c
files Core/Cache/LeastRecentlyUsedIndex.h UnitTests/MemoryCache.cpp
diffstat 2 files changed, 43 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Cache/LeastRecentlyUsedIndex.h	Fri Aug 16 10:17:45 2013 +0200
+++ b/Core/Cache/LeastRecentlyUsedIndex.h	Fri Aug 16 10:24:49 2013 +0200
@@ -146,6 +146,10 @@
     {
       return index_.empty();
     }
+
+    const T& GetOldest() const;
+    
+    const Payload& GetOldestPayload() const;
   };
 
 
@@ -247,4 +251,28 @@
 
     return oldest;
   }
+
+
+  template <typename T, typename Payload>
+  const T& LeastRecentlyUsedIndex<T, Payload>::GetOldest() const
+  {
+    if (IsEmpty())
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+
+    return queue_.back().first;
+  }
+
+
+  template <typename T, typename Payload>
+  const Payload& LeastRecentlyUsedIndex<T, Payload>::GetOldestPayload() const
+  {
+    if (IsEmpty())
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+
+    return queue_.back().second;
+  }
 }
--- a/UnitTests/MemoryCache.cpp	Fri Aug 16 10:17:45 2013 +0200
+++ b/UnitTests/MemoryCache.cpp	Fri Aug 16 10:24:49 2013 +0200
@@ -24,12 +24,19 @@
   r.TagAsMostRecent("d");
   r.TagAsMostRecent("c");
 
+  ASSERT_EQ("a", r.GetOldest());
   ASSERT_EQ("a", r.RemoveOldest());
+  ASSERT_EQ("b", r.GetOldest());
   ASSERT_EQ("b", r.RemoveOldest());
+  ASSERT_EQ("d", r.GetOldest());
   ASSERT_EQ("d", r.RemoveOldest());
+  ASSERT_EQ("c", r.GetOldest());
   ASSERT_EQ("c", r.RemoveOldest());
 
   ASSERT_TRUE(r.IsEmpty());
+
+  ASSERT_THROW(r.GetOldest(), Orthanc::OrthancException);
+  ASSERT_THROW(r.RemoveOldest(), Orthanc::OrthancException);
 }
 
 
@@ -58,8 +65,16 @@
   ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p);
   ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p);
 
+  ASSERT_EQ("a", r.GetOldest());
+  ASSERT_EQ(420, r.GetOldestPayload());
   ASSERT_EQ("a", r.RemoveOldest(p)); ASSERT_EQ(420, p);
+
+  ASSERT_EQ("d", r.GetOldest());
+  ASSERT_EQ(423, r.GetOldestPayload());
   ASSERT_EQ("d", r.RemoveOldest(p)); ASSERT_EQ(423, p);
+
+  ASSERT_EQ("c", r.GetOldest());
+  ASSERT_EQ(422, r.GetOldestPayload());
   ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p);
 
   ASSERT_TRUE(r.IsEmpty());