首先,需要引入的包 Maven
<!-- fastjson json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.52</version>
</dependency>
然后配置 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"/>
<property name="minIdle" value="100"/>
<property name="maxTotal" value="100"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="192.168.0.1" // Redis缓存的服务器地址
p:port="6379"
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>
然后 添加redis.properties文件(不是必须的)
redis.host=255.255.255.255 // redis缓存地址
redis.port=6379
redis.pass=
#maxIdle
redis.maxIdle=30
#minIdle
redis.minIdle=10
#maxTotal
redis.maxTotal=100
#timeout
redis.timeout=5000
redis.testOnBorrow=false
redis.testOnReturn=false
redis.database=0
这样配置就完成了。
在Java中如何使用引入配置的Redis缓存:
@Resource(name = "RedisTemplate")
private ListOperations<String, TestLogPO> listOperations;
@Resource(name = "RedisTemplate")
private RedisTemplate redisTemplate;
listOperations.leftPush(TEST_TEMPLATE_KEY, TestLogPO);
redisTemplate.expireAt(TEST_TEMPLATE_KEY, endTime);
当然这位只是引入一个Redis缓存,如果你的redis有多个,并且不在同一个服务器下,这时你需要再次引入redis
需要在applicationContext-redis.xml 文件中添加引入Redis配置:
在配置文件中添加一个bean标签:
<bean id="Test2JedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="255.255.255.255" // 随便写的
p:port="6379"
p:pool-config-ref="jedisPoolConfig"
p:use-pool="true"
p:database="2"/>
还要添加一个:
<bean id="Test2RedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="Test2JedisConnectionFactory"
p:keySerializer-ref="Test2StringRedisSerializer"
p:valueSerializer-ref="Test2JdkSerializationRedisSerializer"/>
<bean id="Test2StringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="Test2JdkSerializationRedisSerializer"
class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"/>
在Java中如何使用引入配置的Redis缓存:
@Resource(name = "Test2RedisTemplate")
private ListOperations<String, TestLogPO> test2ListOperations;
@Resource(name = "Test2RedisTemplate")
private RedisTemplate test2RedisTemplate;
test2ListOperations.leftPush(TEST_TEMPLATE_KEY, TestLogPO);
test2RedisTemplate.expireAt(TEST_TEMPLATE_KEY, endTime);
结束!作者是菜鸟,如有错误请指导改正
原文链接: https://onlyou.blog.csdn.net//article/details/100633012