redisTemplate.expire 无效和更新含有生存时间的key

本文详细介绍了Spring框架中Redis的配置方法及常见错误,包括Jedis连接池配置、序列化器设置、模板对象使用等,并提供了如何设置键值生存时间及在hash散列中的应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring中redis 配置的不对

如下配置:

  <!--redis配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
    <property name="maxIdle" value="${redis.maxIdle}" /> 
    <property name="maxTotal" value="${redis.maxActive}" /> 
    <property name="maxWaitMillis" value="${redis.maxWait}" /> 
    <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 
  </bean> 
  <!--spring redis配置-->   
  <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
    p:host-name="${redis.host}"
    p:port="${redis.port}"
    p:password="${redis.pass}" 
    p:pool-config-ref="poolConfig"/> 
<!--下面是配置的关键-->
<bean id="stringRedisSerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!--下面可以单独使用,可以设置生存时间-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connectionFactory-ref="connectionFactory" p:keySerializer-ref="stringRedisSerializer"
        p:hashKeySerializer-ref="stringRedisSerializer"
        p:hashValueSerializer-ref="stringRedisSerializer" />

<!--下面可以单独使用,可以设置生存时间-->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
        p:connectionFactory-ref="connectionFactory" />

使用:可以封装到dao中,也可以通过spring上下文去获取RedisTemplate或者 stringRedisTemplate来使用。举例:

redisTemplate.expire("redisobject",  30, TimeUnit.SECONDS);


stringRedisTemplate.expire("redis",  30, TimeUnit.SECONDS);


//注意保证使用的key是存在的
//redisTemplate.expire(key,timeout,timeunit);
//参数说明  key 需要设置的key  timeout:key的生存时间  timeuint:时间单位(小时,分钟,秒……)
//可以用 ttl key 命令查看该key的生存时间

 

 

 

 

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

比如以下场景:一天内限制某个用户只能访问2000次服务api。

怎么做呢?把值放进hash散列里,更新hash散列不会有问题。(实际上INCR,LPUSH,HSET,ZREM指令都不会影响key的生存时间,当然list,set都是可行的,根据自己的场景选择合适的数据结构)

下面简单列一下代码,使用spring-data(不推荐使用,封装的太繁琐,不如直接使用jedis,然后写个工具类做下简单封装)

    @Override//给指定的key设定生存时间
	public boolean setKeyLifeTime(String key,long lifetime,TimeUnit unit){
		boolean result=redisTemplate.expire(key, lifetime, unit);
		return result;
	}

	@Override//放入redis,更新也使用这个方法,前提key要存在
	public void setHash(String hashName, Object key, Object value) {
		redisTemplate.opsForHash().put(hashName, key, value);
	}

	@Override//取值
	public Object getHash(String hashName, Object key) {
		return redisTemplate.opsForHash().get(hashName, key);
	}

 

转载于:https://my.oschina.net/mifans/blog/792446

以下是springboot拦截器的代码 package com.player.music.config; import com.alibaba.fastjson2.JSONObject; import com.player.common.entity.ResultEntity; import com.player.common.entity.ResultUtil; import com.player.common.entity.UserEntity; import com.player.common.utils.JwtToken; import com.player.common.utils.ResultCode; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import java.io.IOException; import java.io.PrintWriter; @Component public class AuthInterceptor implements HandlerInterceptor { @Value("${token.secret}") private String secret; @Autowired private RedisTemplate redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String token = request.getHeader("Authorization"); if (token == null || redisTemplate.opsForValue().get(token) == null) { renderJson(response, ResultUtil.fail("未通过登录认证", null, ResultCode.LOGOUT)); return false; } UserEntity userEntity = JwtToken.parseToken(token, UserEntity.class,secret); if (userEntity == null) { response.setContentType("application/json;charset=UTF-8"); //设置编码格式 response.setCharacterEncoding("UTF-8"); response.setStatus(401); response.getWriter().write("未通过登录认证,请在登录页面登录"); renderJson(response, ResultUtil.fail("未通过登录认证", ResultCode.LOGOUT)); return false; } return true; } protected void renderJson(HttpServletResponse response, ResultEntity resultEntity) { String dataJson = JSONObject.toJSONString(resultEntity); PrintWriter writer; try { response.setContentType("application/json; charset=utf-8"); writer = response.getWriter(); writer.write(dataJson); writer.flush(); return; } catch (IOException e) { throw new RuntimeException(e); } } } 现在要增加redis+session做拦截请求,对以上代码进行修改
最新发布
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值