Skip to content

377947 : How to use the Redis cache #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
342 changes: 342 additions & 0 deletions How to/Redis Cache/Controllers/PdfViewerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using PDFViewerWebService;
using Syncfusion.EJ2.PdfViewer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace RedisCache.Controllers
{
[Route("[controller]")]
[ApiController]
public class PdfViewerController : ControllerBase
{
//Initialize the cache object  
private readonly IHostingEnvironment _hostingEnvironment;
public IMemoryCache _cache;
private IDistributedCache _dCache;
private IConfiguration _configuration;
private int _slidingTime = 0;
string path;
public PdfViewerController(IMemoryCache memoryCache, IHostingEnvironment hostingEnvironment, IDistributedCache cache, IConfiguration configuration)
{
_cache = memoryCache;
_dCache = cache;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
path = _configuration["DOCUMENT_PATH"];
//check the document path environment variable value and assign default data folder
//if it is null.
path = string.IsNullOrEmpty(path) ? Path.Combine(_hostingEnvironment.ContentRootPath, "Data") : Path.Combine(_hostingEnvironment.ContentRootPath, path);
}

[HttpPost("Load")]
[Route("[controller]/Load")]
        //Post action for Loading the PDF documents  
        public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
Console.WriteLine("Load called");
//Initialize the PDF viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
MemoryStream stream = new MemoryStream();
object jsonResult = new object();
if (jsonObject != null && jsonObject.ContainsKey("document"))
{
if (bool.Parse(jsonObject["isFileName"]))
{
string documentPath = GetDocumentPath(jsonObject["document"]);
if (!string.IsNullOrEmpty(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
stream = new MemoryStream(bytes);
}
else
{
string fileName = jsonObject["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0];

if (fileName == "http" || fileName == "https")
{
WebClient WebClient = new WebClient();
byte[] pdfDoc = WebClient.DownloadData(jsonObject["document"]);
stream = new MemoryStream(pdfDoc);
}

else
{
return this.Content(jsonObject["document"] + " is not found");
}
}
}
else
{
byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
stream = new MemoryStream(bytes);
}
}
jsonResult = pdfviewer.Load(stream, jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}

[AcceptVerbs("Post")]
[HttpPost("Bookmarks")]
[Route("[controller]/Bookmarks")]
        //Post action for processing the bookmarks from the PDF documents
        public IActionResult Bookmarks([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
var jsonResult = pdfviewer.GetBookmarks(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}

[AcceptVerbs("Post")]
[HttpPost("RenderPdfPages")]
[Route("[controller]/RenderPdfPages")]
        //Post action for processing the PDF documents 
        public IActionResult RenderPdfPages([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
object jsonResult = pdfviewer.GetPage(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}

[AcceptVerbs("Post")]
[HttpPost("RenderPdfTexts")]
[Route("[controller]/RenderPdfTexts")]
        //Post action for processing the PDF texts 
        public IActionResult RenderPdfTexts([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
object jsonResult = pdfviewer.GetDocumentText(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}

[AcceptVerbs("Post")]
[HttpPost("RenderThumbnailImages")]
[Route("[controller]/RenderThumbnailImages")]
        //Post action for rendering the ThumbnailImages
        public IActionResult RenderThumbnailImages([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
object result = pdfviewer.GetThumbnailImages(jsonObject);
return Content(JsonConvert.SerializeObject(result));
}
[AcceptVerbs("Post")]
[HttpPost("RenderAnnotationComments")]
[Route("[controller]/RenderAnnotationComments")]
        //Post action for rendering the annotations
        public IActionResult RenderAnnotationComments([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
object jsonResult = pdfviewer.GetAnnotationComments(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}
[AcceptVerbs("Post")]
[HttpPost("ExportAnnotations")]
[Route("[controller]/ExportAnnotations")]
        //Post action to export annotations
        public IActionResult ExportAnnotations([FromBody] Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
string jsonResult = pdfviewer.ExportAnnotation(jsonObject);
return Content(jsonResult);
}
[AcceptVerbs("Post")]
[HttpPost("ImportAnnotations")]
[Route("[controller]/ImportAnnotations")]
        //Post action to import annotations
        public IActionResult ImportAnnotations([FromBody] Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
string jsonResult = string.Empty;
object JsonResult;
if (jsonObject != null && jsonObject.ContainsKey("fileName"))
{
string documentPath = GetDocumentPath(jsonObject["fileName"]);
if (!string.IsNullOrEmpty(documentPath))
{
jsonResult = System.IO.File.ReadAllText(documentPath);
}
else
{
return this.Content(jsonObject["document"] + " is not found");
}
}
else
{
string extension = Path.GetExtension(jsonObject["importedData"]);
if (extension != ".xfdf")
{
JsonResult = pdfviewer.ImportAnnotation(jsonObject);
return Content(JsonConvert.SerializeObject(JsonResult));
}
else
{
string documentPath = GetDocumentPath(jsonObject["importedData"]);
if (!string.IsNullOrEmpty(documentPath))
{
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
jsonObject["importedData"] = Convert.ToBase64String(bytes);
JsonResult = pdfviewer.ImportAnnotation(jsonObject);
return Content(JsonConvert.SerializeObject(JsonResult));
}
else
{
return this.Content(jsonObject["document"] + " is not found");
}
}
}
return Content(jsonResult);
}

[AcceptVerbs("Post")]
[HttpPost("ExportFormFields")]
[Route("[controller]/ExportFormFields")]
public IActionResult ExportFormFields([FromBody] Dictionary<string, string> jsonObject)

{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
string jsonResult = pdfviewer.ExportFormFields(jsonObject);
return Content(jsonResult);
}

[AcceptVerbs("Post")]
[HttpPost("ImportFormFields")]
[Route("[controller]/ImportFormFields")]
public IActionResult ImportFormFields([FromBody] Dictionary<string, string> jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
jsonObject["data"] = GetDocumentPath(jsonObject["data"]);
object jsonResult = pdfviewer.ImportFormFields(jsonObject);
return Content(JsonConvert.SerializeObject(jsonResult));
}

[AcceptVerbs("Post")]
[HttpPost("Unload")]
[Route("[controller]/Unload")]
        //Post action for unloading and disposing the PDF document resources 
        public IActionResult Unload([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
pdfviewer.ClearCache(jsonObject);
return this.Content("Document cache is cleared");
}


[HttpPost("Download")]
[Route("[controller]/Download")]
        //Post action for downloading the PDF documents
        public IActionResult Download([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
return Content(documentBase);
}

[HttpPost("PrintImages")]
[Route("[controller]/PrintImages")]
        //Post action for printing the PDF documents
        public IActionResult PrintImages([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
if (Startup.isRedisCacheEnable)
pdfviewer = new PdfRenderer(_cache, _dCache, _slidingTime);
else
pdfviewer = new PdfRenderer(_cache, _slidingTime);
object pageImage = pdfviewer.GetPrintImage(jsonObject);
return Content(JsonConvert.SerializeObject(pageImage));
}

//Gets the path of the PDF document
private string GetDocumentPath(string document)
{
string documentPath = string.Empty;
if (!System.IO.File.Exists(document))
{
var path = _hostingEnvironment.ContentRootPath;
if (System.IO.File.Exists(path + "/Data/" + document))
documentPath = path + "/Data/" + document;
}
else
{
documentPath = document;
}
Console.WriteLine(documentPath);
return documentPath;
}
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
}
}
Binary file added How to/Redis Cache/Data/PDF_Succinctly.pdf
Binary file not shown.
27 changes: 27 additions & 0 deletions How to/Redis Cache/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using PDFViewerWebService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PdfViewerWebService
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Loading