1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| public class CacheImpl <K,V> implements cache.Cache <K,V> {
net.sf.ehcache.Cache localCache = null;
Class valueClass;
public CacheImpl(CacheManager CM, String cacheName)
{
//URL url = getClass().getResource("ehcache.xml");
CacheManager manager = CM;
String name = "ImplCache_"+cacheName;
localCache = manager.getCache(cacheName);
System.out.println("Cache créé : " + name);
}
public void putObject(K key, V value) {
Element wrapperElement = new Element(key,value);
localCache.put(wrapperElement);
valueClass = value.getClass();
}
public V getObject(K key) {
Element wrapperElement = localCache.get(key);
return (V) wrapperElement.getObjectValue();
}
public String getValueClass() {
return valueClass.getName();
}
/* méthodes imposées par l'interface Map */
public int size() {
return localCache.getSize();
}
public boolean isEmpty() {
return localCache.getSize() == 0 ? true : false;
}
public boolean containsKey(Object key) {
return localCache.isKeyInCache(key);
}
public boolean containsValue(Object value) {
return localCache.isValueInCache(value);
}
public V get(Object key) {
return this.getObject((K) key);
}
public V put(K key, V value) {
this.putObject(key, value);
return value;
}
public V remove(Object key) {
V value = this.getObject((K) key);
this.localCache.remove(key);
return value;
}
public void putAll(Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void clear() {
this.localCache.dispose();
}
public Set<K> keySet() {
return new HashSet(this.localCache.getKeys());
}
public Collection<V> values() {
List<V> list = new ArrayList<V>(this.size());
Set<K> keys = this.keySet();
for(K key : keys)
{
list.add(this.getObject(key));
}
return list;
}
public Set<Entry<K, V>> entrySet() {
throw new UnsupportedOperationException("Not supported yet.");
}
/*
public boolean exists(String r) {
try{
localCache.get(r);
return true;
}catch(Exception e){
return false;
}
}
* */
} |