Free Support Forum - groupdocs.com

FileCache generates files with same name


#1

Hi,

We are building an application where certain documents get requested a lot. Because they want to view them in PDF’s we use GroupDocs to convert any document to PDF.

Right now the conversion is done via a webApi request, which in turn returns the PDF. Because we know most of our docs will be requested a lot, we’d like to enable caching. However here is where things are going wrong.

Here is what happening:

First request for document A:
Document A gets converted and cache folder get’s created.

Second Request for document A:
Document A cached version get’s returned

First request for Document B:
Document A cached version get’s returned

When looking at the cache folder I see files named i.dat so I’m thinking that the cache key that’s being generated isn’t working as expected and thus returning the wrong file

This is the code being used:

/// <summary>
///     Renders a document in PDF form
/// </summary>
/// <param name="document">Stream of the document</param>
/// <param name="documentName">Unique name for file</param>
public static RenderResult RenderDocument(Stream document, string documentName)
{
    try
    {
        // Setup Licensing
        SetConfigurations();
        string filePathFormat = Path.Combine(outputPath, documentName);
        outputPath = GeneratorUtilities.OutputPath;
        string cachePath = Path.Combine(GeneratorUtilities.OutputPath, GeneratorUtilities.CacheFolder);

        FileCache cache = new FileCache(cachePath);
        settings = new ViewerSettings(cache);
        using (Viewer viewer = new Viewer(() => document, settings))
        {
            PdfViewOptions options = new PdfViewOptions(filePathFormat);
            viewer.View(options);

            return new RenderResult(documentName, viewer.GetViewInfo(ViewInfoOptions.ForPngView(false)).Pages.Count);
        }
    }
    catch (Exception e)
    {
        Log.Error("Wasn't able to render pdf's", e);
        throw e;
    }
}

Any solutions on fixing this behaviour?


#2

@Glatomme,

This is expected behavior because the same directory path is passed into FileCache’s constructor regardless of which document is processed. To make FileCache place files into separate directories when processing different files it is required to pass the directory that corresponds to the file. Let’s imagine that we have two files A.docx and B.docx and we want to put cache files into cache/A.docx/ and cache/B.docx/ folders. To do so we instantiate FileCache as new FileCache(“cache/A.docx/”) and new FileCache(“cache/B.docx/”) or new FileCache(“cache”, “A.docx”) and new FileCache(“cache”, “B.docx”). The following code sample shows how to place cache files into separate directories.

string cacheSubFolder = ReplaceInvalidChars(documentName);
FileCache cache = new FileCache(cachePath, cacheSubFolder);
------------------------------------
private static string ReplaceInvalidChars(string filename) => 
            string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));