java后台验证码_Java后台生成验证码

packagecom.**.**.common.utils;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.HashOperations;importorg.springframework.data.redis.core.RedisCallback;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.core.ValueOperations;importorg.springframework.data.redis.serializer.StringRedisSerializer;importorg.springframework.stereotype.Component;importjava.io.Serializable;importjava.util.Set;importjava.util.concurrent.TimeUnit;

@Slf4j

@Componentpublic classRedisUtil {

@AutowiredprivateRedisTemplate redisTemplate;public voidremove(String... keys) {for(String key : keys) {

remove(key);

}

}public voidremovePattern(String pattern) {

Set keys = this.redisTemplate.keys(pattern);if (keys.size() > 0) {this.redisTemplate.delete(keys);

}

}public voidremove(String key) {if(exists(key)) {this.redisTemplate.delete(key);

}

}public booleanexists(String key) {return this.redisTemplate.hasKey(key).booleanValue();

}public T get(String key) {

T result= null;

ValueOperations operations = this.redisTemplate.opsForValue();

result=operations.get(key);returnresult;

}public booleanset(String key, T value) {boolean result = false;

ValueOperations operations = this.redisTemplate.opsForValue();

operations.set(key, value);

result= true;returnresult;

}/*** 只适用于key对应的值不再更新的问题,如果遇到包含生命周期的值需要更新的情景就不适用了,因为set 方法会丢失该key的生存时间,变成永久有效的

*@paramkey

*@paramvalue

*@paramexpireTime 单位秒

*@param

*@return

*/

public booleanset(String key, T value, Long expireTime) {boolean result = false;

ValueOperations operations = this.redisTemplate.opsForValue();

operations.set(key, value);this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);

result= true;returnresult;

}/***

* 只适用于key对应的值不再更新的问题,如果遇到包含生命周期的值需要更新的情景就不适用了,因为set 方法会丢失该key的生存时间,变成永久有效的

*@paramkey

*@paramvalue

*@paramexpireTime

*@paramunit

*@param

*@return

*/

public boolean set(String key, T value, Long expireTime, finalTimeUnit unit) {boolean result = false;

ValueOperations operations = this.redisTemplate.opsForValue();

operations.set(key, value);this.redisTemplate.expire(key, expireTime, unit);

result= true;returnresult;

}public T getHash(String hashName, String key) {

T result= null;

HashOperations operations = this.redisTemplate.opsForHash();

result=operations.get(hashName, key);returnresult;

}public voidsetHash(String hashName, String key, T value) {

redisTemplate.opsForHash().put(hashName, key, value);

}/*** 只适用于key对应的值不再更新的问题,如果遇到包含生命周期的值需要更新的情景就不适用了,因为set 方法会丢失该key的生存时间,变成永久有效的

*@paramkey

*@paramexpireTime*/

publicBoolean expire(String key, Long expireTime) {return this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);

}/*** 设置分布式锁

*@paramkey

*@paramvalue

*@return

*/

public synchronized Boolean setNX(final String key, final String value) throwsException{

Object obj= null;try{

obj= redisTemplate.execute((RedisCallback) connection ->{

StringRedisSerializer serializer= newStringRedisSerializer();

Boolean success=connection.setNX(serializer.serialize(key), serializer.serialize(value));

connection.close();returnsuccess;

});

}catch(Exception e) {

log.error("setNX com.strawhat.redis error, key : {} - {}", key,e);throwe;

}return obj != null ? (Boolean) obj : false;

}/*** 设置分布式锁,超时间单位秒

*@paramkey

*@paramvalue

*@return

*/

public synchronized Boolean setNX(final String key, final String value,long timeOut) throwsException {boolean b = this.setNX(key,value);

redisTemplate.expire(key,timeOut,TimeUnit.SECONDS);returnb;

}/*** 删除锁

*@paramkey

*@return

*/

public void unlock(finalString key) {

redisTemplate.delete(key);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值