Cache folder reading


#1

Is it safe for multiple users to read data from same cache folder at the same time?Do we have to worry any lock
when reading the same cache folder at same time from multiple users?.If we dont have to worry about read lock, then
we dont have to create multiple cache folder for each user reading the same document.Does it make sense?

Example of our cache folder is like this

C:\AFTEMP\009\PreviewOtherExports\PreviewingDocumentXmlCache\App_Data_009_14_PreviewDocExport_16282_txt


#3

@philipthomas32,

Thanks for posting your inquiry. For the scenario of reading from the cache when a document is rendered for the second time around by multiple users, please spare us some time to check this at our end. We will keep you updated in this regard.

There can be the case when IOException may occur after multiple users try to render a document (i.e. sample.docx) at the same time. In that case, multiple processes will try to read the document (sample.docx) at the same time. To handle this, you can control the file’s access using FileShare.Read that allows subsequent opening of the file for reading. Following is the sample code to demonstrate this.

using (FileStream stream = File.Open("path-of-the-file", FileMode.Open, FileAccess.Read, FileShare.Read))
{
      List<PageHtml> AllPages = htmlHandler.GetPages(stream);
      // do your required operation on AllPages...
}

#4

@philipthomas32,

GroupDocs.Viewer for .NET is using the lock when reading content from the cache and this lock works among multiple processes so it is unlikely that such situation may happen. Therefore, you do not have to worry about the read lock and you can avoid using the separate cache folder for each user.


#5

thanks.What is an example of code if i do this in two parts.For example in part1 i send a request to server to create cache folder and the files in cache folder.In second part after part 1 is complete i send a request to read data from cache folder.What is an example code that just read cache folder data C:\AFTEMP\009\PreviewOtherExports\PreviewingDocumentXmlCache\App_Data_009_14_PreviewDocExport_16282_txt

By doing in two parts, i dont have to wait on part 1


#6

@philipthomas32,

Thanks for posting your query. In fact, when you call ViewerHandler.GetPages function (with ViewerConfig.UseCache = true) for the first time, it creates the cache folder and the files automatically. When you call ViewerHandler.GetPages for the second time, it simply looks up into the cache and picks up the cached content. Therefore, ViewerHandler.GetPages function will serve you in both of your steps.


#7

Thanks for your fast response and knowledge.

One more question is following

IN part 1 this is how i get cache folder

var pages = _htmlHandler.GetPages(“C:\AFTEMP\009\DocExport\16282.txt”, options);

This will create cache file C:\AFTEMP\009\PreviewOtherExports\PreviewingDocumentXmlCache\App_Data_009_14_PreviewDocExport_16282_txt

Now in part1 i delete orginal source file C:\AFTEMP\009\DocExport\16282.txt

In Part2

In part2 because source file is already deleted in part1 C:\AFTEMP\009\DocExport\16282.txt, how do i read cache data from cache folder C:\AFTEMP\009\PreviewOtherExports\PreviewingDocumentXmlCache\App_Data_009_14_PreviewDocExport_16282_txt

Can i point to cache folder to get pages like following?Question is basically how can i read data just using cache folder reference without original source file location?

var pages = _htmlHandler.GetPages(“C:\AFTEMP\009\PreviewOtherExports\PreviewingDocumentXmlCache\App_Data_009_14_PreviewDocExport_16282_txt”, options);


#8

@philipthomas32,

Thanks for sharing your scenario. If you delete the source file after the first step then you will not be able to get the cached content using GetPages function. In that case, you will have to access the files in the cache folder manually using IO operations. Another possibility is that you save the HTML pages at your specified location in the first step and then access the saved files in the second step. Following code snippet may help you on how to save the HTML pages at the specified location.

  // Create html handler
  ViewerConfig config = new ViewerConfig();
  config.StoragePath = @"D:\storage\";
  config.CachePath = @"D:\storage\cache";
  config.UseCache = true;
  ViewerHtmlHandler htmlHandler = new ViewerHtmlHandler(config);
  string guid = "sample.docx";
  
  HtmlOptions options = new HtmlOptions();
  options.IsResourcesEmbedded = true; 

  List<PageHtml> pages = htmlHandler.GetPages(guid, options);

  var directory = Path.Combine(Path.GetFullPath("D:\\storage\\"), Path.GetFileNameWithoutExtension(guid) + "_" + Path.GetExtension(guid));
  if (!Directory.Exists(directory))
  {
       Directory.CreateDirectory(directory);
  }

  foreach (PageHtml page in pages)
  {  
       // save each HTML page at the disk
       String fname = Path.Combine(directory, Path.GetFileNameWithoutExtension(page.PageNumber + "_" + guid) + ".html");
       System.IO.File.WriteAllText(fname, page.HtmlContent); 
  } 

#9

Thanks.
Before i try this, once cache folder is created , in your above example you used just the name of file instead of actual location of file List pages = htmlHandler.GetPages(guid, options);
Does it mean just with file name(unique file name), it can read data from from already created cache folder?That is what i really wanted


#10

@philipthomas32,

Thanks for your response. The code sample I shared above doesn’t read from the cache. Instead, it simply saves the HTML pages (returned by GetPage function) to a specified location. It may cover your first step. Once the HTML pages are saved to your specified location, you can delete the source file. In your second step, you can simply load the HTML files manually (using your own code) from the specified location and display it in your front end application.

If you want the API to read the files from the cache folder automatically, then it is required that the source file is not removed/deleted. In that case, when you will use the GetPages function second time, the API will pick up the cached files from the cache folder.


#11

Make sense, you are saying i have to pass actual source file.


#12

@philipthomas32,

Yes, exactly. If you want the API to read from the cache using GetPages function, then you will have to avoid removing the source file. Otherwise, you will have to save the resultant HTML files at some place and then read the files from there afterwards.