檢查redis服務端配置
- 檢查是否配置redis伺服器密碼
redisAPI - 啟動服務端
redisAPI - 檢查redi服務端是否允許API程式所運作伺服器ip連接配接或者允許所有ip連接配接(*代表所有)
redisAPI
Jedis操作Redis
<!--
jedis 坐标添加
-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
package com.credi.xmjf.server.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
/**
* Created by Administrator on 2019/6/24.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class JedisTest {
@Test
public void test01(){
//建立Jedis對象 , 傳入構造參數為: redis伺服器ip和通路端口
Jedis jedis = new Jedis("192.168.200.210", 6379);
//設定redis伺服器的密碼
jedis.auth("123456");
//操作redis
jedis.set("color","blue");
jedis.expire("color",90);
System.out.println(jedis.ttl("color"));
}
}
Spring Data Redis 操作redis
<!--
jedis 坐标添加
-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!--
spring-data-redis 坐标添加
-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
redis 環境配置
-->
<!-- 連接配接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大連接配接數 -->
<property name="maxTotal" value="1024" />
<!-- 最大 空閑連接配接數 -->
<property name="maxIdle" value="200" />
<!-- 擷取連接配接時最大等待毫秒數 -->
<property name="maxWaitMillis" value="10000" />
<!-- 在擷取連接配接時檢查有效性 -->
<property name="testOnBorrow" value="true" />
</bean>
<!--
jedis 連接配接工廠配置 : 伺服器ip ,端口,密碼
使用了spring的簡化p标簽 , 注意配置xmlns:p="http://www.springframework.org/schema/p"
-->
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:use-pool="true" p:hostName="192.168.200.210" p:port="6379" p:password="123456" p:poolConfig-ref="jedisPoolConfig"/>
<!--
redis 模闆類配置
-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory"></property>
<!--對key和value的序列化-->
<property name="keySerializer" >
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="valueSerializer" >
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
</property>
</bean>
</beans>
package com.credi.xmjf.server.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* Created by Administrator on 2019/6/24.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class SpringDataRedisTest {
//1 . 注入redis模闆對象
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Test
public void test01(){
/*
redisTemplate.opsForValue();//操作字元串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
*/
// 儲存
redisTemplate.opsForValue().set("today","2019-06-24");
// 設定有效時間
redisTemplate.expire("today",90, TimeUnit.SECONDS);
// 擷取
System.out.println(redisTemplate.opsForValue().get("today"));
}
}