public static PreCacheDocumentResponse AddExistingDocument(Uri uri, LEADDocument document) { // Add an existing document to the pre-cache. // This is used in LoadFromUri for those who wish to get the same cached document // from the same URL all the time. // Note we do no special loading of text or images here. // Get the safe hash name from the uri var regionName = GetRegionName(uri); // The PreCacheEntries are what will be cached, based on this map of sizes to documentId values. var sizeIdDictionary = new Dictionary <int, string>(); var maximumImagePixelSizes = DefaultSizes; // The PreCacheResponseItems are what we will return as a confirmation. var responseItems = new PreCacheResponseSizeItem[maximumImagePixelSizes.Length]; for (var index = 0; index < maximumImagePixelSizes.Length; index++) { 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); } else { documentId = document.DocumentId; } responseItems[index] = new PreCacheResponseSizeItem() { Seconds = 0, DocumentId = documentId, MaximumImagePixelSize = size, }; // add to our dictionary for updating the pre-cache all at once sizeIdDictionary.Add(size, documentId); } // Add all the info to the PreCache AddDocumentToPreCache(regionName, uri, sizeIdDictionary); return(new PreCacheDocumentResponse() { Item = new PreCacheResponseItem() { Uri = uri.ToString(), RegionHash = regionName, Items = responseItems, } }); }
public static ReportPreCacheResponse ReportDocuments(ObjectCache cache, bool syncWithCache) { var responseEntries = new List <PreCacheResponseItem>(); var responseRemoved = new List <PreCacheResponseItem>(); Dictionary <string, string> legendMappings = null; lock (LegendLock) { // we'll need the legend file to get the URI back from the regionName. legendMappings = GetLegend(false); } // Note: since we have a set structure, we won't need to check case regionName == null. _preCache.EnumerateRegions(delegate(string regionName) { string uri = null; if (legendMappings.ContainsKey(regionName)) { uri = legendMappings[regionName]; } var entriesSizeItems = new List <PreCacheResponseSizeItem>(); var removedSizeItems = new List <PreCacheResponseSizeItem>(); // within each region, check all entries. _preCache.EnumerateKeys(regionName, delegate(string keyName) { var entry = _preCache.Get <PreCacheEntry>(keyName, regionName); var sizeItem = new PreCacheResponseSizeItem() { DocumentId = entry.documentId, MaximumImagePixelSize = entry.maximumImagePixelSize, Reads = entry.reads }; if (syncWithCache) { // if the cache entry did not exist, add it to the removed items. using (var document = DocumentFactory.LoadFromCache(cache, entry.documentId)) { if (document == null) { removedSizeItems.Add(sizeItem); return; } } } // if !syncWithCache or it was not null entriesSizeItems.Add(sizeItem); }); if (entriesSizeItems.Count > 0) { var entriesItem = new PreCacheResponseItem() { RegionHash = regionName, Uri = uri, Items = entriesSizeItems.ToArray <PreCacheResponseSizeItem>() }; responseEntries.Add(entriesItem); } if (removedSizeItems.Count > 0) { var removedItem = new PreCacheResponseItem() { RegionHash = regionName, Uri = uri, Items = removedSizeItems.ToArray <PreCacheResponseSizeItem>() }; responseRemoved.Add(removedItem); } }); // do the deletion if (responseRemoved.Count > 0) { foreach (var responseItem in responseRemoved) { foreach (var sizeItem in responseItem.Items) { _preCache.DeleteItem(GetKeyName(sizeItem.MaximumImagePixelSize), responseItem.RegionHash); } } _preCache.EnumerateRegions(delegate(string regionName) { CheckDeleteRegion(regionName); }); } return(new ReportPreCacheResponse() { Entries = responseEntries.ToArray <PreCacheResponseItem>(), Removed = responseRemoved.ToArray <PreCacheResponseItem>() }); }
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, } }); }