View Javadoc
1   package net.sf.appstatus.support.spring.cache;
2   
3   import net.sf.appstatus.core.services.IServiceMonitor;
4   import net.sf.appstatus.core.services.ServiceMonitorLocator;
5   
6   import org.springframework.cache.Cache;
7   
8   /**
9    * A simple adapter for Spring Caches with flags the current service call as
10   * "cacheHit".
11   *
12   * @author Nicolas Richeton
13   *
14   */
15  public class AppStatusCacheAdapter implements Cache {
16  
17  	Cache cache;
18  
19  	public AppStatusCacheAdapter(Cache ehcache) {
20  		this.cache = ehcache;
21  	}
22  
23  	public void clear() {
24  		cache.clear();
25  	}
26  
27  	public void evict(Object key) {
28  		cache.evict(key);
29  	}
30  
31  	public ValueWrapper get(Object key) {
32  		ValueWrapper result = cache.get(key);
33  
34  		if (result != null) {
35  			IServiceMonitor monitor = ServiceMonitorLocator.getCurrentServiceMonitor();
36  			if (monitor != null) {
37  				monitor.cacheHit();
38  			}
39  		}
40  		return result;
41  	}
42  
43  	@SuppressWarnings("unchecked")
44  	public <T> T get(Object arg0, Class<T> arg1) {
45  		return (T) get(arg0);
46  	}
47  
48  	public String getName() {
49  		return cache.getName();
50  	}
51  
52  	public Object getNativeCache() {
53  		return cache.getNativeCache();
54  	}
55  
56  	public void put(Object key, Object value) {
57  		cache.put(key, value);
58  	}
59  
60  	public ValueWrapper putIfAbsent(Object arg0, Object arg1) {
61  		return cache.putIfAbsent(arg0, arg1);
62  	}
63  }