aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Helpers/DataCacheHelper.cs
blob: 1ad697081a6192063fef237155068eabe11d5a38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using System;
using System.Linq;
using System.Threading.Tasks;
using Timeline.Models.Http;
using static Timeline.Resources.Helper.DataCacheHelper;

namespace Timeline.Helpers
{
    public interface ICacheableData
    {
        string Type { get; }
#pragma warning disable CA1819 // Properties should not return arrays
        byte[] Data { get; }
#pragma warning restore CA1819 // Properties should not return arrays
        DateTime? LastModified { get; }
    }

    public class CacheableData : ICacheableData
    {
        public CacheableData(string type, byte[] data, DateTime? lastModified)
        {
            Type = type;
            Data = data;
            LastModified = lastModified;
        }

        public string Type { get; set; }
#pragma warning disable CA1819 // Properties should not return arrays
        public byte[] Data { get; set; }
#pragma warning restore CA1819 // Properties should not return arrays
        public DateTime? LastModified { get; set; }
    }

    public interface ICacheableDataProvider
    {
        Task<string> GetDataETag();
        Task<ICacheableData> GetData();
    }

    public class DelegateCacheableDataProvider : ICacheableDataProvider
    {
        private readonly Func<Task<string>> _getDataETagDelegate;
        private readonly Func<Task<ICacheableData>> _getDataDelegate;

        public DelegateCacheableDataProvider(Func<Task<string>> getDataETagDelegate, Func<Task<ICacheableData>> getDataDelegate)
        {
            _getDataETagDelegate = getDataETagDelegate;
            _getDataDelegate = getDataDelegate;
        }

        public Task<ICacheableData> GetData()
        {
            return _getDataDelegate();
        }

        public Task<string> GetDataETag()
        {
            return _getDataETagDelegate();
        }
    }

    public static class DataCacheHelper
    {
        public static async Task<ActionResult> GenerateActionResult(Controller controller, ICacheableDataProvider provider, TimeSpan? maxAge = null)
        {
            const string CacheControlHeaderKey = "Cache-Control";
            const string IfNonMatchHeaderKey = "If-None-Match";
            const string ETagHeaderKey = "ETag";

            string GenerateCacheControlHeaderValue()
            {
                var cacheControlHeader = new CacheControlHeaderValue()
                {
                    NoCache = true,
                    NoStore = false,
                    MaxAge = maxAge ?? TimeSpan.FromDays(14),
                    Private = true,
                    MustRevalidate = true
                };
                return cacheControlHeader.ToString();
            }

            var loggerFactory = controller.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>();
            var logger = loggerFactory.CreateLogger(typeof(DataCacheHelper));

            var eTagValue = await provider.GetDataETag();
            eTagValue = '"' + eTagValue + '"';
            var eTag = new EntityTagHeaderValue(eTagValue);


            if (controller.Request.Headers.TryGetValue(IfNonMatchHeaderKey, out var value))
            {
                if (!EntityTagHeaderValue.TryParseStrictList(value, out var eTagList))
                {
                    logger.LogInformation(Log.Format(LogBadIfNoneMatch, ("Header Value", value)));
                    return controller.BadRequest(ErrorResponse.Common.Header.IfNonMatch_BadFormat());
                }

                if (eTagList.FirstOrDefault(e => e.Equals(eTag)) != null)
                {
                    logger.LogInformation(LogResultNotModified);
                    controller.Response.Headers.Add(ETagHeaderKey, eTagValue);
                    controller.Response.Headers.Add(CacheControlHeaderKey, GenerateCacheControlHeaderValue());

                    return controller.StatusCode(StatusCodes.Status304NotModified, null);
                }
            }

            var data = await provider.GetData();
            logger.LogInformation(LogResultData);
            controller.Response.Headers.Add(CacheControlHeaderKey, GenerateCacheControlHeaderValue());
            return controller.File(data.Data, data.Type, data.LastModified, eTag);
        }

        public static Task<ActionResult> GenerateActionResult(Controller controller, Func<Task<string>> getDataETagDelegate, Func<Task<ICacheableData>> getDataDelegate, TimeSpan? maxAge = null)
        {
            return GenerateActionResult(controller, new DelegateCacheableDataProvider(getDataETagDelegate, getDataDelegate), maxAge);
        }
    }
}