private static bool LoadCache() { try { if (InitializeCache()) { Console.WriteLine("Loading products into " + _cache.ToString() + "..."); Hashtable keyVals = _db.LoadProducts(); String[] keys = new String[keyVals.Keys.Count]; CacheItem[] items = new CacheItem[keyVals.Keys.Count]; IDictionaryEnumerator ide = keyVals.GetEnumerator(); int i = 0; while (ide.MoveNext()) { keys[i] = ide.Key.ToString(); items[i++] = new CacheItem(ide.Value); } _cache.AddBulk(keys, items); } else { return(false); } } catch (Exception ex) { Console.WriteLine("Exception : " + ex.Message); return(false); } return(true); }
public override IDictionary AddBulk(string[] keys, CacheItem[] items) { IDictionary iDict = null; string exceptionMessage = null; try { iDict = _webCache.AddBulk(keys, items); } catch (Exception e) { exceptionMessage = e.Message; throw; } finally { try { if (_debugConfigurations.IsInLoggingInterval()) { APILogItem logItem = new APILogItem(); logItem.Signature = "AddBulk(string[] keys, CacheItem[] items)"; logItem.NoOfKeys = keys.Length; logItem.ExceptionMessage = exceptionMessage; logItem.RuntimeAPILogItem = (RuntimeAPILogItem)_webCache.APILogHashTable[System.Threading.Thread.CurrentThread.ManagedThreadId]; _apiLogger.Log(logItem); } } catch (Exception) { } _webCache.APILogHashTable.Remove(System.Threading.Thread.CurrentThread.ManagedThreadId); } return(iDict); }
/// <summary> /// This method Adds objects to cache /// </summary> /// <returns>List of keys added to cache </returns> private static string[] AddDataIntoCache() { List <Product> products = LoadProductsFromDatabase(); CacheItem[] items = new CacheItem[products.Count]; string[] keys = new String[products.Count]; for (int i = 0; i < products.Count; i++) { keys[i] = products[i].Id.ToString(); CacheItem item = new CacheItem(products[i]); item.Dependency = new CacheDependency(new Dependency(products[i].Id, _connectionString)); items[i] = item; } _cache.AddBulk(keys, items, DSWriteOption.None, null); // Print output on console Console.WriteLine("Items are added to Cache."); return(keys); }
/// <summary> /// This method adds multiple objects in the cache using bulk api /// </summary> /// <param name="keys"> List of string keys that will be inserted in the cache </param> /// <param name="products"> products that will be inserted in the cache </param> private static void AddMultipleObjectsToCache(string[] keys, Product[] products) { // Adding Bulk Items // Bulk operation returns IDictionary result = _cache.AddBulk(keys, new CacheItem[] { new CacheItem(products[0]), new CacheItem(products[1]), new CacheItem(products[2]), new CacheItem(products[3]) }); if (result.Count == 0) { // Print output on console Console.WriteLine("\nAll items are successfully added to cache."); } else { // Print output on console Console.WriteLine("\nOne or more items could not be added to the cache. Iterate hashmap for details."); // Iterate hashmap for (IEnumerator iter = result.Values.GetEnumerator(); iter.MoveNext();) { Product product = (Product)iter; PrintProductDetails(product); } } }