mirror of
https://github.com/bitwarden/server.git
synced 2025-05-29 23:34:53 -05: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;
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Runtime.Serialization.Formatters.Binary;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Icons.Models;
|
using Icons.Models;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
namespace Icons.Controllers
|
namespace Icons.Controllers
|
||||||
{
|
{
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class IconController : 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]
|
[HttpGet]
|
||||||
public async Task<FileResult> Get([FromQuery] string domain)
|
public async Task<IActionResult> Get([FromQuery] string domain)
|
||||||
{
|
{
|
||||||
var uri = BuildUrl(domain);
|
var uri = BuildUrl(domain);
|
||||||
var fileName = $"{_hostingEnvironment.ContentRootPath}/cache/{domain}.cache";
|
|
||||||
|
|
||||||
// Attempt to load the icon from the cache.
|
Icon icon = await _cache.GetOrCreateAsync(domain, async entry =>
|
||||||
if (FileExists(fileName))
|
|
||||||
{
|
{
|
||||||
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();
|
return null;
|
||||||
var icon = (Icon)binaryFormatter.Deserialize(stream);
|
|
||||||
|
|
||||||
if (icon.HasNotExpired())
|
|
||||||
{
|
|
||||||
return new FileContentResult(icon.Image, icon.Format);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var httpClient = new HttpClient();
|
return new Icon(
|
||||||
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(
|
|
||||||
await response.Content.ReadAsByteArrayAsync(),
|
await response.Content.ReadAsByteArrayAsync(),
|
||||||
response.Content.Headers.ContentType.MediaType
|
response.Content.Headers.ContentType.MediaType
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
var binaryFormatter = new BinaryFormatter();
|
if (icon == null)
|
||||||
binaryFormatter.Serialize(stream, icon);
|
{
|
||||||
return new FileContentResult(icon.Image, icon.Format);
|
return NotFound("Cannot load the icon.");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static bool FileExists(string fileName)
|
return new FileContentResult(icon.Image, icon.Format);
|
||||||
{
|
|
||||||
return System.IO.File.Exists(fileName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string BuildUrl(string domain)
|
private static string BuildUrl(string domain)
|
||||||
|
@ -1,31 +1,18 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.Serialization;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Icons.Models
|
namespace Icons.Models
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class Icon
|
public class Icon
|
||||||
{
|
{
|
||||||
|
|
||||||
public byte[] Image { get; }
|
public byte[] Image { get; }
|
||||||
|
|
||||||
public string Format { get; }
|
public string Format { get; }
|
||||||
|
|
||||||
public DateTime CreatedAt { get; }
|
|
||||||
|
|
||||||
public Icon(byte[] image, string format)
|
public Icon(byte[] image, string format)
|
||||||
{
|
{
|
||||||
this.Image = image;
|
this.Image = image;
|
||||||
this.Format = format;
|
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.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
services.AddMemoryCache();
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user