private static void AddDocumentToPreCache(string hashRegion, Uri uri, Dictionary <int, string> sizeItems) { // keep it in the pre-cache until deleted. var cachePolicy = ServiceHelper.CreateForeverPolicy(); foreach (KeyValuePair <int, string> dictionaryEntry in sizeItems) { // create our object to place in the cache var cacheEntry = new PreCacheEntry() { maximumImagePixelSize = dictionaryEntry.Key, documentId = dictionaryEntry.Value, reads = 0 }; // use the regionHash as our region name, the size as our key, and the entry as our value. var cacheItem = new CacheItem <PreCacheEntry>(GetKeyName(cacheEntry.maximumImagePixelSize), cacheEntry, hashRegion); _preCache.Add(cacheItem, cachePolicy); } lock (LegendLock) { // Also add this to our "Legend" file that will just hold all the regionHash <==> uri mappings in one place. var legendMappings = GetLegend(true); if (legendMappings.ContainsKey(hashRegion)) { legendMappings.Remove(hashRegion); } legendMappings.Add(hashRegion, uri.ToString()); // Add it back to the cache _preCache.Add(new CacheItem <Dictionary <string, string> >(PreCacheLegendName, legendMappings), ServiceHelper.CreateForeverPolicy()); } }
private static void CheckDeleteRegion(string regionName) { // check if the region is now empty. If so, // 1) delete it // 2) remove it from the "Table of Contents" bool isEmpty = true; _preCache.EnumerateKeys(regionName, delegate(string keyName) { isEmpty = false; }); if (isEmpty) { _preCache.DeleteRegion(regionName); // lock here since we're updating the legend item lock (LegendLock) { // Also delete this from our "Legend" file that holds all the uri <==> regionHash mappings in one place. var legendMappings = GetLegend(false); if (legendMappings == null) { return; } if (legendMappings.ContainsKey(regionName.ToString())) { legendMappings.Remove(regionName.ToString()); } // Add it back to the cache _preCache.Add(new CacheItem <Dictionary <string, string> >(PreCacheLegendName, legendMappings), ServiceHelper.CreateForeverPolicy()); } } }
private static String InternalCheckDocument(string regionName, string keyName) { // Add a lock to the read, since we're changing the cache item and re-saving it. lock (ReadingLock) { var entry = _preCache.Get <PreCacheEntry>(keyName, regionName); if (entry != null) { // Update the "reads" property entry.reads++; _preCache.Add(new CacheItem <PreCacheEntry>(keyName, entry, regionName), ServiceHelper.CreateForeverPolicy()); return(entry.documentId); } else { return(null); } } }
public static PreCacheDocumentResponse AddDocument(ObjectCache cache, PreCacheDocumentRequest request) { var loadOptions = new LoadDocumentOptions(); loadOptions.Cache = cache; loadOptions.UseCache = true; // Get the expiry policy CacheItemPolicy cachePolicy; if (request.ExpiryDate == new DateTime()) { cachePolicy = ServiceHelper.CreateForeverPolicy(); } else { cachePolicy = new CacheItemPolicy() { AbsoluteExpiration = request.ExpiryDate }; } loadOptions.CachePolicy = cachePolicy; // Get the maximum pixel size, if the user did not pass one, use the default values of 4096 and 2048 (used by the DocumentViewerDemo) var maximumImagePixelSizes = request.MaximumImagePixelSizes; if (maximumImagePixelSizes == null || maximumImagePixelSizes.Length == 0) { maximumImagePixelSizes = DefaultSizes; } // Sort the maximum image pixel size from largest to smallest // We will re-use the values from largest to set the smaller images text and SVG since they do // not change Array.Sort( maximumImagePixelSizes, new Comparison <int>((x, y) => y.CompareTo(x))); // Get the safe hash name from the uri var regionName = GetRegionName(request.Uri); // The PreCacheEntries are what will be cached, based on this map of sizes to documentId values. var sizeIdDictionary = new Dictionary <int, string>(); // The PreCacheResponseItems are what we will return as a confirmation. var responseItems = new PreCacheResponseSizeItem[maximumImagePixelSizes.Length]; var ocrEngine = ServiceHelper.GetOCREngine(); // Largest document (to re-use) LEADDocument largestDocument = null; try { // Now load the document and cache it for (var index = 0; index < maximumImagePixelSizes.Length; index++) { // No duplicates if (index > 0 && maximumImagePixelSizes[index] == maximumImagePixelSizes[index - 1]) { continue; } var size = maximumImagePixelSizes[index]; // If it's in the cache, delete it (deletes from PreCache also) string documentId = InternalCheckDocument(regionName, GetKeyName(size)); if (documentId != null) { DocumentHelper.DeleteDocument(documentId, false, false); } // keep track for logging purposes var start = DateTime.Now; // re-use the load options, just change the size loadOptions.MaximumImagePixelSize = size; // Cache the Document var document = AddDocumentToCache(largestDocument, ocrEngine, request.Uri, loadOptions, request.CacheOptions); try { var stop = DateTime.Now; documentId = document.DocumentId; responseItems[index] = new PreCacheResponseSizeItem() { Seconds = Math.Round((stop - start).TotalSeconds, 4), DocumentId = documentId, MaximumImagePixelSize = size, }; // add to our dictionary for updating the pre-cache all at once sizeIdDictionary.Add(size, documentId); } finally { if (largestDocument == null) { largestDocument = document; } else { document.Dispose(); } } } } finally { if (largestDocument != null) { largestDocument.Dispose(); } } // Add all the info to the PreCache AddDocumentToPreCache(regionName, request.Uri, sizeIdDictionary); return(new PreCacheDocumentResponse() { Item = new PreCacheResponseItem() { Uri = request.Uri.ToString(), RegionHash = regionName, Items = responseItems, } }); }