spring 刪除redis
redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
result = connection.del(Key.getBytes());
return result;
}
});
//舉例
// 從緩存中取驗證碼,根據用戶名,是否與傳入的匹配
final String Key = cus.getUsername() + "verificationCode";
Object strVerificationCode = redisTemplate.opsForValue().get(Key);
if (strVerificationCode == null)// 驗證碼沒取到 {
message.setCode(ConstantUtil.M_DATA_EXPIRE);
message.setMessage("驗證碼已過期,請重新獲取");
this.out(response, message); return;
}
if (!(strVerificationCode.equals(zipCode))) {
message.setCode(ConstantUtil.M_EXCEPTION);
message.setMessage("驗證碼錯誤");
this.out(response, message);
return;
} else {// 驗證通過,刪除驗證碼
redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
result = connection.del(Key.getBytes());
return result;
}
});
}
spring 刪除redis