/** 清除所有用户授权信息缓存. */ public void clearAllCachedAuthorizationInfo() { Cache<Object, AuthorizationInfo> cache = getAuthorizationCache(); if (cache != null) { for (Object key : cache.keys()) { cache.remove(key); } } }
@Test public void testReplace() { String firstValue = "abc"; String secondValue = "def"; cache.put("123", firstValue); assertEquals(firstValue, cache.put("123", secondValue)); assertEquals(secondValue, cache.get("123")); }
@Test public void testRemove() { cache.put("1", "one"); cache.put("2", "two"); assertEquals(2, cache.size()); cache.remove("1"); assertEquals(1, cache.size()); assertNull(cache.get("1")); }
@Test public void testClear() { cache.put("1", "one"); cache.put("2", "two"); cache.put("3", "three"); assertEquals(3, cache.size()); cache.clear(); assertEquals(0, cache.size()); }
@Override public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) { String username = (String) token.getPrincipal(); AtomicInteger retryCount = passwordRetryCache.get(username); if (retryCount == null) { retryCount = new AtomicInteger(0); passwordRetryCache.put(username, retryCount); } if (retryCount.incrementAndGet() > 10) { throw new ExcessiveAttemptsException(String.valueOf(retryCount.get())); } boolean matches = super.doCredentialsMatch(token, info); if (matches) { passwordRetryCache.remove(username); } return matches; }
// 删除所有菜单 public void clearAllMenu() { Cache<Object, Object> cache = cacheManager.getCache(MENU_CACHE_KEY); cache.clear(); }
// 删除菜单 public void clearMenu(Long roleId) { Cache<Object, Object> cache = cacheManager.getCache(MENU_CACHE_KEY); cache.remove(roleId); }
// 菜单 cache public void putMenu(Long roleId, List<MenuView> list) { Cache<Object, Object> cache = cacheManager.getCache(MENU_CACHE_KEY); cache.put(roleId, list); }
// 获取菜单 public List<MenuView> getMenu(Long roleId) { Cache<Object, Object> cache = cacheManager.getCache(MENU_CACHE_KEY); return (List<MenuView>) cache.get(roleId); }
@Test public void testSize() { assertEquals(0, cache.size()); cache.put("123", "abc"); assertEquals(1, cache.size()); }
@Test public void testGetExistentObject() { cache.put("123", "abc"); assertEquals("abc", cache.get("123")); }
@Test public void testGetNonexistentObject() { assertNull(cache.get("abc")); }
@PreDestroy private void destroy() { Cache<Serializable, Session> cache = getActiveSessionsCache(); cache.clear(); }
public void clearRetryCache(String username) { if (username == null) passwordRetryCache.clear(); else passwordRetryCache.remove(username); }
public int getRetryCount(String username) { AtomicInteger retryCount = passwordRetryCache.get(username); return retryCount != null ? retryCount.get() : 0; }