安装操作
https://www.runoob.com/redis/redis-install.html
redis.properties
# Redis settings
redis.host:127.0.0.1
redis.port:6379
redis.pass:123456
redis.timeout=2000
redis.maxIdle:300
redis.maxActive:600
redis.testOnBorrow:true
applicationContext.xml
<!-- jedis 配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.host}" />
<property name="password" value="${redis.pass}" />
<property name="timeout" value="${redis.timeout}"></property>
</bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
@Autowired
RedisTemplate redisTemplate = new RedisTemplate();ValueOperations<String,String> string = redisTemplate.opsForValue();
if (redisTemplate.hasKey("kkk")){
System.out.println("从redis中查询");
String rr = string.get("kkk");
System.out.println("从redis中查询,rr="+rr);
}else{
System.out.println("从数据库中查询");
string.set("kkk","2001");
//设置5秒过期,重新获取
string.set("kkk2","2002",5000L,TimeUnit.MILLISECONDS);
}