电商项目(十六)--缓存系统
缓存重要数据,首页在打开时,需要查询很多的数据,访问量也比较大,为了首页打开快快速,渲染的效果更快,用户的体验更好,则需要使用缓存,缓存的效率要高的多。
为了提高首页的查询效率,将数据迁移到缓存中
先插缓存,若缓存中没有,再去关系型数据库中查询,在将查询的结果插入到缓存中。
创建common_redis模块
在pom文件中添加依赖
<?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">
<parent>
<artifactId>bz_parent</artifactId>
<groupId>com.bjsxt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common_redis</artifactId>
<dependencies>
<!--pojo-->
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>common_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--utils-->
<dependency>
<groupId>com.bjsxt</groupId>
<artifactId>common_utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--Spring Boot Web Starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cloud Eureka Client Starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--Spring Cloud OpenFeign Starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--Spring Boot Data Redis Starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--Spring Cloud Config Client Starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--Spring Cloud AMQP: (RabbitMQ) Starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--Logback-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建配置文件appilication.yml
spring:
application:
name: common-redis
redis:
database: 1
host: 192.168.54.60
port: 6379
jedis:
pool:
max-active: 200 #最大连接数
max-wait: -1 #连接池最大等待时间(负数表示没有限制)
max-idle: 10 #连接池最大空闲数
min-idle: 0 #连接池最小空闲数
timeout: 2000 #连接超时时间
server:
port: 9080
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server:8761/eureka/
创建启动类
配置RedisTemplate序列化器
创建一个RedisConfig类,配置RedisTemplate序列化器,
/**
* 配置序列化器
*/
@Configuration
public class RedisConfig {
/**
* 配置RedisTemplate序列化器
*/
@Bean
public RedisTemplate<String,Object> setRedisTemplate(RedisConnectionFactory factory){
//创建RedisTemplate
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//设置序列化器
//创建Redis中的value的序列化器,默认是jdk的,但是内存消耗特别大,换成Jackson2JsonRedisSerializer 比较好
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//创建ObjectMapper 对象,Jackson2JsonRedisSerializer 转换的核心对象
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//创建Redis中的key的序列化器
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//设置Redis中的String类型的value的序列化器
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//设置Redis中的Hash类型的value的序列化器
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//设置Redis中的String类型的key的序列化器
redisTemplate.setKeySerializer(stringRedisSerializer);
//设置Redis中的Hash类型的key的序列化器
redisTemplate.setHashKeySerializer(stringRedisSerializer);
//创建对象以后,在对对象进行设置
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
在common_redis中实现缓存首页商品分类
逻辑分析:
在访问首页的时先现请求frontend_portal服务,在调用common_item服务,在做数据库mysql的查询,在依次返回浏览器。这个时候子查询的时候回比较耗时,降低了用户的体验,因此引入了缓存。
在访问首页的时候先请求frontend_portal服务,先请求调用common_redis服务,在做数据库redis的查询,有数据就返回给浏览器,若没有在调用common_item服务,在做数据库mysql的查询,在将数据通过common_redis添加到redis数据库中,再将数据的结果返回给浏览器。
当数据的分类进行了改动,怎么保证数据库的数据和redis的数据同步呢?
若后台进行了增删改操作,需要向common_redis 的数据全部移除掉即可。
添加配置文件信息:
下游服务
在common_redis模块中创建ItemCategoryController类,新增方法:将数据添加缓存首页商品分类,方法的返回值:void,方法的参数:@RequestBody CatResult catResult。向redis缓存中添加数据的方法,查询redis中缓存的商品分类信息的方法。
在对应的业务层中实现首页商品分类的查询操作。
@Value("${frontend_catresult_redis_key}")
private String frontend_catresult_redis_key;
/**
* 向缓存中添加首页商品分类
* @param catResult
*/
@Override
public void insertItemCategory(CatResult catResult) {
this.redisTemplate.opsForValue().set(this.frontend_catresult_redis_key,catResult);
/**
* 查询缓存中的首页商品分类
* @return
*/
@Override
public CatResult selectItemCategory() {
return (CatResult)this.redisTemplate.opsForValue().get(this.frontend_catresult_redis_key);
}
}
上游服务
在frontend_portal服务中的ItemCategory的类中
在业务层中进行修改先查询缓存,若是有直接查询缓存(使用了try catch 为了不影响下面的查询,即使缓存出错,还可以正常的查询数据库的操作),没有查询数据库,在将数据添加到缓存(使用了try catch 为了不影响下面的查询,即使缓存出错,还可以正常的查询数据库的操作)中即可实现。
添加feign的客户端类
在common_redis中实现缓存首页大广告位
下游服务
在common_redis模块中创建ContentController类,新增方法:将数据缓存首页大广告位,方法的返回值:void,方法的参数:@RequestBody List
在配置文件中新增配置信息:
在业务层中中,新增实现添加数据到缓存的方法。
上游服务
在frontend_portal服务中的ContentController 的类中
在业务层中进行修改,先查询缓存,若是有直接查询缓存(使用了try catch 为了不影响下面的查询,即使缓存出错,还可以正常的查询数据库的操作),没有查询数据库,在将数据添加到缓存(使用了try catch 为了不影响下面的查询,即使缓存出错,还可以正常的查询数据库的操作)中即可实现。(注意:不要忘记在feign中添加对应的接口方法)
上一篇: Spark调优 数据本地化调优