|
6 | 6 | using log4net; |
7 | 7 | using SAM.Core.Extensions; |
8 | 8 |
|
9 | | -namespace SAM.Core.Storage |
| 9 | +namespace SAM.Core.Storage; |
| 10 | + |
| 11 | +// TODO: Add configurable cache expiration |
| 12 | +[DebuggerDisplay("{GetFullPath()}")] |
| 13 | +public class CacheKey : ICacheKey |
10 | 14 | { |
11 | | - // TODO: Add configurable cache expiration |
12 | | - [DebuggerDisplay("{GetFullPath()}")] |
13 | | - public class CacheKey : ICacheKey |
14 | | - { |
15 | | - private const string DEFAULT_EXTENSION = ".json"; |
| 15 | + private const string DEFAULT_EXTENSION = ".json"; |
16 | 16 |
|
17 | | - protected readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.ReflectedType ?? typeof(CacheKey)); |
| 17 | + protected readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.ReflectedType ?? typeof(CacheKey)); |
18 | 18 |
|
19 | | - private string _fullPath; |
| 19 | + private string _fullPath; |
20 | 20 |
|
21 | | - public string Key { get; protected set; } |
22 | | - public string FilePath { get; protected set; } |
23 | | - /// <summary> |
24 | | - /// The maximum number of days the cached item is valid for. By <see langword="default"/> (<see langword="null"/>) the cached item does not expire. |
25 | | - /// </summary> |
26 | | - public uint? DaysValid { get; internal set; } |
27 | | - public bool HasExpiration => DaysValid.HasValue; |
| 21 | + public string Key { get; protected set; } |
| 22 | + public string FilePath { get; protected set; } |
| 23 | + /// <summary> |
| 24 | + /// The maximum number of days the cached item is valid for. By <see langword="default"/> (<see langword="null"/>) the cached item does not expire. |
| 25 | + /// </summary> |
| 26 | + public uint? DaysValid { get; internal set; } |
| 27 | + public bool HasExpiration => DaysValid.HasValue; |
28 | 28 |
|
29 | | - protected CacheKey() |
30 | | - { |
| 29 | + protected CacheKey() |
| 30 | + { |
31 | 31 |
|
32 | | - } |
| 32 | + } |
33 | 33 |
|
34 | | - public CacheKey(object key, CacheKeyType type) |
35 | | - { |
36 | | - if (key == null) throw new ArgumentNullException(nameof(key)); |
| 34 | + public CacheKey(object key, CacheKeyType type) |
| 35 | + { |
| 36 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
37 | 37 |
|
38 | | - SetKey(key); |
| 38 | + SetKey(key); |
39 | 39 |
|
40 | | - if (type == CacheKeyType.App) |
41 | | - { |
42 | | - throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter."); |
43 | | - } |
44 | | - |
45 | | - FilePath = type.GetDescription(); |
| 40 | + if (type == CacheKeyType.App) |
| 41 | + { |
| 42 | + throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter."); |
46 | 43 | } |
| 44 | + |
| 45 | + FilePath = type.GetDescription(); |
| 46 | + } |
47 | 47 |
|
48 | | - public CacheKey(object key, CacheKeyType type, uint daysValid) |
49 | | - { |
50 | | - if (key == null) throw new ArgumentNullException(nameof(key)); |
| 48 | + public CacheKey(object key, CacheKeyType type, uint daysValid) |
| 49 | + { |
| 50 | + if (key == null) throw new ArgumentNullException(nameof(key)); |
51 | 51 |
|
52 | | - SetKey(key); |
| 52 | + SetKey(key); |
53 | 53 |
|
54 | | - if (type == CacheKeyType.App) |
55 | | - { |
56 | | - throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter."); |
57 | | - } |
| 54 | + if (type == CacheKeyType.App) |
| 55 | + { |
| 56 | + throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter."); |
| 57 | + } |
58 | 58 |
|
59 | | - DaysValid = daysValid; |
| 59 | + DaysValid = daysValid; |
60 | 60 |
|
61 | | - FilePath = type.GetDescription(); |
62 | | - } |
| 61 | + FilePath = type.GetDescription(); |
| 62 | + } |
63 | 63 |
|
64 | | - public CacheKey(string fileName, object id, CacheKeyType type = CacheKeyType.Default, CacheKeySubType subType = CacheKeySubType.None) |
65 | | - { |
66 | | - if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentNullException(nameof(fileName)); |
| 64 | + public CacheKey(string fileName, object id, CacheKeyType type = CacheKeyType.Default, CacheKeySubType subType = CacheKeySubType.None) |
| 65 | + { |
| 66 | + if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentNullException(nameof(fileName)); |
67 | 67 |
|
68 | | - SetKey(fileName); |
| 68 | + SetKey(fileName); |
69 | 69 |
|
70 | | - if (type != CacheKeyType.App) |
71 | | - { |
72 | | - throw new NotSupportedException(@$"Only {CacheKeyType.App} cache keys support {nameof(id)}."); |
73 | | - } |
74 | | - |
75 | | - var path = new List<object> |
76 | | - { |
77 | | - type.GetDescription(), |
78 | | - id |
79 | | - }; |
80 | | - |
81 | | - // add subtype to path if set (currently only for app images) |
82 | | - if (subType != CacheKeySubType.None) |
83 | | - { |
84 | | - path.Add(subType.GetDescription()); |
85 | | - } |
86 | | - |
87 | | - FilePath = string.Join(Path.DirectorySeparatorChar, path); |
| 70 | + if (type != CacheKeyType.App) |
| 71 | + { |
| 72 | + throw new NotSupportedException(@$"Only {CacheKeyType.App} cache keys support {nameof(id)}."); |
88 | 73 | } |
89 | | - |
90 | | - public virtual string GetFullPath() |
| 74 | + |
| 75 | + var path = new List<object> |
91 | 76 | { |
92 | | - if (_fullPath != null) return _fullPath; |
| 77 | + type.GetDescription(), |
| 78 | + id |
| 79 | + }; |
93 | 80 |
|
94 | | - return _fullPath = Path.Combine(FilePath, Key); |
| 81 | + // add subtype to path if set (currently only for app images) |
| 82 | + if (subType != CacheKeySubType.None) |
| 83 | + { |
| 84 | + path.Add(subType.GetDescription()); |
95 | 85 | } |
96 | 86 |
|
97 | | - public static bool IsExpired(CacheKey key, string fileName) |
98 | | - { |
99 | | - var fi = new FileInfo(fileName); |
| 87 | + FilePath = string.Join(Path.DirectorySeparatorChar, path); |
| 88 | + } |
100 | 89 |
|
101 | | - return IsExpired(key, fi); |
102 | | - } |
| 90 | + public virtual string GetFullPath() |
| 91 | + { |
| 92 | + if (_fullPath != null) return _fullPath; |
103 | 93 |
|
104 | | - public static bool IsExpired(CacheKey key, FileInfo fi) |
105 | | - { |
106 | | - // if there's no expiration, it's never expired |
107 | | - if (!key.HasExpiration) return false; |
| 94 | + return _fullPath = Path.Combine(FilePath, Key); |
| 95 | + } |
108 | 96 |
|
109 | | - var cacheDate = fi.CreationTime; |
110 | | - var cacheLimit = key.DaysValid!.Value; |
111 | | - var expirationDate =cacheDate.AddDays(cacheLimit); |
| 97 | + public static bool IsExpired(CacheKey key, string fileName) |
| 98 | + { |
| 99 | + var fi = new FileInfo(fileName); |
112 | 100 |
|
113 | | - return DateTime.Now < expirationDate; |
114 | | - } |
| 101 | + return IsExpired(key, fi); |
| 102 | + } |
115 | 103 |
|
116 | | - protected void SetKey(object key) |
117 | | - { |
118 | | - var fileName = key.ToString(); |
| 104 | + public static bool IsExpired(CacheKey key, FileInfo fi) |
| 105 | + { |
| 106 | + // if there's no expiration, it's never expired |
| 107 | + if (!key.HasExpiration) return false; |
| 108 | + |
| 109 | + var cacheDate = fi.CreationTime; |
| 110 | + var cacheLimit = key.DaysValid!.Value; |
| 111 | + var expirationDate =cacheDate.AddDays(cacheLimit); |
119 | 112 |
|
120 | | - var hasExtension = Path.HasExtension(fileName); |
121 | | - if (!hasExtension) |
122 | | - { |
123 | | - fileName = Path.ChangeExtension(fileName, DEFAULT_EXTENSION); |
124 | | - } |
| 113 | + return DateTime.Now < expirationDate; |
| 114 | + } |
| 115 | + |
| 116 | + protected void SetKey(object key) |
| 117 | + { |
| 118 | + var fileName = key.ToString(); |
125 | 119 |
|
126 | | - Key = fileName; |
| 120 | + var hasExtension = Path.HasExtension(fileName); |
| 121 | + if (!hasExtension) |
| 122 | + { |
| 123 | + fileName = Path.ChangeExtension(fileName, DEFAULT_EXTENSION); |
127 | 124 | } |
128 | 125 |
|
| 126 | + Key = fileName; |
129 | 127 | } |
| 128 | + |
130 | 129 | } |
0 commit comments