Redis 单机版
Redis 支持单机版和集群,下面的步骤是单机版安装步骤
1. # yum install -y gcc-c++
1.1 由于是c语言编写,所以需要安装支持组件
2. 把压缩包上传到linux服务器上
2.1 示例位置: /usr/local/tmp/ 下
3. # cd /usr/local/tmp
# tar zxvf redis-3.0.6.tar.gz
3.1 进入到/usr/local/tmp 下 运行解压命令
4. # make
4.1 进入到解压后的目录编译
5. # make install PREFIX=/usr/local/redis
5.1 安装,设置安装路径为/usr/local/redis 下
5.2 进入到src下安装
6. # ./redis-server
6.1 前端启动,安装后不能进行其他操作
6.2 Ctrl+c 退出
6.3 命令要在bin目录下执行
7. # cp /usr/local/tmp/redis-3.0.0/redis.conf /usr/local/redis/bin
7.1 把解压目录下配置文件拷贝到安装目录的bin下
8.# vi redis.conf
8.1 修改bin下redis.conf
8.2 把daemonize 由no修改成yes,守护进程启动(后台运行)
8.3 protected-mode no(关闭保护模式)
8.4注释这一段:# bind 127.0.0.1(只允许的IP访问)
9.# ps aux|grep redis
9.1 查看redis启动情况
10../redis-server redis.conf
10.1 启动redis服务
11.# ./redis-cli shutdown
10.1 如果希望关闭,运行上面命令,不关闭不运行即可
12.# ./redis-cli
11.1 进入到自带客户端工具,测试redis是否可用
12 # set name ‘smallming’
12.1 添加一个string ,key为name,value为smallming
13# get name
13.1 取出name中内容
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.shsxt</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0-SNAPSHOT</version> <name>spring-data-redis</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring 核心 jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- spring 测试 jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- spring jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- spring 事物 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- aspectj 切面编程的 jar --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!-- c3p0 连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- 添加 mybatis 与 Spring 整合的核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- mysql 驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- 日志打印相关的 jar --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.2</version> </dependency> <!-- springmvc 依赖 jar --> <!-- spring web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- 添加 json 依赖 jar 包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support </artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.21</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!-- Redis 依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.1</version> </dependency> <!-- spring data redis 依赖 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.18.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <!-- 读取配置文件 --> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.tld</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
UserServiceImpl加入缓存
package com.dbc.service.impl; import com.dbc.dao.UserDao; import com.dbc.entity.User; import com.dbc.service.UserService; import com.dbc.util.JsonUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; /** * @author DBC * @date create 2019/4/6 17:22 */ @Service public class UserServiceImpl implements UserService { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private UserDao userDao; @Override public User queryUserById(Integer userId) { //return doQueryUserById(userId); return doQueryUserById2Json(userId); } private User doQueryUserById(Integer userId) { //从redis查询 User user = (User) redisTemplate.opsForValue().get("user:id_" + userId); if (null != user) { return user; } //没有从数据库查 user = userDao.queryUserById(userId); if (null != user) { redisTemplate.opsForValue().set("user:id" + userId, user); return user; } return null; } public User doQueryUserById2Json(Integer userId) { // 先从 Redis 查询 Object jsonUser = redisTemplate.opsForValue().get("user:id_" + userId); User user = null; if (null != user) { user = JsonUtil.jsonStr2Object((String) jsonUser, User.class); return user; } // Redis 没有再从 mysql 查询 user = userDao.queryUserById(userId); if (null != user) { redisTemplate.opsForValue().set("user:id_" + userId, JsonUtil.object2JsonStr(user)); return user; } return null; } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 连接池配置 --> <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> <!-- 客户端连接工厂 --> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true" p:host-name="192.168.48.130" p:port="6379" > <!--p:use-pool="true" p:host-name="192.168.48.130" p:port="6379" p:password="root">--> <!-- 连接池引用 --> <constructor-arg name="poolConfig" ref="jedisPoolConfig"/> </bean> <!-- redisTemplate 配置 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"> <!-- 配置序列化操作 --> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean> </beans>