Example usage for org.springframework.cache Cache evict

List of usage examples for org.springframework.cache Cache evict

Introduction

In this page you can find the example usage for org.springframework.cache Cache evict.

Prototype

void evict(Object key);

Source Link

Document

Evict the mapping for this key from this cache if it is present.

Usage

From source file:org.hsweb.web.oauth2.service.OAuth2ServiceImpl.java

private void removeAccessFromCache(OAuth2Access auth2Access) {
    ///*from www  .  j a  v  a2 s  . co m*/
    if (cacheManager != null) {
        String cacheKey = "accessToken:".concat(auth2Access.getAccessToken());
        Cache cache = cacheManager.getCache(cacheName);
        cache.evict(cacheKey);
    }
}

From source file:com.baidu.rigel.biplatform.tesseract.datasource.impl.DataSourcePoolServiceImpl.java

@Override
public boolean destroyDataSourceInfo(DataSourceInfo dataSourceInfo) throws DataSourceException {
    DataSourceManager dataSourceManager = DataSourceManagerFactory.getDataSourceManagerInstance(dataSourceInfo);
    // ??/*from w  w w  .  ja v a  2 s.co  m*/
    dataSourceManager.removeDataSource(dataSourceInfo.getDataSourceKey());
    LOG.info("remove datasource from mem datasource pool");
    Cache dsInfoCache = storeManager.getDataStore(DATASOURCEINFO_POOL_CACHE_NAME);
    // cache???
    dsInfoCache.evict(dataSourceInfo.getDataSourceKey());
    LOG.info("remove datasource info from cache:{}", dsInfoCache);
    return true;
}

From source file:us.swcraft.springframework.cache.aerospike.usage.AerospikeCacheManagerKryoIT.java

@Test
public void getCacheNew_writeRead() {
    String name = "cache:ITNEW";
    Cache c = aerospikeCacheManager.getCache(name);
    c.put("B", "DEADBEEF");
    assertThat(c.get("B", String.class), is("DEADBEEF"));
    c.evict("B");
    assertThat(c.get("B"), nullValue());
}

From source file:us.swcraft.springframework.cache.aerospike.usage.AerospikeCacheManagerFastSerializeIT.java

@Test
public void getCacheDefault_writeRead() {
    String name = "cache:ITPRECONF";
    Cache c = aerospikeCacheManager.getCache(name);
    c.put("A", "DEADBEEF");
    assertThat(c.get("A", String.class), is("DEADBEEF"));
    c.evict("A");
    assertThat(c.get("A"), nullValue());
}

From source file:us.swcraft.springframework.cache.aerospike.usage.AerospikeCacheManagerFastSerializeIT.java

@Test
public void getCacheNew_writeRead() {
    String name = "cache:ITNEW";
    Cache c = aerospikeCacheManager.getCache(name);
    String id = UUID.randomUUID().toString();
    c.put(id, "DEADBEEF");
    assertThat(c.get(id, String.class), is("DEADBEEF"));
    c.evict(id);
    assertThat(c.get(id), nullValue());/*w w  w . ja v a 2s  .  c o  m*/
    System.out.print(false);
}

From source file:org.hsweb.web.controller.login.AuthorizeController.java

/**
 * ,?,?./*from  w w  w.ja  v a2  s  .co m*/
 * <ul>
 * <li>
 * ???{@link ConfigService#getInt(String, String, int)}:login,error.max_number,5
 * </li>
 * <li>
 * ???{@link ConfigService#getInt(String, String, int)}:login,error.wait_minutes,10
 * <p>
 * </li>
 * </ul>
 *
 * @param username ??
 * @param password ?
 * @param request  {@link HttpServletRequest}
 * @return 
 * @throws AuthorizeForbiddenException ??
 * @throws NotFoundException           ?
 * @throws Exception                   
 */
@RequestMapping(value = "/login", method = RequestMethod.POST)
@AccessLogger("")
public ResponseMessage login(@RequestParam String username, @RequestParam String password,
        HttpServletRequest request) throws Exception {
    //??
    String userIp = WebUtil.getIpAddr(request);
    int maxErrorNumber = configService.getInt("login", "error.max_number", 5);
    int waitMinutes = configService.getInt("login", "error.wait_minutes", 10);
    Cache cache = cacheManager.getCache("login.error");
    String cachePrefix = username.concat("@").concat(userIp);
    String timeCacheKey = cachePrefix.concat("-time");
    String numberCacheKey = cachePrefix.concat("-number");
    Integer error_number = cache.get(numberCacheKey, Integer.class);
    Long error_time = cache.get(timeCacheKey, Long.class);
    long now_time = System.currentTimeMillis();
    if (error_number != null && error_time != null) {
        if ((now_time - error_time) / 1000 / 60d > waitMinutes) {
            cache.evict(timeCacheKey);
            cache.evict(numberCacheKey);
            error_number = 0;
            error_time = 0l;
        }
        if (error_number >= maxErrorNumber)
            throw new AuthorizeForbiddenException("?,"
                    + (waitMinutes - ((now_time - error_time) / 1000 / 60)) + "??!");
    }
    User user = userService.selectByUserName(username);
    if (user == null || user.getStatus() != 1)
        throw new NotFoundException("?");
    //?
    if (!user.getPassword().equals(MD5.encode(password))) {
        if (error_number == null)
            error_number = 0;
        cache.put(timeCacheKey, System.currentTimeMillis());
        cache.put(numberCacheKey, ++error_number);
        throw new AuthorizeForbiddenException(
                "?,??" + (maxErrorNumber - error_number) + "");
    }
    cache.evict(timeCacheKey);
    cache.evict(numberCacheKey);
    user.setPassword("");//?
    if (user.getUsername().equals("admin"))
        userService.initAdminUser(user);
    else
        user.initRoleInfo();
    User newUser = new User();
    BeanUtilsBean.getInstance().getPropertyUtils().copyProperties(newUser, user);
    httpSessionManager.addUser(newUser, request.getSession());
    return ResponseMessage.ok();
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ?/*from   w  ww  .  j  a  va2s.  c om*/
 *
 * @param id  id
 *
 */
public void validMail(String id) {

    Cache cache = cacheManager.getCache(validMailCacheName);
    MailValidToken entity = cache.get(id, MailValidToken.class);

    if (entity == null) {
        throw new TokenNotFoundException("?ID[" + id + "]?");
    }

    try {
        long creationTime = entity.getCreationDate().getTime();
        long currentTime = System.currentTimeMillis();

        if (currentTime - creationTime > validMailExpiredTime) {
            cache.evict(id);
            throw new TokenExpiredException("ID[" + entity.getId() + "]");
        }

        Map<String, Object> principal = Subjects.getPrincipal();

        if (!principal.get("id").equals(entity.getPrincipal().get("id"))) {
            throw new ServiceException("???");
        }

        principal.put("isBindingMail", 1);
        principal.put("email", entity.getMail());

        Apis.invoke("accountService", "saveUser", principal, new ArrayList<>());
    } finally {
        cache.evict(id);
    }
}

From source file:org.hsweb.web.oauth2.service.OAuth2ServiceImpl.java

@Override
@Transactional(noRollbackFor = AccessTimeoutException.class)
public User getUserByAccessToken(String accessToken) {
    OAuth2Access auth2Access = null;//w  w  w  . j  a va2s .c o  m
    Cache cache = null;
    String cacheKey = "accessToken:".concat(accessToken);
    boolean inCache = false;
    if (cacheManager != null) {
        cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            Cache.ValueWrapper wrapper = cache.get(cacheKey);
            if (wrapper != null) {
                auth2Access = (OAuth2Access) wrapper.get();
                inCache = true;
            }
        }
    }
    if (auth2Access == null)
        auth2Access = oAuth2AccessMapper.selectByAccessToken(accessToken);
    if (auth2Access == null) {
        return null;
    }
    //?
    if (auth2Access.getLeftTime() <= 0) {
        if (cache != null) {
            cache.evict(cacheKey);
        }
        // TODO: 16-8-17 token
        oAuth2AccessMapper.deleteById(auth2Access.getId());
        throw new AuthorizeException("expired_token");
    }
    if (!inCache) {
        User user = userService.selectByPk(auth2Access.getUserId());
        user.initRoleInfo();
        user.setPassword(null);
        User newUser = new User();
        try {
            BeanUtilsBean.getInstance().getPropertyUtils().copyProperties(newUser, user);
        } catch (Exception e) {
        }
        auth2Access.setUser(newUser);
        cache.put(cacheKey, auth2Access);
        return newUser;
    } else {
        return auth2Access.getUser();
    }
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ?? key//from   www  . jav  a2s  .c  o  m
 *
 * @param id key
 *
 * @return ??? map?
 */
public ValidToken validForgotPassword(String id) {

    Cache cache = cacheManager.getCache(forgotPasswordCacheName);
    ValidToken entity = cache.get(id, ValidToken.class);
    if (entity == null) {
        throw new TokenNotFoundException("???");
    }

    try {

        long creationTime = entity.getCreationDate().getTime();
        long currentTime = System.currentTimeMillis();

        if (currentTime - creationTime > forgotPasswordExpiredTime) {
            throw new TokenExpiredException("ID[" + entity.getId() + "]");
        }
    } finally {
        cache.evict(id);
    }

    Map<String, Object> principal = entity.getPrincipal();
    id = new Md5Hash(principal.get("id").toString() + System.currentTimeMillis()).toHex();
    ValidToken validToken = new ValidToken(id, new Date(), principal);

    cache = cacheManager.getCache(resetPasswordCacheName);
    cache.put(id, validToken);

    return validToken;
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ??//from   w w  w.java  2s  . c o  m
 *
 * @param id       token id
 * @param password ?
 */
public void resetPassword(String id, String password, String confirmPassword) {

    if (!StringUtils.equals(password, confirmPassword)) {
        throw new ServiceException("????");
    }

    Cache cache = cacheManager.getCache(resetPasswordCacheName);
    ValidToken entity = cache.get(id, ValidToken.class);

    if (entity == null) {
        throw new TokenNotFoundException("???");
    }

    long creationTime = entity.getCreationDate().getTime();
    long currentTime = System.currentTimeMillis();

    try {
        if (currentTime - creationTime > resetPasswordExpiredTime) {
            throw new TokenExpiredException("ID[" + entity.getId() + "]");
        }

        Integer userId = Casts.cast(entity.getPrincipal().get("id"), Integer.class);

        Apis.invoke("accountService", "updateUserPassword", userId, password);
    } finally {
        cache.evict(id);
    }
}