@@ -27,26 +27,47 @@ private static string _eventsDirectory
2727 get { return _cacheDirectory + "/Events" ; }
2828 }
2929
30- private static string [ ] _cachedSessions => Directory . GetFiles ( _sessionsDirectory , "*" + SESSION_FILE_PREFIX ) ;
31-
32- private static string [ ] _cachedEvents => Directory . GetFiles ( _eventsDirectory , "*" + EVENT_FILE_PREFIX ) ;
33-
3430 private static string _deviceIdFile = _cacheDirectory + "/deviceId.txt" ;
3531
36- private const string SESSION_FILE_PREFIX = ".session" ;
32+ private const string SESSION_FILE_SUFFIX = ".session" ;
3733
38- private const string EVENT_FILE_PREFIX = ".event" ;
34+ private const string EVENT_FILE_SUFFIX = ".event" ;
3935
4036 private const int MAX_CACHED_DAYS = 60 ;
4137
4238
39+
4340 public CacheManager ( Configuration configuration )
4441 {
4542 _configuration = configuration ;
4643 CheckForDirectoryCreation ( ) ;
4744 RemoveExpiredPayloads ( ) ;
4845 }
4946
47+ private string [ ] GetCachedEventFiles ( )
48+ {
49+ return GetFilesBySuffix ( _eventsDirectory , EVENT_FILE_SUFFIX ) ;
50+ }
51+
52+ private string [ ] GetCachedSessionFiles ( )
53+ {
54+
55+ return GetFilesBySuffix ( _sessionsDirectory , SESSION_FILE_SUFFIX ) ;
56+ }
57+
58+ private string [ ] GetFilesBySuffix ( string path , string suffix )
59+ {
60+ try
61+ {
62+ var files = Directory . GetFiles ( path , "*" + suffix ) ;
63+ return files ;
64+ }
65+ catch
66+ {
67+ return new string [ ] { } ;
68+ }
69+ }
70+
5071 public string GetCachedDeviceId ( )
5172 {
5273 try
@@ -73,16 +94,16 @@ public string GetCachedDeviceId()
7394
7495 public void SaveDeviceIdToCache ( string deviceId )
7596 {
76- File . WriteAllText ( _deviceIdFile , deviceId ) ;
97+ WriteFile ( _deviceIdFile , deviceId ) ;
7798 }
7899
79100 private void RemoveExpiredPayloads ( )
80101 {
81- try
102+ var files = GetCachedEventFiles ( ) . ToList ( ) ;
103+ files . AddRange ( GetCachedSessionFiles ( ) ) ;
104+ foreach ( var file in files )
82105 {
83- var files = _cachedEvents . ToList ( ) ;
84- files . AddRange ( _cachedSessions ) ;
85- foreach ( var file in files )
106+ try
86107 {
87108 var creationTime = File . GetCreationTimeUtc ( file ) ;
88109 if ( ( DateTime . UtcNow - creationTime ) . TotalDays > MAX_CACHED_DAYS )
@@ -91,27 +112,27 @@ private void RemoveExpiredPayloads()
91112 File . Delete ( file ) ;
92113 }
93114 }
115+ catch { }
94116 }
95- catch { }
96117 }
97118
98119 public void SaveSessionToCache ( string id , string json )
99120 {
100- var path = _sessionsDirectory + "/" + id + SESSION_FILE_PREFIX ;
121+ var path = _sessionsDirectory + "/" + id + SESSION_FILE_SUFFIX ;
101122 WritePayloadToDisk ( json , path ) ;
102- CheckForMaxCachedPayloads ( _cachedSessions , _configuration . MaxPersistedSessions ) ;
123+ CheckForMaxCachedPayloads ( GetCachedSessionFiles ( ) , _configuration . MaxPersistedSessions ) ;
103124 }
104125
105126 public void SaveEventToCache ( string id , string json )
106127 {
107- var path = _eventsDirectory + "/" + id + EVENT_FILE_PREFIX ;
128+ var path = _eventsDirectory + "/" + id + EVENT_FILE_SUFFIX ;
108129 WritePayloadToDisk ( json , path ) ;
109- CheckForMaxCachedPayloads ( _cachedEvents , _configuration . MaxPersistedEvents ) ;
130+ CheckForMaxCachedPayloads ( GetCachedEventFiles ( ) , _configuration . MaxPersistedEvents ) ;
110131 }
111132
112133 private void WritePayloadToDisk ( string jsonData , string path )
113134 {
114- File . WriteAllText ( path , jsonData ) ;
135+ WriteFile ( path , jsonData ) ;
115136 }
116137
117138 private void CheckForMaxCachedPayloads ( string [ ] payloads , int maxPayloads )
@@ -127,38 +148,56 @@ private void RemoveOldestFiles(string[] filePaths, int numToRemove)
127148 var ordered = filePaths . OrderBy ( file => File . GetCreationTimeUtc ( file ) ) . ToArray ( ) ;
128149 foreach ( var file in ordered . Take ( numToRemove ) )
129150 {
130- File . Delete ( file ) ;
131- }
151+ DeleteFile ( file ) ;
152+ }
132153 }
133154
134155 public void RemoveCachedEvent ( string id )
135156 {
136- foreach ( var cachedEventPath in _cachedEvents )
137- {
138- if ( cachedEventPath . Contains ( id ) )
139- {
140- File . Delete ( cachedEventPath ) ;
141- }
142- }
157+ RemovePayloadWithID ( GetCachedEventFiles ( ) , id ) ;
143158 }
144159
145160 public void RemoveCachedSession ( string id )
146161 {
147- foreach ( var cachedSessionPath in _cachedSessions )
162+ RemovePayloadWithID ( GetCachedSessionFiles ( ) , id ) ;
163+ }
164+
165+ private void RemovePayloadWithID ( string [ ] files , string id )
166+ {
167+ foreach ( var path in files )
148168 {
149- if ( cachedSessionPath . Contains ( id ) )
169+ if ( path . Contains ( id ) )
150170 {
151- File . Delete ( cachedSessionPath ) ;
171+ DeleteFile ( path ) ;
172+ return ;
152173 }
153174 }
154175 }
155176
156- private string GetJsonFromCachePath ( string path )
177+ private void DeleteFile ( string path )
157178 {
158- if ( File . Exists ( path ) )
179+ try
159180 {
160- return File . ReadAllText ( path ) ;
161- }
181+ File . Delete ( path ) ;
182+ } catch { }
183+ }
184+
185+ private void WriteFile ( string path , string data )
186+ {
187+ try
188+ {
189+ File . WriteAllText ( path , data ) ;
190+ } catch { }
191+ }
192+
193+ private string GetJsonFromCachePath ( string path )
194+ {
195+ try {
196+ if ( File . Exists ( path ) )
197+ {
198+ return File . ReadAllText ( path ) ;
199+ }
200+ } catch { }
162201 return null ;
163202 }
164203
@@ -188,29 +227,45 @@ private static void CheckForDirectoryCreation()
188227
189228 public List < string > GetCachedEventIds ( )
190229 {
191- var cachedEventIds = new List < string > ( ) ;
192- var ordered = _cachedEvents . OrderBy ( file => File . GetCreationTimeUtc ( file ) ) . ToArray ( ) ;
230+ return GetPayloadIDsFromDirectory ( GetCachedEventFiles ( ) ) ;
231+ }
232+
233+ public List < string > GetCachedSessionIds ( )
234+ {
235+ return GetPayloadIDsFromDirectory ( GetCachedSessionFiles ( ) ) ;
236+ }
237+
238+ private List < string > GetPayloadIDsFromDirectory ( string [ ] files )
239+ {
240+ var names = new List < string > ( ) ;
241+ var ordered = GetCreationOrderedFiles ( files ) ;
193242 foreach ( var path in ordered )
194243 {
195- cachedEventIds . Add ( Path . GetFileNameWithoutExtension ( path ) ) ;
244+ try
245+ {
246+ names . Add ( Path . GetFileNameWithoutExtension ( path ) ) ;
247+ }
248+ catch { }
196249 }
197- return cachedEventIds ;
250+ return names ;
198251 }
199252
200- public List < string > GetCachedSessionIds ( )
253+ private string [ ] GetCreationOrderedFiles ( string [ ] files )
201254 {
202- var cachedSessionIds = new List < string > ( ) ;
203- var ordered = _cachedSessions . OrderBy ( file => File . GetCreationTimeUtc ( file ) ) . ToArray ( ) ;
204- foreach ( var path in ordered )
255+ try
256+ {
257+ var orderedFiles = files . OrderBy ( file => File . GetCreationTimeUtc ( file ) ) . ToArray ( ) ;
258+ return orderedFiles ;
259+ }
260+ catch
205261 {
206- cachedSessionIds . Add ( Path . GetFileNameWithoutExtension ( path ) ) ;
262+ return new string [ ] { } ;
207263 }
208- return cachedSessionIds ;
209264 }
210265
211266 public string GetCachedEvent ( string id )
212267 {
213- foreach ( var path in _cachedEvents )
268+ foreach ( var path in GetCachedEventFiles ( ) )
214269 {
215270 if ( path . Contains ( id ) )
216271 {
@@ -222,7 +277,7 @@ public string GetCachedEvent(string id)
222277
223278 public string GetCachedSession ( string id )
224279 {
225- foreach ( var path in _cachedSessions )
280+ foreach ( var path in GetCachedSessionFiles ( ) )
226281 {
227282 if ( path . Contains ( id ) )
228283 {
0 commit comments