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);
}
}