Skip to content

Commit 7016e94

Browse files
Create SimpleCache.java
0 parents  commit 7016e94

File tree

1 file changed

+237
-0
lines changed

1 file changed

+237
-0
lines changed

src/SimpleCache.java

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import java.io.BufferedReader;
2+
import java.io.BufferedWriter;
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.io.OutputStreamWriter;
9+
import java.io.Writer;
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
12+
import java.net.URLConnection;
13+
import java.util.Date;
14+
import java.util.Scanner;
15+
16+
17+
18+
/**
19+
* SimpleCache Caching Class Originally published in PHP by: Gilbert Pellegrom
20+
* Webpage: https://github.com/gilbitron/PHP-SimpleCache
21+
*
22+
* Ported to Java by HiddenMotives
23+
*
24+
* Released under the MIT license.
25+
* http://www.opensource.org/licenses/mit-license.php
26+
*
27+
*/
28+
29+
public class SimpleCache {
30+
31+
// Cache time is defined in day intervals.
32+
private int cache_time = 1;
33+
34+
// Directory where cached files will be stored
35+
private String cache_path = "cache/";
36+
37+
// Extension of the cached files
38+
private String cache_extension = ".cache";
39+
40+
public int get_cache_time () {
41+
return this.cache_time;
42+
}
43+
44+
public String get_cache_path() {
45+
return this.cache_path;
46+
}
47+
48+
public String get_cache_extension() {
49+
return this.cache_extension;
50+
}
51+
52+
public void set_cache_time(int days) {
53+
this.cache_time = days;
54+
}
55+
56+
public void set_cache_path(String path) {
57+
this.cache_path = path;
58+
if (!(new File(this.cache_path).isDirectory())) {
59+
new File(this.cache_path).mkdirs();
60+
}
61+
}
62+
63+
public void set_cache_extension(String ext) {
64+
this.cache_extension = ext;
65+
}
66+
67+
/**
68+
* Check if a file is cached or not.
69+
* @param label
70+
* @return
71+
*/
72+
public boolean is_cached(String label) {
73+
String filename = String.valueOf(this.cache_path)
74+
+ this.safe_filename(label) + this.cache_extension;
75+
File file = new File(filename);
76+
long diff = new Date().getTime() - file.lastModified();
77+
78+
return file.exists() && (!(diff > (long) this.cache_time * 24 * 60 * 60
79+
* 1000));
80+
}
81+
82+
/**
83+
* Function that allows you to grab the cache from a label.
84+
* @param label
85+
* @return
86+
*/
87+
public String get_cache(String label) {
88+
if (this.is_cached(label)) {
89+
String filename = String.valueOf(this.cache_path)
90+
+ this.safe_filename(label) + this.cache_extension;
91+
92+
String data;
93+
try (Scanner reader = new Scanner(new File(filename)).useDelimiter("\\Z")) {
94+
data = reader.next();
95+
reader.close();
96+
return data;
97+
} catch (FileNotFoundException e) {
98+
return null;
99+
}
100+
}
101+
102+
return null;
103+
}
104+
105+
/**
106+
* Set a cache file using a custom label and predefined data.
107+
* @param label
108+
* @param data
109+
*/
110+
public void set_cache(String label, String data) {
111+
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
112+
new FileOutputStream(this.cache_path
113+
+ this.safe_filename(label) + this.cache_extension),
114+
"utf-8"))) {
115+
writer.write(data);
116+
writer.close();
117+
} catch (IOException ex) {
118+
System.out.println(ex.getMessage());
119+
}
120+
}
121+
122+
/**
123+
* Function that tries to cached data, if no cached file is found it will
124+
* try and cache the data from the URL.
125+
*
126+
* @param label Label of the cached file
127+
* @param url
128+
* @return
129+
* @throws MalformedURLException
130+
* @throws IOException
131+
*/
132+
public String get_data(String label, String url)
133+
throws MalformedURLException, IOException {
134+
String data = null;
135+
136+
if (this.get_cache(label) != null) {
137+
data = this.get_cache(label);
138+
return data;
139+
}
140+
141+
data = this.grab_url(url);
142+
this.set_cache(label, data);
143+
return data;
144+
}
145+
146+
/**
147+
* Function that reads and returns the contents of a URL.
148+
* Using the specified user agent and timeout when making the URL request.
149+
* @param url
150+
* @param timeout in milliseconds
151+
* @param userAgent
152+
* @return Contents of the URL.
153+
* @throws MalformedURLException
154+
* @throws IOException
155+
*/
156+
public String grab_url(String url, int timeout, String userAgent)
157+
throws MalformedURLException, IOException {
158+
StringBuilder response = new StringBuilder();
159+
URL website = new URL(url);
160+
URLConnection connection = website.openConnection();
161+
connection.setConnectTimeout(timeout);
162+
connection.setRequestProperty("User-Agent", userAgent);
163+
try (BufferedReader in = new BufferedReader(
164+
new InputStreamReader(
165+
connection.getInputStream()))) {
166+
while ((url = in.readLine()) != null) {
167+
response.append(url);
168+
}
169+
170+
in.close();
171+
}
172+
173+
return response.toString();
174+
}
175+
176+
/**
177+
* Function that reads and returns the contents of a URL.
178+
* @param url
179+
* @return Contents of the URL
180+
* @throws MalformedURLException
181+
* @throws IOException
182+
*/
183+
public String grab_url(String url) throws MalformedURLException,
184+
IOException {
185+
StringBuilder response = new StringBuilder();
186+
URL website = new URL(url);
187+
URLConnection connection = website.openConnection();
188+
connection.setConnectTimeout(5000);
189+
try (BufferedReader in = new BufferedReader(
190+
new InputStreamReader(
191+
connection.getInputStream()))) {
192+
while ((url = in.readLine()) != null) {
193+
response.append(url);
194+
}
195+
196+
in.close();
197+
}
198+
return response.toString();
199+
}
200+
201+
/**
202+
* Remove all Cached files
203+
*/
204+
public void clearCache() {
205+
for (File file : new File(this.cache_path).listFiles()) {
206+
file.delete();
207+
}
208+
}
209+
210+
public void clearCache(String label) throws FileNotFoundException {
211+
String filename = String.valueOf(this.cache_path)
212+
+ this.safe_filename(label) + this.cache_extension;
213+
File file = new File(filename);
214+
if(file.exists()) {
215+
file.delete();
216+
} else {
217+
throw new FileNotFoundException();
218+
}
219+
}
220+
221+
/**
222+
* Function the number of cached files currently stored
223+
* @return total cached files
224+
*/
225+
public int get_total_cached() {
226+
return new File(this.cache_path).listFiles().length;
227+
}
228+
229+
/**
230+
* Helper function to help validate file names
231+
* @param filename
232+
* @return
233+
*/
234+
private String safe_filename(String filename) {
235+
return filename.replaceAll("/[^0-9a-z\\.\\_\\-]/i", "");
236+
}
237+
}

0 commit comments

Comments
 (0)