1, deployment Oscache to the Web project, this case download Oscache version is Oscache-2.4.1-full.zip,
Steps:
1 Oscache-2.4.1.jar, Commons-logging.jar, Jgroups-all.jar package in the project Web-inf/lib directory
2 The Oscache.properties file into the project's SRC directory
3) Modify the project Web-inf/web.xml file, add the following content
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class> com.opensymphony.oscache.web.filter.cachefilter</filter-class>
<init-param>
< Description> cache effective time, in seconds, set for 7 days </description>
<param-name>time</param-name>
< param-value>604800</param-value>
</init-param>
<init-param>
<description > Cache range, can only be session or application</description>
<param-name>scope</param-name>
< param-value>application</param-value>
</init-param>
</filter>
< filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/oscache/* </url-pattern>
</filter-mapping>
2, in fact, after the deployment of step 1, you can cache the pages under the/oscache/* path. But there is a problem, that is how to force clear caching. The above Web.xml code can be seen, through the Cachefilter filter only configured the cache effective time, cannot force clear cache. in the actual application, but often encounter to delete the cache, for example, the menu Management page, basically the data on this page will be unchanged, less changes, but there will be changes, such as this situation will need to change the cache immediately after, otherwise the user will see the old menu. I looked for the next Oscache API document and sent it to clear the cache with the following code:
Cache cache = Servletcacheadministrator.getinstance (Request.getsession (). Getservletcontext ()). GetCache (Request,
scopetype);
Cache.flushentry (key);
The problem with this code is that you must know that the cached key is able to delete the corresponding cache. Next, we looked at the Com.opensymphony.oscache.web.filter.CacheFilter source code and found the following code:
/**
* {@link icachekeyprovider}
* @see com.opensymphony.oscache.web.filter.icachekeyprovider# Createcachekey (Javax.servlet.http.HttpServletRequest, Servletcacheadministrator, Cache)
*/Public
String Createcachekey (HttpServletRequest HttpRequest, Servletcacheadministrator scadmin, cache cache) {return
Scadmin.generateentrykey (null, HttpRequest, cachescope);
}
This code, is the cache key generation algorithm. Can see Oscache has its own key generation rules, but if replaced by their own key generation rules, you can not know, the project all pages corresponding to the cache key. So I wrote a cachefilter, the code is as follows:
Package edu.filter;
Import Javax.servlet.http.HttpServletRequest;
Import Com.opensymphony.oscache.base.Cache;
Import Com.opensymphony.oscache.web.ServletCacheAdministrator;
Import Com.opensymphony.oscache.web.filter.CacheFilter;
/**
* Title:CustomOscacheFilter.java
* Description: custom Oscache Cache filter
* modifies key generation rules, Make the page path (relative to the Webapp path) as key
* @author Yh.zeng
* @date 2017-6-16/public
class Customoscachefilter Extends cachefilter{
private string getquerystring (HttpServletRequest httprequest) {
String querystring = Httprequest.getquerystring ();
if (querystring = null | | querystring.equals ("")) {return
"";
} else{return
"?" + querystring;
}
}
@Override public
String Createcachekey (httpservletrequest httprequest,
servletcacheadministrator scadmin , cache cache) {return
Httprequest.getservletpath () + getquerystring (HttpRequest);
}
}
Modify Web-inf/web.xml, Filter Cachefilter class name changed to its own Write filter class, the code is as follows:
<filter>
<filter-name>CacheFilter</filter-name>
<!--<filter-class> com.opensymphony.oscache.web.filter.cachefilter</filter-class>-->
<filter-class> edu.filter.customoscachefilter</filter-class>
<init-param>
<description> Cache valid time, unit seconds, Set up here for 7 days </description>
<param-name>time</param-name>
<param-value>604800</ param-value>
</init-param>
<init-param>
<description> Cache scope, can only be session or application</description>
<param-name>scope</param-name>
< param-value>application</param-value>
</init-param>
</filter>
< filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/oscache/* </url-pattern>
</filter-mapping>
Then the code that forces the deletion of the cache is also encapsulated into a tool class, as follows:
Package utils;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.jsp.PageContext;
Import Com.opensymphony.oscache.base.Cache;
Import Com.opensymphony.oscache.web.ServletCacheAdministrator; /** * Title:OscacheUtils.java * Description:oscache Cache Tool Class * @author Yh.zeng * @date 2017-6-16/public class Oscach eutils {public enum cache_scope{application_scope, session_scope}/** * Force refresh (that is, clear) Oscache cached data * @param r Equest Request Object * @param key Cache key * @param scope Cache Scope */public static void Flushcachebykey (HttpServlet
Request request, String key, Cache_scope SCOPE) {int scopetype = 0;
Switch (scope) {case application_scope:scopetype = Pagecontext.application_scope;
Break
Case session_scope:scopetype = Pagecontext.session_scope;
Break Cache cache = Servletcacheadministrator.getinstance (Request.getsession (). Getservletcontext ()). GetCache (Request,
Scopetype);
Cache.flushentry (key);
}
}
3, write a test page/oscache/test.jsp, test caching function
<%@ page language= "java" import= "Java.util.Date" pageencoding= "Utf-8"%>
You will find that no matter how the refresh reopen the test.jsp page, the time will not change. This is because the first time the page was opened, the page was Oscache cached, the next time the page is reopened, will read the page data from the cache, the server will not reparse this page. The following background logs will show you:
[INFO][2017-06-17 00:35:55][cachefilter:116]-Oscache:filter in scope 4 [INFO][2017-06-17 00:35:55] [cachefilter:171]-oscache:new cache entry, cache stale or cache scope flushed for/oscache/test.jsp [INFO][2017-06-17 0 0:35:56][CACHEFILTER:186]-oscache:new entry added to the cache with key/oscache/test.jsp [INFO][2017-06-17 00:35:59][c ACHEFILTER:116]-Oscache:filter in scope 4 [info][2017-06-17 00:35:59][cachefilter:146]-oscache:using Cached entry fo r/oscache/test.jsp [info][2017-06-17 00:35:59][cachefilter:116]-Oscache:filter in scope 4 [INFO][2017-06-17 00:35:59] [cachefilter:146]-oscache:using Cached Entry for/oscache/test.jsp [INFO][2017-06-17 00:36:00][cachefilter:116]-OSCa Che:filter in scope 4
, and you can see that the cached key for the/oscache/test.jsp page corresponds to/oscache/test.jsp. You may ask questions about this page access path after the request parameter will refresh time. The answer, yes. For example, you visit http://localhost:8080/OscacheTestProj/oscache/test.jsp?a=1 the time of the page changes, because this time the page corresponds to the cache key=/oscache/ Test.jsp?a=1, just oscache the cache does not have key=/oscache/test.jsp?a=1 cache data, so the server will reparse the page and create key=/oscache/test.jsp?a=1 cached data. You can see the background log:
[INFO] [2017-06-17 00:44:02] [cachefilter:116]-Oscache:filter in scope 4
[info][2017-06-17 00:44:02][cachefilter:146]-oscache:using cached en Try for/oscache/test.jsp
[info][2017-06-17 00:44:03][cachefilter:116]-Oscache:filter in scope 4
[INFO][ 2017-06-17 00:44:03][cachefilter:146]-oscache:using Cached entry for/oscache/test.jsp
[info][2017-06-17 00:44:31][CACHEFILTER:116]-Oscache:filter in scope 4
[info][2017-06-17 00:44:31][cachefilter:171]-oscache:new C Ache entry, cache stale or cache scope flushed for/oscache/test.jsp?a=1
[info][2017-06-17 00:44:31][cachefilter:186 ]-Oscache:new entry added to the cache with Key/oscache/test.jsp?a=1
4, you will find that the test page/oscache/test.jsp no "clear cache" function, this function is indeed very practical. You must remember the Oscacheutils.java tool class that I mentioned earlier, which can be used to clear the cache. This is the function of manual removal, which is definitely the function of front-end and back-end interaction, so the front-end I consider using jquery ajax, backend, write a servlet call Oscacheutils.java tool class to implement, the code is as follows:
Package edu.servlet;
Import java.io.IOException;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.log4j.Logger; Import Utils.
Oscacheutils; Import Utils.
StringUtils; /** * Title:ClearCacheServlet.java * Description: Clear key corresponding Oscache cache * @author Yh.zeng * @date 2017-6-16/Public cl
Ass Clearcacheservlet extends HttpServlet {private static final long serialversionuid = -3266620619413471751l;
Private Logger Logger = Logger.getlogger (Clearcacheservlet.class); Private Oscacheutils.cache_scope Cache_scope = Oscacheutils.cache_scope. Application_scope;
Cache scope @Override public void init (ServletConfig config) throws servletexception {super.init (config);
String scope = config.getinitparameter ("Scope"); if (scope!= null) {if (!scope.equals ("Application") &&!scope.equals ("session") {LoGger.error (the value of the Init-param parameter scope must be application or session.)
"); throw new Servletexception ("The value of the Init-param parameter scope must be application or session."
"); }else{if (scope.equals ("Application")) {Cache_scope = Oscacheutils.cache_scope.
Application_scope; }else{Cache_scope = Oscacheutils.cache_scope.
Session_scope; @Override protected void doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexcept
Ion, IOException {String CacheKey = Req.getparameter ("CacheKey");//Cached Key Boolean success = TRUE;
try{Oscacheutils.flushcachebykey (req, CacheKey, cache_scope);
}catch (Exception e) {e.printstacktrace ();
Logger.error (Stringutils.getexceptionmessage (e));
Success = false;
} if (success) {Resp.getwriter (). Write ("Success");
}else{resp.getwriter (). Write ("fail"); } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, I oexception {doget (req, resp);
}
}
Modify the Web-inf/web.xml and add the Clearcacheservlet configuration as follows:
<servlet>
<servlet-name>clearCacheServlet</servlet-name>
<servlet-class> edu.servlet.clearcacheservlet</servlet-class>
<init-param>
<param-name>scope</ param-name>
<param-value>application</param-value>
</init-param>
</servlet >
<servlet-mapping>
<servlet-name>clearCacheServlet</servlet-name>
< Url-pattern>/servlet/clearcacheservlet</url-pattern>
</servlet-mapping>
/oscache/test.jsp has also been modified with the following code:
<%@ page language= "java" import= "Java.util.Date" pageencoding= "Utf-8"%>
Effect:
Click "Clear Cache", then refresh the page or open the page will find that the time of the page display will change.
Finally, the resource structure of the project is shown below:
Oscache-2.4.1-full.zip Download Address: http://download.csdn.net/detail/yh_zeng2/9870218
Demo of this case download address: https://github.com/zengyh/OscacheTestProj.git