0、介紹
Redis 可以存儲鍵與5種不同數據結構類型之間的映射,這5種數據結構類型分別為String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合)。
1、maven依賴
<!--redis客戶端jedis依賴-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- redis Spring 基於注解配置 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
2、redis.propeprties
#ip地址
redis.hostName=47.93.216.***
#端口號
redis.port=6500
#如果有密碼
redis.password=123456
#客戶端超時時間單位是毫秒 默認是2000
redis.timeout=10000
#最大空閑數
redis.maxIdle=300
#連接池的最大數據庫連接數。設為0表示無限制,如果是jedis 2.4以后用redis.maxTotal
#redis.maxActive=600
#控制一個pool可分配多少個jedis實例,用來替換上面的redis.maxActive,如果是jedis 2.4以后用該屬性
redis.maxTotal=1000
#最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。
redis.maxWaitMillis=1000
#連接的最小空閑時間 默認1800000毫秒(30分鍾)
redis.minEvictableIdleTimeMillis=300000
#每次釋放連接的最大數目,默認3
redis.numTestsPerEvictionRun=1024
#逐出掃描的時間間隔(毫秒) 如果為負數,則不運行逐出線程, 默認-1
redis.timeBetweenEvictionRunsMillis=30000
#是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個
redis.testOnBorrow=true
#在空閑時檢查有效性, 默認false
redis.testWhileIdle=true
#redis.sentinel.host1=172.20.1.230
#redis.sentinel.port1=26379
#redis.sentinel.host2=172.20.1.231
#redis.sentinel.port2=26379
#redis.sentinel.host3=172.20.1.232
#redis.sentinel.port3=26379
3、spring-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<!-- redis連接池配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
<!--最大空閑數-->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--連接池的最大數據庫連接數 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<!--最大建立連接等待時間-->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!--逐出連接的最小空閑時間 默認1800000毫秒(30分鍾)-->
<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
<!--每次逐出檢查時 逐出的最大數目 如果為負數就是 : 1/abs(n), 默認3-->
<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
<!--逐出掃描的時間間隔(毫秒) 如果為負數,則不運行逐出線程, 默認-1-->
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
<!--是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個-->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<!--在空閑時檢查有效性, 默認false -->
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
</bean >
<!-- redis集群配置 哨兵模式 -->
<!-- <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
這個值要和Sentinel中指定的master的值一致,不然啟動時找不到Sentinel會報錯的
<property name="name" value="mymaster"></property>
</bean>
</property>
記住了,這里是指定Sentinel的IP和端口,不是Master和Slave的
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.sentinel.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.sentinel.port3}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean> -->
<!--redis連接工廠 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="poolConfig" ref="jedisPoolConfig"></property>
<!--IP地址 -->
<property name="hostName" value="${redis.hostName}"></property>
<!--端口號 -->
<property name="port" value="${redis.port}"></property>
<!--如果Redis設置有密碼,沒有的話就注釋掉下面的配置 -->
<!--<property name="password" value="${redis.password}" />-->
<!--客戶端超時時間單位是毫秒 -->
<property name="timeout" value="${redis.timeout}"></property>
</bean>
<!--redis操作模版,使用該對象可以操作redis -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="jedisConnectionFactory" />
<!--如果不配置Serializer,那么存儲的時候缺省使用String,如果用User類型存儲,那么會提示錯誤User can't cast to String!! -->
<property name="keySerializer" >
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer" >
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<!--開啟事務 -->
<property name="enableTransactionSupport" value="true"></property>
</bean >
<!--自定義redis工具類,在需要緩存的地方注入此類 -->
<bean id="redisUtil" class="com.demo.utils.RedisUtil">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
</beans>
4、測試類
public class TestRedis {
RedisUtil redisUtil = null;
@Before
public void before() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-redis2.xml");
redisUtil = (RedisUtil) context.getBean("redisUtil");
}
@Test
public void test1() throws InterruptedException {
System.out.println("---" + redisUtil.hasKey("mylist"));
System.out.println(redisUtil.expire("mylist", 100));
Thread.sleep(10000);
System.out.println(redisUtil.getExpire("mylist"));
}