mirror of
https://github.com/bitwarden/server.git
synced 2024-11-23 12:25:16 +01:00
Use In-memory cache instead of custom file cache.
This commit is contained in:
parent
ea5213698d
commit
753496b95d
@ -1,70 +1,52 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Threading.Tasks;
|
||||
using Icons.Models;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace Icons.Controllers
|
||||
{
|
||||
[Route("[controller]")]
|
||||
public class IconController : Controller
|
||||
{
|
||||
private readonly IHostingEnvironment _hostingEnvironment;
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public IconController(IHostingEnvironment hostingEnvironment)
|
||||
public IconController(IMemoryCache memoryCache)
|
||||
{
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
this._cache = memoryCache;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<FileResult> Get([FromQuery] string domain)
|
||||
public async Task<IActionResult> Get([FromQuery] string domain)
|
||||
{
|
||||
var uri = BuildUrl(domain);
|
||||
var fileName = $"{_hostingEnvironment.ContentRootPath}/cache/{domain}.cache";
|
||||
|
||||
// Attempt to load the icon from the cache.
|
||||
if (FileExists(fileName))
|
||||
Icon icon = await _cache.GetOrCreateAsync(domain, async entry =>
|
||||
{
|
||||
using (Stream stream = System.IO.File.Open(fileName, FileMode.Open))
|
||||
entry.AbsoluteExpiration = DateTime.Now.AddDays(1);
|
||||
|
||||
var httpClient = new HttpClient();
|
||||
var response = await httpClient.GetAsync(uri);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var binaryFormatter = new BinaryFormatter();
|
||||
var icon = (Icon)binaryFormatter.Deserialize(stream);
|
||||
|
||||
if (icon.HasNotExpired())
|
||||
{
|
||||
return new FileContentResult(icon.Image, icon.Format);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var httpClient = new HttpClient();
|
||||
var response = await httpClient.GetAsync(uri);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
throw new Exception("Cannot load the image");
|
||||
}
|
||||
|
||||
// Serialize the icon.
|
||||
using (Stream stream = System.IO.File.Open(fileName, FileMode.Create))
|
||||
{
|
||||
var icon = new Icon(
|
||||
return new Icon(
|
||||
await response.Content.ReadAsByteArrayAsync(),
|
||||
response.Content.Headers.ContentType.MediaType
|
||||
);
|
||||
});
|
||||
|
||||
var binaryFormatter = new BinaryFormatter();
|
||||
binaryFormatter.Serialize(stream, icon);
|
||||
return new FileContentResult(icon.Image, icon.Format);
|
||||
if (icon == null)
|
||||
{
|
||||
return NotFound("Cannot load the icon.");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool FileExists(string fileName)
|
||||
{
|
||||
return System.IO.File.Exists(fileName);
|
||||
return new FileContentResult(icon.Image, icon.Format);
|
||||
}
|
||||
|
||||
private static string BuildUrl(string domain)
|
||||
|
@ -1,31 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Icons.Models
|
||||
{
|
||||
[Serializable]
|
||||
public class Icon
|
||||
{
|
||||
|
||||
public byte[] Image { get; }
|
||||
|
||||
public string Format { get; }
|
||||
|
||||
public DateTime CreatedAt { get; }
|
||||
|
||||
public Icon(byte[] image, string format)
|
||||
{
|
||||
this.Image = image;
|
||||
this.Format = format;
|
||||
this.CreatedAt = DateTime.Now;
|
||||
}
|
||||
|
||||
public bool HasNotExpired()
|
||||
{
|
||||
return CreatedAt > DateTime.Now.AddDays(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ namespace Icons
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddMemoryCache();
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user