Example #1
0
public class SimpleCacheWrapper {

  protected final Logger logger = MyLoggerFactory.getLogger(this.getClass());
  public static final int DEFAULT_EXPIRE_TIME = 60 * 30; // a half hour

  private Cache cache;

  public SimpleCacheWrapper(Cache cache) {
    super();
    this.cache = cache;
  }

  public boolean canCache() {
    return this.cache != null;
  }

  @SuppressWarnings("unchecked")
  public <T> T get(String key) {
    if (!canCache()) return null;
    ValueWrapper value = cache.get(key);
    if (value == null) return null;
    CacheElement ce = (CacheElement) value.get();

    if (!ce.isIndate()) {
      logger.info("clear cache by key: " + ce);
      return null;
    }

    if (logger.isInfoEnabled()) {
      logger.info("get from cache by key: " + ce);
    }

    return (T) ce.getValue();
  }

  public void put(String key, Object value) {
    put(key, value, DEFAULT_EXPIRE_TIME);
  }

  public void put(String key, Object value, int expire) {
    if (!canCache() || value == null) return;
    CacheElement ce = CacheElement.create(key, value, expire);
    cache.put(key, ce);

    if (logger.isInfoEnabled()) {
      logger.info("put into cache: " + ce);
    }
  }
}
Example #2
0
public class WebFilterAdapter implements WebFilterInitializers, WebFilter {

  protected final Logger logger = MyLoggerFactory.getLogger(this.getClass());

  @Override
  public void onInit(FilterConfig config) {}

  /*@Override
  public void onThrowable(HttpServletRequest request, HttpServletResponse response, Throwable ex) throws IOException, ServletException {
  	if(ex instanceof ServletException)
  		throw (ServletException)ex;
  	else if(ex instanceof IOException)
  		throw (IOException)ex;
  	throw new ServletException("error: " + ex.getMessage(), ex);
  }*/

  @Override
  public void onFilter(HttpServletRequest request, HttpServletResponse response) {}

  @Override
  public void onFinally(HttpServletRequest request, HttpServletResponse response) {}
}