@Test public void testClearAllCache() throws CacheException { cacheService.store(TEST1, "test1", "test1"); assertTrue("cache was not empty", cacheService.getCacheSize(TEST1) > 0); cacheService.clearAll(); assertTrue("cache was not cleared", cacheService.getCacheSize(TEST1) == 0); }
@Test public void testPutSameItemTwice() throws CacheException { cacheService.store(TEST1, "sameItem", "value"); assertEquals("first element not added", 1, cacheService.getCacheSize(TEST1)); cacheService.store(TEST1, "sameItem", "value"); assertEquals("element added twice", 1, cacheService.getCacheSize(TEST1)); }
@Test public void testPutSimpleObjectInCache() throws CacheException { final int cacheSize = cacheService.getCacheSize(TEST1); final String myObject = "testObject"; cacheService.store(TEST1, "test", myObject); assertEquals("cache size did not increased", cacheSize + 1, cacheService.getCacheSize(TEST1)); }
@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); }
@Before public void setUp() throws Exception { LOGGER.info("Testing : {}", name.getMethodName()); cacheService = getCacheService(); cacheService.clearAll(); cacheService.start(); }
@Test public void testTTL() throws Exception { CacheService cs = getCache(); int result = cs.longServiceToCache_3_100("foo"); assertTrue(result == cs.longServiceToCache_3_100("foo")); Thread.sleep(1000); assertTrue(result + 1 == cs.longServiceToCache_3_100("foo")); }
@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); }
@Test public void testInvalidMaxEntries() { CacheService cs = getCache(); try { cs.invalidMaxEntries(); fail("Should not allow maxEntries <= 0"); } catch (IllegalArgumentException e) { } }
@Test public void testPutLotOfItems() throws CacheException { cacheService.store(TEST2, "testLotOfItems0", "value0"); final int j = 20000; for (int i = 1; i <= j; i++) { cacheService.store(TEST2, "testLotOfItems" + i, "value" + i); } assertEquals("Not all elements were added", j + 1, cacheService.getCacheSize(TEST2)); }
@Test public void testPutComplexObjectInCache() throws CacheException { final int cacheSize = cacheService.getCacheSize(TEST1); 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); assertEquals("cache size did not increased", cacheSize + 1, cacheService.getCacheSize(TEST1)); }
@Test public void getKeysOfACache() throws CacheException { final List<?> keys = cacheService.getKeys(TEST1); assertFalse(keys.contains("aKeyThatMustBeHere")); final int cacheKeySize = keys.size(); cacheService.store(TEST1, "aKeyThatMustBeHere", "value1"); final List<?> keys2 = cacheService.getKeys(TEST1); assertEquals(cacheKeySize + 1, keys2.size()); assertTrue(keys2.contains("aKeyThatMustBeHere")); }
@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 testBasicCache() { CacheService cs = getCache(); int result = cs.longServiceToCache_5("static"); for (int n = 0; n < 5; n++) { int loopresult = cs.longServiceToCache_5("static"); assertTrue(loopresult == result); } }
@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); }
protected void registerCompletionListener() { if (!syncListenerRegistrations.isEmpty() && completionRegistrationId == null) { synchronized (completionRegistrationMutex) { if (completionRegistrationId == null) { final CacheService service = getService(); CacheEventListener entryListener = new CacheCompletionEventListener(); completionRegistrationId = service.registerListener(getDistributedObjectName(), entryListener); } } } }
@Test public void testPutLotOfItemsWithOverflow() throws CacheException { final int cacheSize = cacheService.getCacheSize(TEST1); final int j = 20000; for (int i = 0; i < j; i++) { cacheService.store(TEST1, "testLotOfItems" + i, "value" + i); } assertEquals( "Not all elements were added with the overflow", cacheSize + j, cacheService.getCacheSize(TEST1)); }
@Test public void testCacheMax() { CacheService cs = getCache(); for (int n = 0; n < 5; n++) { int loopresult = cs.longServiceToCache_5("" + n); assertTrue(loopresult == n); } for (int n = 0; n < 5; n++) { int loopresult = cs.longServiceToCache_5("" + n); assertTrue(loopresult == n); } assertTrue(0 == cs.longServiceToCache_5("0")); assertTrue(5 == cs.longServiceToCache_5("not cached")); }
protected void deregisterCompletionListener() { if (syncListenerRegistrations.isEmpty() && completionRegistrationId != null) { synchronized (completionRegistrationMutex) { if (completionRegistrationId != null) { final CacheService service = getService(); final boolean isDeregistered = service.deregisterListener(getDistributedObjectName(), completionRegistrationId); if (isDeregistered) { completionRegistrationId = null; } } } } }
public static List<AVUser> searchUser(String searchName, int skip) throws AVException { AVQuery<AVUser> q = AVUser.getQuery(AVUser.class); q.whereContains(User.USERNAME, searchName); q.limit(Constant.PAGE_SIZE); q.skip(skip); AVUser user = AVUser.getCurrentUser(); List<String> friendIds = new ArrayList<String>(CacheService.getFriendIds()); friendIds.add(user.getObjectId()); q.whereNotContainedIn(Constant.OBJECT_ID, friendIds); q.orderByDescending(Constant.UPDATED_AT); q.setCachePolicy(AVQuery.CachePolicy.NETWORK_ELSE_CACHE); List<AVUser> users = q.find(); CacheService.registerUsers(users); return users; }
@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")); }
@Test(expected = CacheException.class) public void defaultConfigNotSerializablePutObject() throws CacheException { final String cacheName = "Should_use_default_config"; final String key = "someObjectCacheKey"; // We cannot store non-Serializable objects if copyOnRead or copyOnWrite is set to true: cacheService.store(cacheName, key, new Object()); }
/** 通知用 根据学生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 testSoftRef() { CacheService cs = getCache(); try { for (int n = 0; n < 1000; n++) { cs.bigMemHard(n); } fail("should run out of ram"); } catch (java.lang.OutOfMemoryError e) { // clear cache to free ram cs.__cacheData() .invalidateCache(MethodCall.get(cs.getClass(), "bigMemHard", (int) 0).getMethod()); } for (int n = 0; n < 10; n++) { cs.bigMemSoft(n); } }
public synchronized void cron_config(HttpServletRequest request) throws Exception { DataBaseDao dataBaseDao = (DataBaseDao) BeanFactory.getBean("dataBaseDao"); List<Map<String, Object>> query = dataBaseDao.executeQuery( "SELECT nextrun FROM " + JavaCenterHome.getTableName("cron") + " WHERE available>'0' ORDER BY nextrun LIMIT 1"); int nextrun = query.size() > 0 ? (Integer) (query.get(0).get("nextrun")) : 0; DataBaseService dataBaseService = (DataBaseService) BeanFactory.getBean("dataBaseService"); Map<String, Object> insertsqlarr = new HashMap<String, Object>(); insertsqlarr.put("var", "cronnextrun"); insertsqlarr.put("datavalue", nextrun); dataBaseService.insertTable("config", insertsqlarr, false, true); CacheService cacheService = (CacheService) BeanFactory.getBean("cacheService"); cacheService.config_cache(false); }
// 学生换班,管理员修改名称,删除该家长的缓存信息 private void deleteRedis(List<MFamilyRelation> frList) { List<String> keyList = new ArrayList<>(); for (MFamilyRelation fr : frList) { keyList.add(MessageFormat.format(CacheConstants.KEY_KID_OF_PARENT, fr.getUserId())); keyList.add(MessageFormat.format(CacheConstants.KEY_FAMILY_RELATION, fr.getUserId())); keyList.add(MessageFormat.format(CacheConstants.KEY_KID_NAME_OF_PARENT, fr.getUserId())); } if (CollectionUtils.isNotEmpty(keyList)) { cacheService.delete(keyList); } }
@Test public void testInvalidateAllValues() { CacheService cs = getCache(); cs.longServiceToCache_5("static1"); cs.longServiceToCache_5("static2"); cs.longServiceToCache_5("static3"); cs.longServiceToCache_5("static4"); cs.__cacheData() .invalidateCache( MethodCall.get(cs.getClass(), "longServiceToCache_5", "static").getMethod()); int prev = cs.getCounter(); cs.longServiceToCache_5("static1"); cs.longServiceToCache_5("static2"); cs.longServiceToCache_5("static3"); cs.longServiceToCache_5("static4"); assertTrue(prev + 4 == cs.getCounter()); }
@Test public void testInvalidateSingleton() { CacheService cs = getCache(); cs.longSingletonToCache_5("static"); cs.__cacheData() .invalidateCache(MethodCall.get(cs.getClass(), "longSingletonToCache_5", "static")); int prev = cs.getCounter(); cs.longSingletonToCache_5("static"); assertTrue(prev + 1 == cs.getCounter()); }
@Test public void testFIFO() { CacheService cs = getCache(); double result = cs.esFIFO(0); for (int n = 1; n < 6; n++) { cs.esFIFO(0); assertTrue(cs.esFIFO(n) == cs.esFIFO(n)); } assertTrue(result != cs.esFIFO(0)); }
@Test public void testLFU() { CacheService cs = getCache(); double result0 = cs.esLFU(0); double result1 = cs.esLFU(1); for (int n = 2; n < 6; n++) { cs.esLFU(0); assertTrue(cs.esLFU(n) == cs.esLFU(n)); } assertTrue(result0 == cs.esLFU(0)); assertTrue(result1 != cs.esLFU(1)); }