diff options
author | crupest <crupest@outlook.com> | 2020-03-13 18:09:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-13 18:09:50 +0800 |
commit | b20c488a9b02807cfa117b10dd8906271af03614 (patch) | |
tree | 23167ac72ad2a0a878eacbfec7fdd9b8b4a81c55 /Timeline/Controllers/UserAvatarController.cs | |
parent | d51954c2eaf07769e045270a02c19e46e01fe73f (diff) | |
parent | 5edffd0242a16e9866a1f4e9f1e1d2ff4e549d2c (diff) | |
download | timeline-b20c488a9b02807cfa117b10dd8906271af03614.tar.gz timeline-b20c488a9b02807cfa117b10dd8906271af03614.tar.bz2 timeline-b20c488a9b02807cfa117b10dd8906271af03614.zip |
Merge pull request #71 from crupest/cache
Add cache support for timeline post data.
Diffstat (limited to 'Timeline/Controllers/UserAvatarController.cs')
-rw-r--r-- | Timeline/Controllers/UserAvatarController.cs | 34 |
1 files changed, 4 insertions, 30 deletions
diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index f4f3db3e..4062837b 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -2,9 +2,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
-using Microsoft.Net.Http.Headers;
using System;
-using System.Linq;
using System.Threading.Tasks;
using Timeline.Auth;
using Timeline.Filters;
@@ -32,7 +30,6 @@ namespace Timeline.Controllers }
[HttpGet("users/{username}/avatar")]
- [ResponseCache(NoStore = false, Location = ResponseCacheLocation.None, Duration = 0)]
public async Task<IActionResult> Get([FromRoute][Username] string username)
{
long id;
@@ -46,34 +43,11 @@ namespace Timeline.Controllers return NotFound(ErrorResponse.UserCommon.NotExist());
}
- const string IfNonMatchHeaderKey = "If-None-Match";
-
- var eTagValue = $"\"{await _service.GetAvatarETag(id)}\"";
- var eTag = new EntityTagHeaderValue(eTagValue);
-
- if (Request.Headers.TryGetValue(IfNonMatchHeaderKey, out var value))
+ return await DataCacheHelper.GenerateActionResult(this, () => _service.GetAvatarETag(id), async () =>
{
- if (!EntityTagHeaderValue.TryParseStrictList(value, out var eTagList))
- {
- _logger.LogInformation(Log.Format(LogGetBadIfNoneMatch,
- ("Username", username), ("If-None-Match", value)));
- return BadRequest(ErrorResponse.Common.Header.IfNonMatch_BadFormat());
- }
-
- if (eTagList.FirstOrDefault(e => e.Equals(eTag)) != null)
- {
- Response.Headers.Add("ETag", eTagValue);
- _logger.LogInformation(Log.Format(LogGetReturnNotModify, ("Username", username)));
- return StatusCode(StatusCodes.Status304NotModified);
- }
- }
-
- var avatarInfo = await _service.GetAvatar(id);
- var avatar = avatarInfo.Avatar;
-
- _logger.LogInformation(Log.Format(LogGetReturnData, ("Username", username)));
- return File(avatar.Data, avatar.Type, new DateTimeOffset(avatarInfo.LastModified), eTag);
-
+ var avatar = await _service.GetAvatar(id);
+ return avatar.ToCacheableData();
+ });
}
[HttpPut("users/{username}/avatar")]
|