*
首先是Redis配置文件 applicationContext-redis.xml:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false">
<context:property-placeholder location="classpath:redis.properties"
ignore-unresolvable="true"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1000"/> <!-- ${redis.maxIdle} -->
<property name="minIdle" value="100"/> <!-- ${redis.minIdle} -->
<property name="maxTotal" value="100"/> <!-- ${redis.minIdle} -->
<property name="testOnBorrow" value="true"/> <!-- ${redis.testOnBorrow} -->
<property name="testOnReturn" value="true"/> <!-- ${redis.testOnReturn} -->
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="255.255.255.255" <!-- ${redis.host} -->
p:port="6379" <!-- ${redis.port} -->
p:pool-config-ref="jedisPoolConfig"
p:use-pool="true"
p:database="0"/> <!-- ${redis.database} -->
<bean id="RedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:keySerializer-ref="stringRedisSerializer"
p:valueSerializer-ref="JdkSerializationRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="JdkSerializationRedisSerializer"
class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"/>
</beans>
*
java 实现层引入Redis
// String类型
@Autowired
private RedisTemplate<String, String> redisTemplate;
*
操作Redis进行获取值,添加值
private static final String REDIS_STRING_KEY = "activity:template";
// (1)获取值
String jsonStr = redisTemplate.boundValueOps(REDIS_STRING_KEY).get();
/*
如果引入
@Autowired
private RedisTemplate<String, Object> redisTemplate;
获取redisTemplate.boundValueOps(REDIS_STRING_KEY).get() 返回的是一个对象,返回结果中包含一个@type键值*/
// (2) 添加值
String templateDetailJson = JSON.toJSONString(templateDetail); //对象转json串
//添加1
redisTemplate.opsForValue().set(ACTIVITY_TEMPLATE_KEY, templateDetailJson);
//添加2
redisTemplate.boundValueOps(ACTIVITY_TEMPLATE_KEY).set(templateDetailJson);
// 设置键的过期时间
// 制定过期时间 date
redisTemplate.expireAt(ACTIVITY_TEMPLATE_KEY, templateDetail.getEndTime());
// 设置1天后过期
redisTemplate.expire(ACTIVITY_TEMPLATE_KEY, 1, TimeUnit.DAYS);
后续更新。。。。。。。
原文链接: https://onlyou.blog.csdn.net//article/details/100763227