Commit 004b23ff authored by feng's avatar feng

Add a filter to chean the cache frequently

Add TimedCacheCleanerFilter class to clean the nesstar api cache every 10 minutes
And the expirationTimeout can also be configured in config.yaml, default value is 600 seconds
Fixes #2640x
parent 8c876038
......@@ -7,7 +7,7 @@ Getting Started
---------------
1. Run `mvn package` to build everything
1. Copy the `config.yaml.example` to `config.yaml` and edit it to add your own server URI and logon credentials.
1. Copy the `config.yaml.example` to `config.yaml` and edit it to add your own server URI, logon credentials and some configuration options like interval of clean cache.
1. Run `java -jar target/nesstar_rest_api-0.4.0.jar server config.yaml`
Note: The name of the JAR file may change. See the `target` folder.
......
serverURI: http://my-example.com
userId: myUser
password: s3cret-p4ssw0rd
expirationTimeout: 600 //the interval of clean cache, default value is 600 second
......@@ -10,6 +10,7 @@ public final class NesstarDropConfiguration extends Configuration {
private String serverURI;
private String userId;
private String password;
private String expirationTimeout;
public String getServerURI() {
return serverURI;
......@@ -22,4 +23,6 @@ public final class NesstarDropConfiguration extends Configuration {
public String getPassword() {
return password;
}
public String getExpirationTimeout() {return expirationTimeout;}
}
package com.nesstar.rest;
import com.nesstar.rest.filters.TabulationEntityTagFilter;
import com.nesstar.rest.filters.ManualJettyAllowWildcardOriginFilter;
import com.nesstar.rest.filters.CorrelationEntityTagFilter;
import com.nesstar.rest.filters.EntityTagFilter;
import com.nesstar.rest.filters.CubeEntityTagFilter;
import com.nesstar.rest.filters.RegressionEntityTagFilter;
import com.nesstar.rest.filters.LanguageFilter;
import com.nesstar.rest.filters.CommonEntityTagFilter;
import com.nesstar.rest.filters.*;
import com.nesstar.rest.resources.SearchResource;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlets.CrossOriginFilter;
......@@ -67,6 +60,9 @@ public class NesstarDropService extends Service<NesstarDropConfiguration> {
environment.addHealthCheck(new NesstarHealthCheck(serverHandler));
TimedCacheCleanerFilter timedCacheCleanerFilter = new TimedCacheCleanerFilter(configuration.getExpirationTimeout());
environment.addFilter(timedCacheCleanerFilter, "/*");
environment.addFilter(CrossOriginFilter.class, "/*")
.setInitParam(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");
......
package com.nesstar.rest.filters;
import com.nesstar.api.NesstarDBFactory;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Date;
public class TimedCacheCleanerFilter implements Filter {
private static long latestTimeStamp = 0;
private int EXPIRATION_TIMEOUT = 600 * 1000;
public TimedCacheCleanerFilter (String expirationTimeout) {
if (expirationTimeout != null) {
try {
this.EXPIRATION_TIMEOUT = Integer.parseInt(expirationTimeout) * 1000;
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
public synchronized void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//Check timestamp of cache
Date currentTime = new Date();
if (currentTime.getTime() - latestTimeStamp > this.EXPIRATION_TIMEOUT) {
this.latestTimeStamp = currentTime.getTime();
//If cache is old, clear
NesstarDBFactory.getInstance().clear();
}
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException { }
public void destroy() { }
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment