←Select platform

IsDocumentInCache Method

Summary

Helper method to quickly determine if a document exists in the cache.

Syntax
C#
C++/CLI
Python
public static bool IsDocumentInCache( 
   ObjectCache cache, 
   string documentId 
) 
public:  
   static bool IsDocumentInCache( 
      ObjectCache^ cache, 
      String^ documentId 
   ) 
def IsDocumentInCache(self,cache,documentId): 

Parameters

cache

Cache to check. This value cannot be null.

documentId

The ID of the document to check. This value cannot be null and can be either a document ID or the URL to an uploaded LEAD document (leaddocument://).

Return Value

true if the document exists in the cache; otherwise, false.

Remarks

This method uses the information described in Document Toolkit and Caching to quickly determine if the document exists in the cache.

Example
C#
Java
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 IsDocumentInCacheExample() 
{ 
   // Create a cache 
   FileCache cache = new FileCache(); 
   cache.CacheDirectory = @"c:\cache-dir"; 
 
   // We will use a custom document ID 
   string documentId = "my-document"; 
 
   // Ensure the document is not in the cache 
   Debug.Assert(!DocumentFactory.IsDocumentInCache(cache, documentId)); 
 
   // Upload the into the document 
   Uri documentUri = DocumentFactory.BeginUpload(new UploadDocumentOptions 
   { 
      Cache = cache, 
      DocumentId = documentId, 
   }); 
 
   byte[] data = System.IO.File.ReadAllBytes(@"C:\LEADTOOLS22\Resources\Images\leadtools.pdf"); 
   DocumentFactory.UploadDocument(cache, documentUri, data, 0, data.Length); 
   DocumentFactory.EndUpload(cache, documentUri); 
 
   // Now ensure the document is in the cache, using the ID and the URI 
   Debug.Assert(DocumentFactory.IsDocumentInCache(cache, documentId)); 
   Debug.Assert(DocumentFactory.IsDocumentInCache(cache, documentUri.ToString())); 
 
   // Load the document from the cache 
   using (LEADDocument document = DocumentFactory.LoadFromUri(documentUri, 
      new LoadDocumentOptions 
      { 
         Cache = cache, 
      })) 
   { 
      Debug.Assert(document != null); 
      document.AutoDeleteFromCache = false; 
      document.AutoSaveToCache = false; 
      document.SaveToCache(); 
   } 
 
   // Ensure the document is still in the cache, using the ID and the URI 
   Debug.Assert(DocumentFactory.IsDocumentInCache(cache, documentId)); 
   Debug.Assert(DocumentFactory.IsDocumentInCache(cache, documentUri.ToString())); 
 
   // Delete the document from the cache 
   bool isDeleted = DocumentFactory.DeleteFromCache(new LoadFromCacheOptions 
   { 
      Cache = cache, 
      DocumentId = documentId 
   }); 
   Debug.Assert(isDeleted); 
 
   // Ensure the document is not in the cache 
   Debug.Assert(!DocumentFactory.IsDocumentInCache(cache, documentId)); 
   Debug.Assert(!DocumentFactory.IsDocumentInCache(cache, documentUri.ToString())); 
} 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
import java.util.regex.Pattern; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.annotations.engine.*; 
import leadtools.barcode.*; 
import leadtools.caching.*; 
import leadtools.codecs.*; 
import leadtools.document.*; 
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void isDocumentInCacheExample() throws IOException { 
 
   // Create a cache 
   FileCache cache = new FileCache(); 
   cache.setCacheDirectory("c:\\cache-dir"); 
 
   // We will use a custom document ID 
   String documentId = "my-document"; 
 
   // Ensure the document is not in the cache 
   assertTrue(!DocumentFactory.isDocumentInCache(cache, documentId)); 
 
   // Upload the into the document 
   UploadDocumentOptions uploadOptions = new UploadDocumentOptions(); 
   uploadOptions.setCache(cache); 
   uploadOptions.setDocumentId(documentId); 
   URI documentUri = DocumentFactory.beginUpload(uploadOptions); 
 
   byte[] data = Files.readAllBytes(Paths.get("C:\\LEADTOOLS23\\Resources\\Images\\leadtools.pdf")); 
   DocumentFactory.uploadDocument(cache, documentUri, data, 0, data.length); 
   DocumentFactory.endUpload(cache, documentUri); 
 
   // Now ensure the document is in the cache, using the ID and the URI 
   assertTrue(DocumentFactory.isDocumentInCache(cache, documentId)); 
   assertTrue(DocumentFactory.isDocumentInCache(cache, documentUri.toString())); 
 
   // Load the document from the cache 
   LoadDocumentOptions loadOptions = new LoadDocumentOptions(); 
   loadOptions.setCache(cache); 
   LEADDocument document = DocumentFactory.loadFromUri(documentUri, loadOptions); 
 
   assertTrue(document != null); 
   document.setAutoDeleteFromCache(false); 
   document.setAutoSaveToCache(false); 
   document.saveToCache(); 
 
   // Ensure the document is still in the cache, using the ID and the URI 
   assertTrue(DocumentFactory.isDocumentInCache(cache, documentId)); 
   assertTrue(DocumentFactory.isDocumentInCache(cache, documentUri.toString())); 
 
   // Delete the document from the cache 
   LoadFromCacheOptions loadCacheOptions = new LoadFromCacheOptions(); 
   loadCacheOptions.setCache(cache); 
   loadCacheOptions.setDocumentId(documentId); 
   boolean isDeleted = DocumentFactory.deleteFromCache(loadCacheOptions); 
   assertTrue(isDeleted); 
 
   // Ensure the document is not in the cache 
   assertTrue(!DocumentFactory.isDocumentInCache(cache, documentId)); 
   assertTrue(!DocumentFactory.isDocumentInCache(cache, documentUri.toString())); 
 
} 
Requirements

Target Platforms

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

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