@Test public void shortTimeoutConfig() throws Exception { final String key = "shortTimeout"; cacheService.store(SOME_DEFAULT_CACHE_NAME, key, new Object()); Object object = cacheService.get(SOME_DEFAULT_CACHE_NAME, key); assertNotNull("Object should be in cache", object); Thread.sleep(1020); object = cacheService.get(SOME_DEFAULT_CACHE_NAME, key); assertNull("Object should not be in cache any longer", object); }
@Test public void eternalCacheTest() throws Exception { final String key = "eternalCacheTest"; final Object value = new Object(); cacheService.store(ETERNAL_CACHE, key, value); Object object = cacheService.get(ETERNAL_CACHE, key); assertNotNull("Object should be in cache", object); Thread.sleep(1020); object = cacheService.get(ETERNAL_CACHE, key); assertEquals("Object should still be in cache", value, object); }
@Test public void testGetSimpleObjectInCache() throws CacheException { final String myObject = "testObject"; cacheService.store(TEST1, "test", myObject); final String inObject = (String) cacheService.get(TEST1, "test"); assertEquals("we didn't retrieve the same object", myObject, inObject); }
@Test public void testUpdateElementInCache() throws CacheException { cacheService.store(TEST1, "sameItem2", "value1"); assertEquals("first element not added", 1, cacheService.getCacheSize(TEST1)); cacheService.store(TEST1, "sameItem2", "value2"); assertEquals("element added 2 times", 1, cacheService.getCacheSize(TEST1)); assertEquals("element was not updated", "value2", cacheService.get(TEST1, "sameItem2")); }
@SuppressWarnings("unchecked") @Test public void testChangeCachedElementWithoutCopy() throws CacheException { final ArrayList<String> list = new ArrayList<String>(); cacheService.store(TEST2, "mylist", list); list.add("kikoo"); final ArrayList<String> cachedList = (ArrayList<String>) cacheService.get(TEST2, "mylist"); assertTrue("object was copied in cached", cachedList.size() == 1); }
@SuppressWarnings("unchecked") @Test public void testGetComplexObjectInCache() throws CacheException { final ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(); final HashMap<String, String> map = new HashMap<String, String>(); map.put("bpm", "bonita"); list.add(map); cacheService.store(TEST1, "complex", list); final Object object = cacheService.get(TEST1, "complex"); assertNotNull("the object does not exists", object); assertEquals( "Not the same object", "bonita", ((ArrayList<Map<String, String>>) object).get(0).get("bpm")); }
/** 通知用 根据学生Id查询家庭成员 */ @Override public List<FamilyRelationDTO> queryFamilyByStudent(String userId) { String key = MessageFormat.format(CacheConstants.KEY_PARENT_OF_KID, userId); List<FamilyRelationDTO> list = cacheService.get(key); if (list != null) { return list; } List<FamilyRelationDTO> familyRelationDTOs = new ArrayList<>(); List<MFamilyRelation> mFamilyRelations = famillyRelationMapper.queryFamilyByStudent(userId, userService.getSchoolId(userId)); for (MFamilyRelation fr : mFamilyRelations) { FamilyRelationDTO familyRelationDTO = new FamilyRelationDTO(); BeanUtils.copyProperties(fr, familyRelationDTO); familyRelationDTOs.add(familyRelationDTO); } cacheService.set(key, familyRelationDTOs, CacheConstants.CACHE_SEVEN_DAY, TimeUnit.DAYS); return familyRelationDTOs; }
@Test public void testPutNullInCacheShouldWork() throws CacheException { cacheService.store(TEST1, "test2", null); assertEquals("Null should be added to the cache", 1, cacheService.getCacheSize(TEST1)); assertTrue("Null value can't be put", cacheService.get(TEST1, "test2") == null); }
@Override public void print() { // Adds a key to the cache System.out.println("==== Node with lock: " + cacheService.get()); }
public Object get(String key) { return cacheService.get(key); }