Redis的啓動
1.redis最簡單的啓動就是運行redis安裝目錄下的redis-server.exe文件,運行成功會打開一個dos命令窗口,但是窗口已關閉redis就會停止運行
2、還有一種方法是將Redis變成服務進程,操作方法如下
1.win+R,輸入cmd打開命令窗口
2.進入redis安裝目錄
3.輸入:redis-server –service-install redis.windows.conf –loglevel verbose ( 安裝redis服務 )
4.輸入:redis-server –service-start ( 啓動服務 )
5.輸入:redis-server –service-stop(停止服務)
序列化與反序列化概念
序列化:把對象轉換爲字節序列的過程稱爲對象的序列化。
反序列化:把字節序列恢復爲對象的過程稱爲對象的反序列化。
SSM結合Redis
pom.xml(注意版本的一致,不然會出現問題)
<!-- config redis data and client jar-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
redis.properties
#redis主機IP
redis.host=127.0.0.1
#端口
redis.port=6379
redis.password=
# 最大連接數
redis.maxTotal=30
# 最大空閒連接數
redis.maxIdle=10
# 獲取鏈接最大等待毫秒
redis.maxWaitMillis=1000
# 獲取鏈接時檢查有效性
redis.testOnBorrow=true
redis.testOnReturn=true
applicationContext-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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:property-placeholder location="classpath:dbConfig/redis.properties" ignore-unresolvable="true"/>
<!-- redis數據源 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
<property name="testOnReturn" value="${redis.testOnReturn}"></property>
<!-- 在空閒時檢查有效性, 默認false -->
<property name="testWhileIdle" value="true" />
<!-- 連接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!--設置鏈接屬性-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!-- jedis模板配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>
</beans>
junit test.class
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test2()
{
redisTemplate.opsForValue().set("pass", "123456");
logger.info("value "+redisTemplate.opsForValue().get("pass"));
}
RedisTemplate介紹
Spring封裝了RedisTemplate對象來進行對Redis的各種操作,它支持所有的Redis原生的api。RedisTemplate位於spring-data-redis包下。
創建工具類redisTemplateUtil來實現對redis的操作
public class RedisTemplateUtil {
public static Logger logger=LoggerFactory.getLogger(RedisTemplateUtil.class);
private RedisTemplate redisTemplate;
public RedisTemplateUtil(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void set(String key, Object value) {
ValueOperations valueOperations = redisTemplate.opsForValue();
//設置過期時間 單位爲秒
valueOperations.set(key, value,60*3,TimeUnit.SECONDS);
//BoundValueOperations的理解對保存的值做一些細微的操作
// BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key);
}
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
public void setList(String key, List<?> value) {
//Operation 操作。
ListOperations listOperations = redisTemplate.opsForList();
// for(int i=0;i<value.size();i++)
// {
// listOperations.leftPush(key, value.get(i));
// }
listOperations.leftPush(key, value);
}
public Object getList(String key) {
return redisTemplate.opsForList().leftPop(key);
}
public void setSet(String key, Set<?> value) {
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add(key, value);
}
public Object getSet(String key) {
return redisTemplate.opsForSet().members(key);
}
public void setHash(String key, Map<String, ?> value) {
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.putAll(key, value);
}
public Object getHash(String key) {
return redisTemplate.opsForHash().entries(key);
}
public void delete(String key) {
redisTemplate.delete(key);
}
public void clearAll() {
redisTemplate.multi();
}
public boolean hasKey(String key)//判斷redis數據庫中是否有key這個關鍵字
{
return redisTemplate.hasKey(key);
}
}