-
Notifications
You must be signed in to change notification settings - Fork 0
/
S3CachingHelper.cs
210 lines (179 loc) · 6.38 KB
/
S3CachingHelper.cs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Text;
using Nop.Core;
using Nop.Core.Domain.Media;
using Nop.Services.Aws;
namespace Nop.Services.Caching.Extension
{
public class S3CachingHelper : IS3CachingHelper
{
#region Field
private readonly IAwsS3Helper _s3Helper;
private readonly MediaSettings _mediaSettings;
#endregion
#region Ctr
public S3CachingHelper(IAwsS3Helper s3Helper, MediaSettings mediaSettings)
{
_s3Helper = s3Helper;
_mediaSettings = mediaSettings;
}
#endregion
#region Cache File (Single)
/// <summary>
/// Write cache
/// </summary>
/// <param name="content"></param>
/// <param name="fileName"></param>
/// <param name="expireInMinutes"></param>
/// <returns></returns>
public bool WriteCache(string content, string fileName, int expireInMinutes = 0)
{
//cache to aws S3
if (_mediaSettings.AWSS3Enable)
{
_s3Helper.S3UploadFile(fileName, Encoding.UTF8.GetBytes(content), "txt", expireInMinutes);
}
//cache to wwwroot
else
{
var filePath = CommonHelper.MapPath($"~/caches/{fileName}.txt");
System.IO.File.WriteAllText(filePath, String.Empty);
System.IO.File.WriteAllText(filePath, content);
}
return true;
}
/// <summary>
/// Get S3 cache content by name
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public string GetCache(string fileName)
{
//get cache data form S3
if (_mediaSettings.AWSS3Enable)
{
if (_s3Helper.S3ExistsFile(fileName))
return _s3Helper.S3GetFile(fileName);
//empty
return string.Empty;
}
//cache from file(wwwroot)
var filePath = CommonHelper.MapPath($"~/caches/{fileName}.txt");
if (System.IO.File.Exists(filePath))
return System.IO.File.ReadAllText(filePath);
//empty
return string.Empty;
}
/// <summary>
/// Remove Cache
/// </summary>
/// <param name="fileName"></param>
public void RemoveCache(string fileName)
{
if (_mediaSettings.AWSS3Enable)
{
_s3Helper.S3DeleteFile(fileName);
}
else
{
//cache form file(wwwroot)
var filePath = CommonHelper.MapPath($"~/caches/{fileName}.txt");
//delete if exists
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
}
}
#endregion
#region File Cache Array
/// <summary>
/// Get S3 cache file content (delete if expires based on S3 object expire header
/// </summary>
/// <param name="identifier"></param>
/// <param name="arrayPath"></param>
/// <param name="content"></param>
/// <param name="deleteIfExpiresAlready"></param>
/// <returns></returns>
public bool TryGetCacheArray(int identifier, string arrayPath, out string content, bool deleteIfExpiresAlready = false)
{
var fileName = $"{arrayPath}/{identifier}";
content = string.Empty;
//get cache data form S3
if (_mediaSettings.AWSS3Enable)
{
if (_s3Helper.S3ExistsFile(fileName))
{
content = _s3Helper.S3GetFile(fileName, deleteIfExpiresAlready);
return true;
}
}
else
{
//cache form file(wwwroot)
var filePath = CommonHelper.MapPath($"~/caches/{fileName}.txt");
if (System.IO.File.Exists(filePath))
{
content = System.IO.File.ReadAllText(filePath);
return true;
}
}
return false;
}
/// <summary>
/// Write cache
/// </summary>
/// <param name="identifier"></param>
/// <param name="arrayPath"></param>
/// <param name="content"></param>
/// <param name="expireInMinutes"></param>
/// <returns></returns>
public bool TryWriteCacheArray(int identifier, string arrayPath, string content, int expireInMinutes = 0)
{
var fileName = $"{arrayPath}/{identifier}";
//cache to aws S3
if (_mediaSettings.AWSS3Enable)
{
_s3Helper.S3UploadFile(fileName, Encoding.UTF8.GetBytes(content), "txt", expireInMinutes);
}
//cache to wwwroot
else
{
var filePath = CommonHelper.MapPath($"~/caches/{fileName}.txt");
System.IO.File.WriteAllText(filePath, String.Empty);
System.IO.File.WriteAllText(filePath, content);
}
return true;
}
/// <summary>
/// Remove S3 Cache file array
/// </summary>
/// <param name="identifier"></param>
/// <param name="arrayPath"></param>
public bool TryRemoveCacheArray(int identifier, string arrayPath)
{
var fileName = $"{arrayPath}/{identifier}";
if (_mediaSettings.AWSS3Enable)
{
if (_s3Helper.S3ExistsFile(fileName))
{
_s3Helper.S3DeleteFile(fileName);
return true;
}
}
else
{
//cache form file(wwwroot)
var filePath = CommonHelper.MapPath($"~/caches/{fileName}.txt");
//delete if exists
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
return true;
}
}
return true;
}
#endregion
}
}