more cache updates
authoraidan
Tue, 10 Jan 2012 17:50:20 +0000
changeset 74 a86a8cfe49c8
parent 73 46ccb2af4459
child 75 d647d52028bc
more cache updates
servlib/bundling/BPQCache.cc
servlib/bundling/BPQCache.h
--- a/servlib/bundling/BPQCache.cc	Tue Jan 10 17:03:46 2012 +0000
+++ b/servlib/bundling/BPQCache.cc	Tue Jan 10 17:50:20 2012 +0000
@@ -43,8 +43,7 @@
 	if ( iter == bpq_table_.end() ) {
 		log_debug("no response found in cache, create new cache entry");
 
-		create_cache_entry(bundle, block, key);
-		return true;
+		return create_cache_entry(bundle, block, key);
 
 	} else {
 		log_debug("response found in cache");
@@ -59,8 +58,7 @@
 				log_debug("received bundle is newer than cached one: "
 						  "replace cache entry");
 
-				replace_cache_entry(entry, bundle, block, key);
-				return true;
+				return replace_cache_entry(entry, bundle, block, key);
 
 			} else {
 				log_debug("cached bundle is newer than received one: "
@@ -187,9 +185,16 @@
 
 
 //----------------------------------------------------------------------
-void
+bool
 BPQCache::create_cache_entry(Bundle* bundle, BPQBlock* block, std::string key)
 {
+	if (cache_size_ < bundle->payload().length()) {
+		log_warn("bundle too large to add to cache {cache size: %u, bundle size: %u}",
+				cache_size_, bundle->payload().length());
+
+		return false;
+	}
+
 	if ( bundle->is_fragment() ) {
 		log_debug("creating new cache entry for bundle fragment "
 				  "{key: %s, offset: %u, length: %u}",
@@ -214,22 +219,22 @@
 	bpq_table_[key] = entry;
 	cache_size_ += entry->entry_size();
 	update_lru_keys(key);
+
+	return true;
 }
 
 //----------------------------------------------------------------------
-void
+bool
 BPQCache::replace_cache_entry(BPQCacheEntry* entry, Bundle* bundle,
 							  BPQBlock* block, std::string key)
 {
 	ASSERT ( ! bundle->is_fragment() );
 	log_debug("Remove existing cache entry");
 
-
 	remove_cache_entry(entry, key);
 
-
 	log_debug("Create new cache entry");
-	create_cache_entry(bundle, block, key);
+	return create_cache_entry(bundle, block, key);
 }
 
 //----------------------------------------------------------------------
@@ -259,6 +264,13 @@
 {
 	ASSERT( bundle->is_fragment() );
 
+	if (cache_size_ < bundle->payload().length()) {
+		log_warn("bundle too large to add to cache {cache size: %u, bundle size: %u}",
+				cache_size_, bundle->payload().length());
+
+		return false;
+	}
+
 	log_debug("appending received bundle fragment to cache {offset: %u, length: %u}",
 			  bundle->frag_offset(), bundle->payload().length());
 
@@ -375,6 +387,10 @@
 	while (cache_size_ > BPQCache::max_cache_size_) {
 		std::string lru = lru_keys_.back();
 
+		// don't remove the key that's being updated
+		if (lru == key)
+			break;
+
 		Cache::iterator cache_iter = bpq_table_.find(lru);
 
 		if ( cache_iter != bpq_table_.end() ) {
--- a/servlib/bundling/BPQCache.h	Tue Jan 10 17:03:46 2012 +0000
+++ b/servlib/bundling/BPQCache.h	Tue Jan 10 17:50:20 2012 +0000
@@ -69,14 +69,18 @@
     /**
      * Build a new BPQCcacheEntry from this bundle.
      * Copy the bundle into the fragment list
+     * @return  true if the new cache entry was created
+     *   		false otherwise (eg if bundle is larger than cache)
      */
-    void create_cache_entry(Bundle* bundle, BPQBlock* block, std::string key);
+    bool create_cache_entry(Bundle* bundle, BPQBlock* block, std::string key);
 
     /**
 	 * Remove existing cache entry along with all bundle fragments
 	 * and create a new entry
+     * @return  true if the new cache entry was created
+     *   		false otherwise (eg if bundle is larger than cache)
 	 */
-    void replace_cache_entry(BPQCacheEntry* entry, Bundle* bundle,
+    bool replace_cache_entry(BPQCacheEntry* entry, Bundle* bundle,
     						 BPQBlock* block, std::string key);
 
     void remove_cache_entry(BPQCacheEntry* entry, std::string key);