[blam] Use a cache for the items



commit a76e1bcc65bbd29a3d8ce1487c9a78fcd817de8d
Author: Carlos MartÃn Nieto <carlos cmartin tk>
Date:   Tue Aug 23 20:11:15 2011 +0200

    Use a cache for the items
    
    Signed-off-by: Carlos MartÃn Nieto <carlos cmartin tk>

 src/ItemStore.cs |   30 ++++++++++++++++--------------
 1 files changed, 16 insertions(+), 14 deletions(-)
---
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index addb362..5845fba 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -16,7 +16,7 @@ namespace Imendio.Blam
         static ItemStore instance = null;
         static string itemfile = Defines.APP_HOMEDIR + "/" + Defines.APP_ITEMSTORE_FILE;
         static string itemfile_tmp = Defines.APP_HOMEDIR + "/" + Defines.APP_ITEMSTORE_FILE + ".tmp";
-        private static object db_lock = new object();
+        private static object cache_lock = new object();
 
         public static Item Get(string id)
         {
@@ -28,22 +28,24 @@ namespace Imendio.Blam
 			if(instance == null)
 				Load();
 
-			if(instance.cache.ContainsKey(id))
-				return instance.cache[id] as Item;
+			lock(cache_lock){
+				if(instance.cache.ContainsKey(id))
+					return instance.cache[id] as Item;
 
-			IList<Item> items = instance.db.Query<Item>(delegate(Item item) {
-				return item.Id == id;
-			});
+				IList<Item> items = instance.db.Query<Item>(delegate(Item item) {
+					return item.Id == id;
+				});
 
-			if(items.Count > 1){
-				Console.Error.WriteLine("There is more than one {0} in the DB", id);
-			}
+				if(items.Count > 1){
+					Console.Error.WriteLine("There is more than one {0} in the DB", id);
+				}
 
-			if(items.Count > 0){
-				instance.cache.Add(id, items[0]);
-				return items[0];
-			} else {
-				return null;
+				if(items.Count > 0){
+					instance.cache.Add(id, items[0]);
+					return items[0];
+				} else {
+					return null;
+				}
 			}
         }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]