public V get(final K key) { Session s; try { s = oGrid.getSession(); ObjectMap map = s.getMap(cacheName); return (V) map.get(key); } catch (ObjectGridException oge) { throw new RuntimeException("Error performing cache operation", oge); } finally { if (s != null) s.close(); } return null; }
public void put(final K key, final V value) { Session s; try { s = oGrid.getSession(); ObjectMap map = s.getMap(cacheName); map.upsert(key, value); } catch (ObjectGridException oge) { throw new RuntimeException("Error performing cache operation", oge); } finally { if (s != null) s.close(); } }
public Map<K, V> getAll(Set<? extends K> keys) { final List<V> valueList = new ArrayList<V>(); final List<K> keyList = new ArrayList<K>(); keyList.addAll(keys);
Session s; try { s = oGrid.getSession(); ObjectMap map = s.getMap(cacheName); valueList.addAll(map.getAll(keyList)); } catch (ObjectGridException oge) { throw new RuntimeException("Error performing cache operation", oge); } finally { if (s != null) s.close(); }
Map<K, V> map = new HashMap<K, V>(); for (int i = 0; i < keyList.size(); i++) { map.put(keyList.get(i), valueList.get(i)); } return map; }
遇到的问题
1 2 3 4 5 6 7 8 9 10 11
Session s; try { s = oGrid.getSession(); ObjectMap map = s.getMap(cacheName); // Some small bit of business logic goes here } catch (ObjectGridException oge) { throw new RuntimeException("Error performing cache operation", oge); } finally { if (s != null) s.close(); }
public V get(final K key) { return executeWithMap(new Executable<V>() { public V execute(ObjectMap map) throws ObjectGridException { return (V) map.get(key); } }); }
public void put(final K key, final V value) { executeWithMap(new Executable<Void>() { public Void execute(ObjectMap map) throws ObjectGridException { map.upsert(key, value); return null; } }); }
public Map<K, V> getAll(Set<? extends K> keys) { final List<K> keyList = new ArrayList<K>(); keyList.addAll(keys); List<V> valueList = executeWithMap(new Executable<List<V>>() { public List<V> execute(ObjectMap map) throws ObjectGridException { return map.getAll(keyList); } });
Map<K, V> map = new HashMap<K, V>(); for(int i = 0; i < keyList.size(); i++) { map.put(keyList.get(i), valueList.get(i)); } return map; }