Skip to content

Commit

Permalink
6.13 轮播图存入redis 直接从非关系型数据库中读取 不需要每次都去数据库中查 虽然这个项目轮播图是从前端拿的
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzzzzzyt committed Jun 13, 2022
1 parent 885827e commit 0e3b860
Show file tree
Hide file tree
Showing 26 changed files with 839 additions and 49 deletions.
564 changes: 564 additions & 0 deletions logs/MusicWebsite-2022-06-11.log

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion music-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ const HttpManager = {

deleteUserSupport:({commentId,userId}) => post(`userSupport/delete`, {commentId,userId}),

insertUserSupport:({commentId,userId}) => post(`userSupport/insert`, {commentId,userId})
insertUserSupport:({commentId,userId}) => post(`userSupport/insert`, {commentId,userId}),

//获取所有的海报
getBannerList: () => get("banner/getAllBanner")
};


Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 0 additions & 2 deletions music-client/src/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MUSICNAME } from "./music-name";
import { NavName, HEADERNAVLIST, SIGNLIST, MENULIST } from "./nav";
import { singerStyle } from "./singer";
import { SONGSTYLE } from "./songList";
import { swiperList } from "./swiper";
import { RouterName } from "./router-name";
import { validatePassword, SignInRules, SignUpRules } from "./validate";

Expand All @@ -19,7 +18,6 @@ export {
MENULIST,
singerStyle,
SONGSTYLE,
swiperList,
validatePassword,
SignInRules,
SignUpRules,
Expand Down
27 changes: 0 additions & 27 deletions music-client/src/enums/swiper.ts

This file was deleted.

10 changes: 8 additions & 2 deletions music-client/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!--轮播图-->
<el-carousel class="swiper-container" type="card" height="20vw" :interval="4000">
<el-carousel-item v-for="(item, index) in swiperList" :key="index">
<img :src="item.picImg" />
<img :src="HttpManager.attachImageUrl(item.pic)" />
</el-carousel-item>
</el-carousel>
<!--热门歌单-->
Expand All @@ -14,14 +14,20 @@
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import PlayList from "@/components/PlayList.vue";
import { swiperList, NavName } from "@/enums";
import { NavName } from "@/enums";
import { HttpManager } from "@/api";
import mixin from "@/mixins/mixin";
const songList = ref([]); // 歌单列表
const singerList = ref([]); // 歌手列表
const swiperList = ref([]);// 轮播图 每次都在进行查询
const { changeIndex } = mixin();
try {
HttpManager.getBannerList().then((res) => {
swiperList.value = (res as ResponseBody).data.sort();
});
HttpManager.getSongList().then((res) => {
songList.value = (res as ResponseBody).data.sort().slice(0, 10);
});
Expand Down
13 changes: 13 additions & 0 deletions music-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@
<artifactId>lombok</artifactId>
</dependency>

<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.6</version>
</dependency>
<!--<spring2.X集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>

</dependencies>

<!-- 阿里云搭建的国内镜像http://maven.aliyun.com,跑起来速度很快,可以进行配置-->
Expand Down
63 changes: 63 additions & 0 deletions music-server/src/main/java/com/example/yin/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.example.yin.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@EnableCaching //开启缓存注解
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}

@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
.addResourceLocations(Constants.SONG_PATH);
registry.addResourceHandler("/img/songListPic/**")
.addResourceLocations(Constants.SONGLIST_PIC_PATH);
registry.addResourceHandler("/img/swiper/**")
.addResourceLocations(Constants.BANNER_PIC_PATH);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Constants {
public static String SONG_PIC_PATH = "file:" + PROJECT_PATH + "/img/songPic/";
public static String SONG_PATH = "file:" + PROJECT_PATH + "/song/";
public static String SINGER_PIC_PATH = "file:" + PROJECT_PATH + "/img/singerPic/";
public static String BANNER_PIC_PATH = "file:" + PROJECT_PATH + "/img/swiper/";


/* 盐值加密 */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.yin.controller;

import com.example.yin.common.R;
import com.example.yin.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @Author 祝英台炸油条
* @Time : 2022/6/13 13:16
**/
@RestController
@RequestMapping("/banner")
public class BannerController {

@Autowired
private BannerService bannerService;

@GetMapping("/getAllBanner")
public R getAllBanner(){
return R.success("成功获取轮播图与",bannerService.getAllBanner());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.yin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.yin.model.domain.Banner;

/**
* @author asus
* @description 针对表【banner】的数据库操作Mapper
* @createDate 2022-06-13 13:13:42
* @Entity generator.domain.Banner
*/
public interface BannerMapper extends BaseMapper<Banner> {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.yin.model.domain;

import lombok.Data;

import java.io.Serializable;

/**
*
* @TableName banner
*/
@Data
public class Banner implements Serializable {
/**
*
*/
private Integer id;

/**
*
*/
private String pic;

private static final long serialVersionUID = 1L;

@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Banner other = (Banner) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getPic() == null ? other.getPic() == null : this.getPic().equals(other.getPic()));
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getPic() == null) ? 0 : getPic().hashCode());
return result;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", pic=").append(pic);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.yin.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.yin.model.domain.Banner;

import java.util.List;

/**
* @author asus
* @description 针对表【banner】的数据库操作Service
* @createDate 2022-06-13 13:13:42
*/
public interface BannerService extends IService<Banner> {

List<Banner> getAllBanner();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.yin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yin.mapper.BannerMapper;
import com.example.yin.model.domain.Banner;
import com.example.yin.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author asus
* @description 针对表【banner】的数据库操作Service实现
* @createDate 2022-06-13 13:13:42
*/
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner>
implements BannerService {

@Autowired
private BannerMapper bannerMapper;

@Cacheable(value = "banner", key = "'list'") //放在缓存中 redis 是以key-value进行存储的
@Override
public List<Banner> getAllBanner() {
System.out.println("没有走缓存");
return bannerMapper.selectList(null);
}
}
4 changes: 4 additions & 0 deletions music-server/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.datasource.url=jdbc:mysql://localhost:3306/tp_music?serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
4 changes: 4 additions & 0 deletions music-server/src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.datasource.url=jdbc:mysql://121.43.39.136:3306/zeng?serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=zyt
spring.datasource.password=zyt
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
29 changes: 12 additions & 17 deletions music-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
#spring.datasource.url=jdbc:mysql://localhost:3306/tp_music?serverTimezone=Asia/Shanghai&useSSL=false
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://121.43.39.136:3306/zeng?serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=zyt
spring.datasource.password=zyt
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.typeAliasesPackage=com.example.yin.model.domain
mybatis.mapperLocations=classpath:mapper/*.xml

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

server.port=8888

#热部署生效
spring.devtools.restart.enabled=false

#设置重启的目录
spring.devtools.restart.additional-paths=src/main/java

#classpath目录下的WEB-INF文件夹内容修改不重启
spring.devtools.restart.exclude=WEB-INF/**

# 关闭CONDITIONS EVALUATION REPORT及自动配置内容向控制台的输出
logging.level.org.springframework.boot.autoconfigure=ERROR
logging.level.org.springframework.boot.autoconfigure=ERROR
# redis相应的地址 还有一些配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
spring.profiles.active=dev
Loading

0 comments on commit 0e3b860

Please sign in to comment.