Options to use with DocumentMemoryCache.
public class DocumentMemoryCacheStartOptions public:ref class DocumentMemoryCacheStartOptions
class DocumentMemoryCacheStartOptions: DocumentMemoryCacheStartOptions is used as the parameter for DocumentMemoryCache.Start and contains the following members:
| Member | Description |
|---|---|
| MinimumLoadDuration | Minimum amount of time the initial loading of a document takes to be considered for memory optimization |
| MaximumItems | Maximum number of items to keep in the cache |
| SlidingExpiration | Amount of time a cache entry can remain in the cache without being touched before being deleted |
| TimerInterval | Interval the timer uses to check for and remove expired items |
Refer to DocumentMemoryCache for more information.
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 usingFileCache cache = new FileCache();cache.PolicySerializationMode = CacheSerializationMode.Json;cache.DataSerializationMode = CacheSerializationMode.Json;cache.CacheDirectory = @"c:\cache-dir";// The document files we are usingstring[] 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 timesLoadAndProcessDocument(documentFiles, cache);// Then process with memory cache// Document memory cache options to usevar documentMemoryCacheStartOptions = new DocumentMemoryCacheStartOptions{// Use for documents that take more than 2 seconds to load initiallyMinimumLoadDuration = TimeSpan.FromSeconds(2),// No maximum limit on the number of cache items to keep in memoryMaximumItems = 0,// Purse items from the cache if not touched for 60 secondsSlidingExpiration = TimeSpan.FromSeconds(60),// Check for expired items every 60 secondsTimerInterval = TimeSpan.FromSeconds(60)};// Use itDocumentFactory.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 fasterLoadAndProcessDocument(documentFiles, cache);// Clean upDocumentFactory.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 timesstopwatch.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 cacheConsole.WriteLine($"Is using memory cache is {DocumentFactory.DocumentMemoryCache.HasDocument(documentIds[i], false)}");// Next call LoadFromCache and process a page in multiple threadsstopwatch.Restart();LoadAllPagesInThreads(documentIds[i], pageCount, cache);elapsed = stopwatch.Elapsed;Console.WriteLine($"Multi-threaded load of all pages took ~{(int)elapsed.TotalSeconds} seconds");}// Clean upDeleteDocumentsFromCache(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 cacheusing (LEADDocument document = DocumentFactory.LoadFromCache(new LoadFromCacheOptions{Cache = cache,DocumentId = documentId})){// Simulates processing of the pageDocumentPage 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,});}}
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 documentMemoryCacheExample() {// The cache we are usingFileCache cache = new FileCache();cache.setPolicySerializationMode(CacheSerializationMode.JSON);cache.setDataSerializationMode(CacheSerializationMode.JSON);cache.setCacheDirectory("c:\\cache-dir");// The document files we are usingString[] documentFiles = {// PDF files are very fast to load and will not use memory cache"C:\\LEADTOOLS23\\Resources\\Images\\leadtools.pdf",// Large Excel files are complex and loading may take some time, they could use// memory cache"C:\\LEADTOOLS23\\Resources\\Images\\large_sheet_5k.xlsx",};// First process without memory cache and obtain some timesloadAndProcessDocument(documentFiles, cache);// Then process with memory cache// Document memory cache options to useDocumentMemoryCacheStartOptions documentMemoryCacheStartOptions = new DocumentMemoryCacheStartOptions();// Use for documents that take more than 2 seconds to load initiallydocumentMemoryCacheStartOptions.setMinimumLoadDuration(2);// No maximum limit on the number of cache items to keep in memorydocumentMemoryCacheStartOptions.setMaximumItems(0);// Purse items from the cache if not touched for 60 secondsdocumentMemoryCacheStartOptions.setSlidingExpiration(60);// Check for expired items every 60 secondsdocumentMemoryCacheStartOptions.setTimerInterval(60);// Use itDocumentFactory.getDocumentMemoryCache().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 fasterloadAndProcessDocument(documentFiles, cache);// Clean upDocumentFactory.getDocumentMemoryCache().stop();}private static void loadAndProcessDocument(String[] documentFiles,ObjectCache cache) {System.out.println("Using memory cache is " +DocumentFactory.getDocumentMemoryCache().isStarted());String[] documentIds = new String[documentFiles.length];long stopwatch = System.currentTimeMillis();long elapsed;for (int i = 0; i < documentFiles.length; i++) {String documentFile = documentFiles[i];int pageCount;// First try without memory cache and obtain some timesstopwatch = System.currentTimeMillis();LoadDocumentOptions loadOptions = new LoadDocumentOptions();loadOptions.setCache(cache);LEADDocument document = DocumentFactory.loadFromFile(documentFile,loadOptions);document.getImages().setMaximumImagePixelSize(2048);document.setAutoSaveToCache(false);document.setAutoDeleteFromCache(false);document.saveToCache();documentIds[i] = document.getDocumentId();pageCount = document.getPages().size();elapsed = (System.currentTimeMillis() - stopwatch) / 1000;System.out.println("Initial load from " + documentFile + " took ~" + elapsed + " seconds");// Check if it's in the cacheSystem.out.println("Is using memory cache is "+ DocumentFactory.getDocumentMemoryCache().hasDocument(documentIds[i],false));// Next call LoadFromCache and process a page in multiple threadsstopwatch = System.currentTimeMillis();loadAllPagesInThreads(documentIds[i], pageCount, cache);elapsed = (System.currentTimeMillis() - stopwatch) / 1000;System.out.println("Multi-threaded load of all pages took ~" + elapsed+ " seconds");}// Clean updeleteDocumentsFromCache(documentIds, cache);}private static void loadAllPagesInThreads(String documentId, int pageCount, ObjectCache cache) {int maxDegreeOfParallelism = 4;ExecutorService executorService = Executors.newFixedThreadPool(maxDegreeOfParallelism);try {// Create a list to hold the Future objects representing the processing of each// pageList<Future<Void>> futures = new ArrayList<>();for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) {final int currentPage = pageNumber;// Submit each page processing task to the executorFuture<Void> future = executorService.submit(new Callable<Void>() {@Overridepublic Void call() throws Exception {// Load the document from the cacheLoadFromCacheOptions options = new LoadFromCacheOptions();options.setCache(cache);options.setDocumentId(documentId);LEADDocument document = DocumentFactory.loadFromCache(options);DocumentPage documentPage = document.getPages().get(currentPage - 1);RasterImage image = documentPage.getImage();return null;}});futures.add(future);}// Wait for all tasks to completefor (Future<Void> future : futures) {future.get();}} catch (Exception e) {e.printStackTrace();} finally {// Shut down the executor serviceexecutorService.shutdown();}}private static void deleteDocumentsFromCache(String[] documentIds,ObjectCache cache) {for (String documentId : documentIds) {LoadFromCacheOptions loadOptions = new LoadFromCacheOptions();loadOptions.setCache(cache);loadOptions.setDocumentId(documentId);DocumentFactory.deleteFromCache(loadOptions);}}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
