安装
https://blog.csdn.net/Yang_yangyang/article/details/88668221
https://www.cnblogs.com/liangyou666/p/9577468.html
需要注意的是:(设置服务前如果redis服务在开着 要先关闭redis服务 不然后面生成不了redis-6379.pid,可以查看redis服务进程 关闭杀死resid服务)
https://blog.csdn.net/lexang1/article/details/52381949
解压后进入src目录下执行make命令,先编译一下,然后在/usr/local目录下新建文件夹redis,在我们解压后的src目录下执行完编译后,执行安装指令:make PREFIX=/usr/local/redis install,安装到我们新建的文件夹下,安装完成后再把解压目录下的redis.conf复制到redis目录下的bin目录下,vim修改配置文件.
设置Redis开机自启
1.在/etc下新建redis文件夹
mkdir /etc/redis
2.把安装redis目录里面的redis.conf文件复制到/etc/redis/6379.conf里面,6379.conf是取的文件名称,启动脚本里面的变量会读取这个名称,所以要是redis的端口号改了,这里也要修改
cp /usr/local/redis-5.0.4/redis.conf /etc/redis/6379.conf
3.复制redis启动脚本到/etc/init.d/redis文件中
cp /usr/local/redis-5.0.4/utils/redis_init_script /etc/init.d/redis
4.修改启动脚本参数
vi /etc/init.d/redis
在/etc/init.d/redis文件的头部添加下面两行注释代码,也就是在文件中#!/bin/sh的下方添加
# chkconfig: 2345 10 90
# description: Start and Stop redis
如图
同时还要修改参数,指定redis的安装路径
保存退出
5.启动
打开redis命令:service redis start
关闭redis命令:service redis stop
设为开机启动:chkconfig redis on
设为开机关闭:chkconfig redis off
Redis缓存Demo
依赖导入spring和redis相关jar包
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
</dependencies>
创建redis.properties文件,保存公共信息
redis_host=192.168.220.128
redis_port=6379
redis_maxIdle=300
redis_maxWait=3000
redis_maxTotal=4
创建配置文件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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath*:properties/redis.properties"/>
<!-- redis相关配置 -->
<!-- 获取连接池poolConfig -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis_maxIdle}"></property>
<property name="maxWaitMillis" value="${redis_maxWait}"></property>
<property name="maxTotal" value="${redis_maxTotal}"></property>
</bean>
<!-- 获取连接池工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis_host}" p:port="${redis_port}" p:pool-config-ref="poolConfig">
</bean>
<!-- Jedis模板 -->
<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean>
</beans>
package com.offcn.demo;
import java.util.Set;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class Demo {
public static void main(String[] args) {
//读取配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
//获取模板对象
RedisTemplate<String,String> template = (RedisTemplate<String, String>) context.getBean("jedisTemplate");
//赋值
template.boundSetOps("name").add("xiaocang");
//取出相同name的所有值
Set<String> members = template.boundSetOps("name").members();
System.out.println(members);
//取出相同name的一个值
String pop = template.boundSetOps("name").pop();
System.out.println(pop);
}
}
从左装数据和丛有装数据是以0位界限的.
package com.offcn.demo;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class Demo3 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-redis.xml");
RedisTemplate<String,String> template = (RedisTemplate<String, String>) context.getBean("jedisTemplate");
//从右开始装数据
// template.boundListOps("person").rightPush("小苍");
// template.boundListOps("person").rightPush("小泽");
// template.boundListOps("person").rightPush("小西");
// template.boundListOps("person").rightPush("小东");
// List<String> range = template.boundListOps("person").range(0, 1);
// System.out.println(range);
// template.delete("person");
// System.out.println(template.boundListOps("person").range(0,100));
//从左边装数据
// template.boundListOps("person").leftPush("小苍left");
// template.boundListOps("person").leftPush("小泽left");
// template.boundListOps("person").leftPush("小西left");
// template.boundListOps("person").leftPush("小东left");
//
// template.delete("name");
// template.delete("person");
// System.out.println(template.boundListOps("person").range(0, 100));
//下标取值
// String index = template.boundListOps("person").index(0);
// System.out.println(index);
//
// template.boundListOps("person").remove(0, "小东left");
// System.out.println(template.boundListOps("person").range(0, 100));
template.boundHashOps("person").put("name", "小苍");
template.boundHashOps("person").put("age", "小泽");
System.out.println(template.boundHashOps("person").get("name"));
System.out.println(template.boundHashOps("person").values());
}
}
首页轮播广告添加缓存
同样的需要导入配置文件和属性文件,当然属性可以直接写死,但是推荐使用统一文件,这里需要注意的是在加载配置文件的时候,只能有一个文件加载,文件名写*.properties即可,如果在redis的配置文件中也扫描的话会扫不到.