How to use custom cache implementation

Despite the fact that GroupDocs.Viewer implements caching to local drive out of the box, it also allows you to cache rendering result in your own way. You can do this by using Cache interface implementation.
Let see how to implement some custom cache implementation

Using Custom cache (example)

The following steps should be followed.

  • Create CustomCache class which implements Cache interface.
  • Instantiate the CustomCache class.
  • Pass the reference of the RedisCache class instance into the constructor of ViewerSettings class instance.
  • Instantiate Viewer class with ViewerSettings object.
  • Call view(…) method of Viewer class.

Below is the code that demonstrates how to use custom caching for GroupDocs.Viewer:

    String outputDirectory = "C:\\output";
    String pageFilePathFormat = Path.combine(outputDirectory, "page_{0}.html");

    MemoryCache cache = new MemoryCache("sample_");
    ViewerSettings settings = new ViewerSettings(cache);

    Viewer viewer = new Viewer("C:\\sample.docx", settings);

    HtmlViewOptions options = HtmlViewOptions.forEmbeddedResources(pageFilePathFormat);
    long stopWatch = System.currentTimeMillis();
    viewer.view(options);
    stopWatch = System.currentTimeMillis() - stopWatch;
    System.out.println("Time taken on first call to View method " + stopWatch + " (ms).");
    stopWatch = System.currentTimeMillis();
    viewer.view(options);
    stopWatch = System.currentTimeMillis() - stopWatch;
    System.out.println("Time taken on second call to View method " + stopWatch + " (ms).");

    System.out.println("\nSource document rendered successfully.\nCheck output in " + outputDirectory + ".");
    viewer.close();
public class MemoryCache implements Cache {
    private final Map<String, Object> data = new HashMap<>();
    private final String mPrefix;

    public MemoryCache(String prefix) {
        this.mPrefix = prefix;
    }

    @Override
    public void set(String key, Object value) {
        data.put(mPrefix + key, value);
    }

    @Override
    public Object get(String key) {
        return data.get(mPrefix + key);
    }

    @Override
    public List<String> getKeys(String filter) {
        List<String> result = new ArrayList<>();
        for (String key : data.keySet()) {
            if (key.contains(filter)) {
                result.add(key);
            }
        }
        return result;
    }
}

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.