-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCache.java
More file actions
241 lines (209 loc) · 6.85 KB
/
SimpleCache.java
File metadata and controls
241 lines (209 loc) · 6.85 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Scanner;
/**
* SimpleCache Caching Class Originally published in PHP by: Gilbert Pellegrom
* Webpage: https://github.com/gilbitron/PHP-SimpleCache
*
* Ported to Java by HiddenMotives
*
* Released under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
*/
public final class SimpleCache {
// Cache time is defined in day intervals.
private int cache_time = 1;
// Directory where cached files will be stored
private String cache_path = "cache/";
// Extension of the cached files
private String cache_extension = ".cache";
public SimpleCache() {
this.set_cache_path(cache_path);
}
public int get_cache_time () {
return this.cache_time;
}
public String get_cache_path() {
return this.cache_path;
}
public String get_cache_extension() {
return this.cache_extension;
}
public void set_cache_time(int days) {
this.cache_time = days;
}
public void set_cache_path(String path) {
this.cache_path = path;
if (!(new File(this.cache_path).isDirectory())) {
new File(this.cache_path).mkdirs();
}
}
public void set_cache_extension(String ext) {
this.cache_extension = ext;
}
/**
* Check if a file is cached or not.
* @param label
* @return
*/
public boolean is_cached(String label) {
String filename = String.valueOf(this.cache_path)
+ this.safe_filename(label) + this.cache_extension;
File file = new File(filename);
long diff = new Date().getTime() - file.lastModified();
return file.exists() && (!(diff > (long) this.cache_time * 24 * 60 * 60
* 1000));
}
/**
* Function that allows you to grab the cache from a label.
* @param label
* @return
*/
public String get_cache(String label) {
if (this.is_cached(label)) {
String filename = String.valueOf(this.cache_path)
+ this.safe_filename(label) + this.cache_extension;
String data;
try (Scanner reader = new Scanner(new File(filename)).useDelimiter("\\Z")) {
data = reader.next();
reader.close();
return data;
} catch (FileNotFoundException e) {
return null;
}
}
return null;
}
/**
* Set a cache file using a custom label and predefined data.
* @param label
* @param data
*/
public void set_cache(String label, String data) {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(this.cache_path
+ this.safe_filename(label) + this.cache_extension),
"utf-8"))) {
writer.write(data);
writer.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* Function that tries to cached data, if no cached file is found it will
* try and cache the data from the URL.
*
* @param label Label of the cached file
* @param url
* @return
* @throws MalformedURLException
* @throws IOException
*/
public String get_data(String label, String url)
throws MalformedURLException, IOException {
String data = null;
if (this.get_cache(label) != null) {
data = this.get_cache(label);
return data;
}
data = this.grab_url(url);
this.set_cache(label, data);
return data;
}
/**
* Function that reads and returns the contents of a URL.
* Using the specified user agent and timeout when making the URL request.
* @param url
* @param timeout in milliseconds
* @param userAgent
* @return Contents of the URL.
* @throws MalformedURLException
* @throws IOException
*/
public String grab_url(String url, int timeout, String userAgent)
throws MalformedURLException, IOException {
StringBuilder response = new StringBuilder();
URL website = new URL(url);
URLConnection connection = website.openConnection();
connection.setConnectTimeout(timeout);
connection.setRequestProperty("User-Agent", userAgent);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()))) {
while ((url = in.readLine()) != null) {
response.append(url);
}
in.close();
}
return response.toString();
}
/**
* Function that reads and returns the contents of a URL.
* @param url
* @return Contents of the URL
* @throws MalformedURLException
* @throws IOException
*/
public String grab_url(String url) throws MalformedURLException,
IOException {
StringBuilder response = new StringBuilder();
URL website = new URL(url);
URLConnection connection = website.openConnection();
connection.setConnectTimeout(5000);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()))) {
while ((url = in.readLine()) != null) {
response.append(url);
}
in.close();
}
return response.toString();
}
/**
* Remove all Cached files
*/
public void clearCache() {
for (File file : new File(this.cache_path).listFiles()) {
file.delete();
}
}
public void clearCache(String label) throws FileNotFoundException {
String filename = String.valueOf(this.cache_path)
+ this.safe_filename(label) + this.cache_extension;
File file = new File(filename);
if(file.exists()) {
file.delete();
} else {
throw new FileNotFoundException();
}
}
/**
* Function the number of cached files currently stored
* @return total cached files
*/
public int get_total_cached() {
return new File(this.cache_path).listFiles().length;
}
/**
* Helper function to help validate file names
* @param filename
* @return
*/
private String safe_filename(String filename) {
return filename.replaceAll("/[^0-9a-z\\.\\_\\-]/i", "");
}
}