/** * Clear. */ public final void clear() { sessionCache = new ConcurrentHashMap<Object, Object>(); // Clear L2 Cahce if (getL2Cache() != null) { getL2Cache().evictAll(); } }
/** * Removes the. * * @param <T> * the generic type * @param entityClass * the entity class * @param id * the id */ protected <T> void remove(Class<T> entityClass, Object id) { remove(entityClass, id, Boolean.TRUE); }
/** * Store in L1 only. * * @param id * the id * @param entity * the entity */ protected void store(Object id, Object entity) { store(id, entity, Boolean.TRUE); }
/** * Test. */ public void test() { assertNotNull(ems); // Store objects into session ems.store(person1.getPersonId(), person1); assertEquals(1, ems.getL2Cache().size()); ems.store(person2.getPersonId(), person2); assertEquals(2, ems.getL2Cache().size()); // Lookup object from session PersonnelDTO p1 = ems.lookup(PersonnelDTO.class, person1.getPersonId()); assertNotNull(p1); assertEquals(person1.getPersonId(), p1.getPersonId()); assertEquals(person1.getFirstName(), p1.getFirstName()); assertEquals(person1.getLastName(), p1.getLastName()); // Remove object from session ems.remove(PersonnelDTO.class, person1.getPersonId()); assertNotNull(ems); assertEquals(1, ems.getL2Cache().size()); // Clear session ems.clear(); assertNotNull(ems); assertEquals(0, ems.getL2Cache().size()); }
/** * Save to cache. * * @param id * the id * @param entity * the entity * @param spillOverToL2 * the spill over to l2 */ protected void store(Object id, Object entity, boolean spillOverToL2) { String key = cacheKey(entity.getClass(), id); LOG.debug("Writing to L1 >> " + key); sessionCache.put(key, entity); if (spillOverToL2) { LOG.debug("Writing to L2 >>" + key); // save to second level cache Cache c = (Cache) getL2Cache(); if (c != null) { c.put(key, entity); } } }
/** * Instantiates a new entity manager cache. * * @param cache * the cache */ public EntityManagerSession(Cache cache) { this.sessionCache = new ConcurrentHashMap<Object, Object>(); setL2Cache(cache); }
protected void tearDown() throws Exception { super.tearDown(); ems.clear(); cacheProvider.shutdown(); cache = null; }
protected void setUp() throws Exception { super.setUp(); Class<CacheProvider> cacheProviderClass = (Class<CacheProvider>) Class.forName(cacheProviderClassName); cacheProvider = cacheProviderClass.newInstance(); cacheProvider.init(cacheResource); cache = (Cache) cacheProvider.createCache("Kundera"); ems = new EntityManagerSession(cache); person1 = new PersonnelDTO("1", "Amresh", "Singh"); person2 = new PersonnelDTO("2", "Vivek", "Mishra"); }
/** * Removes the from cache. * * @param <T> * the generic type * @param entityClass * the entity class * @param id * the id * @param spillOverToL2 * the spill over to l2 */ protected <T> void remove(Class<T> entityClass, Object id, boolean spillOverToL2) { String key = cacheKey(entityClass, id); LOG.debug("Removing from L1 >> " + key); Object o = sessionCache.remove(key); if (spillOverToL2) { LOG.debug("Removing from L2 >> " + key); Cache c = (Cache) getL2Cache(); if (c != null) { c.evict(entityClass, key); } } }
protected <T> T lookup(Class<T> entityClass, Object id) String key = cacheKey(entityClass, id); LOG.debug("Reading from L1 >> " + key); T o = (T) sessionCache.get(key); Cache c = (Cache) getL2Cache(); if (c != null)