squid更新缓存
1.在squid.conf文件里添加
acl Purge method PURGE acl Localhost src 192.168.1.1/10 //你的前端服务器IP地址,如Nginx服务器IP http_access allow Purge Localhost http_access deny Purge
2.squid.properties
servers=192.168.1.1:3128
3.PurgeManager.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.Properties; public class PurgeManager { String[] servers = {}; private static PurgeManager pm = null; public synchronized static PurgeManager getInstance() { if (pm == null) { pm = new PurgeManager(); } return pm; } private PurgeManager() { InputStream inputStream = this.getClass().getResourceAsStream("squid.properties"); Properties p = new Properties(); try { p.load(inputStream); } catch (IOException e1) { e1.printStackTrace(); } String ipstr = p.getProperty("servers"); System.out.println("ipstr=========" + ipstr); if (ipstr != null) { servers = ipstr.split(";"); } try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public String purgeSquid(String url) { String msg = ""; System.out.println("PURGE " + url); for (int i = 0; i < servers.length; i++) { String t[] = servers[i].split(":"); if (t.length == 1) doOneSquid(t[0], url); else doOneSquid(t[0], url, Integer.parseInt(t[1])); } return msg; } private String doOneSquid(String serverip, String purl) { return doOneSquid(serverip, purl, 80); } private String doOneSquid(String serverip, String purl, int port) { String message = ""; // 获取hostname String hname = parseHostNameFromUrl(purl); try { Socket s = new Socket(serverip, port); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); PrintWriter out = new PrintWriter(new OutputStreamWriter(os), true); String state = "PURGE " + purl + " HTTP/1.1"; out.println(state); out.println("HOST:" + hname); out.println(); String downstr; while ((downstr = in.readLine()) != null) { System.out.println(downstr); message += downstr + "<br>"; } s.close(); } catch (Exception e) { e.printStackTrace(); message += "<br>操作失败!"; } return message; } private String parseHostNameFromUrl(String purl) { if (purl == null) return ""; int idx1 = 0; int idx2 = 0; if (purl.toLowerCase().startsWith("http://")) { idx1 = 7; } idx2 = purl.indexOf("/", idx1); if (idx2 == -1) idx2 = purl.length(); return purl.substring(idx1, idx2); } public static void main(String[] args) { //System.out.println(PurgeManager.getInstance(). //parseHostNameFromUrl("db.com")); PurgeManager.getInstance().purgeSquid(http://192.168.1.1:3128/index.html); } }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25