Fix cache update method 98/27598/11
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 16 Sep 2014 10:53:09 +0000 (12:53 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Thu, 16 Oct 2014 08:28:52 +0000 (01:28 -0700)
Support update with existing entry.

Change-Id: I0a242c9580ae0a521fddb8bf4fc4c2b8be3507c0

src/client-common/cache/CapacityCache.cpp

index 04f1cb5..c6449bc 100644 (file)
@@ -119,15 +119,31 @@ int CapacityCache::update(const ClientSession &session,
     PolicyResult storedResult = result;
 
     if (m_capacity > 0) {
+        std::string cacheKey = keyToString(key);
+        auto resultIt = m_keyValue.find(cacheKey);
         if (plugin->isCacheable(session, storedResult)) {
             LOGD("Entry cacheable");
             if (m_keyValue.size() == m_capacity) {
                 LOGD("Capacity reached.");
                 evict();
             }
-            std::string cacheKey = keyToString(key);
-            m_keyUsage.push_front(cacheKey);
+
+            //Move value usage to front
+            if (resultIt != m_keyValue.end()) {
+                auto usageIt = std::get<2>(resultIt->second);
+                m_keyUsage.splice(m_keyUsage.begin(), m_keyUsage, usageIt);
+            } else {
+                m_keyUsage.push_front(cacheKey);
+            }
+
             m_keyValue[cacheKey] = std::make_tuple(storedResult, session, m_keyUsage.begin());
+        } else {
+            //Remove element
+            if (resultIt != m_keyValue.end()) {
+                auto usageIt = std::get<2>(resultIt->second);
+                m_keyUsage.erase(usageIt);
+                m_keyValue.erase(resultIt);
+            }
         }
     }
     return plugin->toResult(session, storedResult);