←Select platform

HasDocument Method

Summary

Indicates whether an entry associated with the specified document exists.

Syntax
C#
C++/CLI
Python
public bool HasDocument( 
   string documentId, 
   bool updateExpiry 
) 
public:  
   bool HasDocument( 
      String^ documentId, 
      bool updateExpiry 
   ) 
def HasDocument(self,documentId,updateExpiry): 

Parameters

documentId

Document ID.

updateExpiry

Touches the item by updating its expiry policy.

Return Value

true if the memory cache has an entry associated with documentId; otherwise, false.

Remarks

If updateExpiry is true and the cache contains an entry associated with documentId, then the item expiry will be extended by another DocumentMemoryCacheStartOptions.SlidingExpiration.

Refer to DocumentMemoryCache for more information.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public void DocumentMemoryCacheExample() 
{ 
   // The cache we are using 
   FileCache cache = new FileCache(); 
   cache.PolicySerializationMode = CacheSerializationMode.Json; 
   cache.DataSerializationMode = CacheSerializationMode.Json; 
   cache.CacheDirectory = @"c:\cache-dir"; 
 
   // The document files we are using 
   string[] documentFiles = 
   { 
      // PDF files are very fast to load and will not use memory cache 
      @"C:\LEADTOOLS22\Resources\Images\leadtools.pdf", 
      // Large Excel files are complex and loading may take some time, they could use memory cache 
      @"C:\LEADTOOLS22\Resources\Images\complex.xlsx", 
   }; 
 
   // First process without memory cache and obtain some times 
   LoadAndProcessDocument(documentFiles, cache); 
 
   // Then process with memory cache 
 
   // Document memory cache options to use 
   var documentMemoryCacheStartOptions = new DocumentMemoryCacheStartOptions 
   { 
      // Use for documents that take more than 2 seconds to load initially 
      MinimumLoadDuration = TimeSpan.FromSeconds(2), 
      // No maximum limit on the number of cache items to keep in memory 
      MaximumItems = 0, 
      // Purse items from the cache if not touched for 60 seconds 
      SlidingExpiration = TimeSpan.FromSeconds(60), 
      // Check for expired items every 60 seconds 
      TimerInterval = TimeSpan.FromSeconds(60) 
   }; 
 
   // Use it 
   DocumentFactory.DocumentMemoryCache.Start(documentMemoryCacheStartOptions); 
 
   // Run again 
   // For the first document, times should be very close to the since this is a PDF document and is very fast (less than MinimumLoadDuration) 
   // For the second document, initial times should be the same, but loading all pages should be much faster 
   LoadAndProcessDocument(documentFiles, cache); 
 
   // Clean up 
   DocumentFactory.DocumentMemoryCache.Stop(); 
} 
 
private static void LoadAndProcessDocument(string[] documentFiles, ObjectCache cache) 
{ 
   Console.WriteLine($"Using memory cache is {DocumentFactory.DocumentMemoryCache.IsStarted}"); 
   string[] documentIds = new string[documentFiles.Length]; 
 
   var stopwatch = new Stopwatch(); 
   TimeSpan elapsed; 
 
   for (var i = 0; i < documentFiles.Length; i++) 
   { 
      string documentFile = documentFiles[i]; 
      int pageCount; 
 
      // First try without memory cache and obtain some times 
      stopwatch.Restart(); 
      using (LEADDocument document = DocumentFactory.LoadFromFile( 
         documentFile, 
         new LoadDocumentOptions 
         { 
            Cache = cache 
         })) 
      { 
         document.Images.MaximumImagePixelSize = 2048; 
         document.AutoSaveToCache = false; 
         document.AutoDeleteFromCache = false; 
         document.SaveToCache(); 
         documentIds[i] = document.DocumentId; 
         pageCount = document.Pages.Count; 
      } 
      elapsed = stopwatch.Elapsed; 
      Console.WriteLine($"Initial load from {Path.GetFileName(documentFile)} took ~{(int)elapsed.TotalSeconds} seconds"); 
      // Check if it's in the cache 
      Console.WriteLine($"Is using memory cache is {DocumentFactory.DocumentMemoryCache.HasDocument(documentIds[i], false)}"); 
 
      // Next call LoadFromCache and process a page in multiple threads 
      stopwatch.Restart(); 
      LoadAllPagesInThreads(documentIds[i], pageCount, cache); 
      elapsed = stopwatch.Elapsed; 
      Console.WriteLine($"Multi-threaded load of all pages took ~{(int)elapsed.TotalSeconds} seconds"); 
   } 
 
   // Clean up 
   DeleteDocumentsFromCache(documentIds, cache); 
} 
 
private static void LoadAllPagesInThreads(string documentId, int pageCount, ObjectCache cache) 
{ 
   System.Threading.Tasks.Parallel.For( 
      1, 
      pageCount + 1, 
      new System.Threading.Tasks.ParallelOptions { MaxDegreeOfParallelism = 4 }, 
      (int pageNumber) => 
      { 
         // Load the document from the cache 
         using (LEADDocument document = DocumentFactory.LoadFromCache( 
            new LoadFromCacheOptions 
            { 
               Cache = cache, 
               DocumentId = documentId 
            })) 
         { 
            // Simulates processing of the page 
            DocumentPage documentPage = document.Pages[pageNumber - 1]; 
            using (RasterImage image = documentPage.GetImage()) 
            { 
 
            } 
         } 
      }); 
} 
 
private static void DeleteDocumentsFromCache(string[] documentIds, ObjectCache cache) 
{ 
   foreach (string documentId in documentIds) 
   { 
      DocumentFactory.DeleteFromCache(new LoadFromCacheOptions 
      { 
         Cache = cache, 
         DocumentId = documentId, 
      }); 
   } 
} 
Requirements

Target Platforms

Help Version 22.0.2023.4.21
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.