From a276fb5ab706a1da188b3abfaaf50e2e24ee3475 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 29 Aug 2019 00:03:48 +0800 Subject: Use cache attribute. --- Timeline.Tests/IntegratedTests/UserAvatarTests.cs | 4 ++++ Timeline/Controllers/UserAvatarController.cs | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Timeline.Tests/IntegratedTests/UserAvatarTests.cs b/Timeline.Tests/IntegratedTests/UserAvatarTests.cs index a98d807e..246125d0 100644 --- a/Timeline.Tests/IntegratedTests/UserAvatarTests.cs +++ b/Timeline.Tests/IntegratedTests/UserAvatarTests.cs @@ -70,6 +70,10 @@ namespace Timeline.Tests.IntegratedTests res.Content.Headers.ContentType.MediaType.Should().Be("image/png"); var body = await res.Content.ReadAsByteArrayAsync(); body.Should().Equal(defaultAvatarData); + var cacheControl = res.Headers.CacheControl; + cacheControl.NoCache.Should().BeTrue(); + cacheControl.NoStore.Should().BeFalse(); + cacheControl.MaxAge.Should().NotBeNull().And.Be(TimeSpan.Zero); eTag = res.Headers.ETag; } diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index 96b43bdb..e7e12d0b 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -61,11 +61,10 @@ namespace Timeline.Controllers [HttpGet("users/{username}/avatar")] [Authorize] + [ResponseCache(NoStore = false, Location = ResponseCacheLocation.None, Duration = 0)] public async Task Get([FromRoute] string username) { const string IfNonMatchHeaderKey = "If-None-Match"; - const string CacheControlHeaderKey = "Cache-Control"; - const string CacheControlHeaderValue = "no-cache, must-revalidate, max-age=1"; try { @@ -79,7 +78,6 @@ namespace Timeline.Controllers if (eTagList.FirstOrDefault(e => e.Equals(eTag)) != null) { - Response.Headers.Add(CacheControlHeaderKey, CacheControlHeaderValue); Response.Headers.Add("ETag", eTagValue); return StatusCode(StatusCodes.Status304NotModified); } @@ -88,7 +86,6 @@ namespace Timeline.Controllers var avatarInfo = await _service.GetAvatar(username); var avatar = avatarInfo.Avatar; - Response.Headers.Add(CacheControlHeaderKey, CacheControlHeaderValue); return File(avatar.Data, avatar.Type, new DateTimeOffset(avatarInfo.LastModified), eTag); } catch (UserNotExistException e) -- cgit v1.2.3