/** * Clear. */ public final void clear() { sessionCache = new ConcurrentHashMap<Object, Object>(); // Clear L2 Cahce if (getL2Cache() != null) { getL2Cache().evictAll(); } }
/** * 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); } } }
/** * 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); } } }
Cache c = (Cache) getL2Cache(); if (c != null)
/** * 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()); }