Skip to content

Commit cf0d560

Browse files
author
liujun
committed
添加了redis支持
1 parent 1eef1b0 commit cf0d560

File tree

12 files changed

+117
-13
lines changed

12 files changed

+117
-13
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package biz.llmall.commodity.config;
2+
import org.springframework.cache.CacheManager;
3+
import org.springframework.cache.annotation.CachingConfigurerSupport;
4+
import org.springframework.cache.annotation.EnableCaching;
5+
import org.springframework.cache.interceptor.KeyGenerator;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.data.redis.cache.RedisCacheManager;
9+
import org.springframework.data.redis.core.RedisTemplate;
10+
11+
import java.util.Arrays;
12+
13+
@Configuration
14+
@EnableCaching
15+
public class RedisConfig extends CachingConfigurerSupport {
16+
private RedisTemplate redisTemplate;
17+
18+
public RedisConfig(RedisTemplate redisTemplate) {
19+
this.redisTemplate = redisTemplate;
20+
}
21+
22+
@Bean
23+
@Override
24+
public KeyGenerator keyGenerator() {
25+
return (target, method, params) -> {
26+
StringBuilder stringBuilder = new StringBuilder();
27+
stringBuilder.append(target.getClass().getName());
28+
stringBuilder.append(method.getName());
29+
Arrays.stream(params).forEach(param -> stringBuilder.append(param.toString()));
30+
return stringBuilder.toString();
31+
};
32+
}
33+
34+
@Bean
35+
@Override
36+
public CacheManager cacheManager() {
37+
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
38+
redisCacheManager.setDefaultExpiration(24 * 3600);
39+
return redisCacheManager;
40+
}
41+
}

llmall-commodity/src/main/java/biz/llmall/commodity/controller/CarouselController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import biz.llmall.common.dto.response.EnumStatus;
55
import biz.llmall.common.entity.carousel.Carousel;
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.PathVariable;
78
import org.springframework.web.bind.annotation.RequestMapping;
89
import org.springframework.web.bind.annotation.RequestMethod;
910
import org.springframework.web.bind.annotation.RestController;
@@ -22,8 +23,18 @@ public APIResponse<List<Carousel>> findCarousels() {
2223
try {
2324
List<Carousel> carousels = carouselService.findCarousels(new Date());
2425
return APIResponse.success(carousels);
25-
} catch (Exception cex) {
26-
return APIResponse.error(EnumStatus.SERVICE_ERROR, cex.getMessage());
26+
} catch (Exception ex) {
27+
return APIResponse.error(EnumStatus.SERVICE_ERROR, ex.getMessage());
28+
}
29+
}
30+
31+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
32+
public APIResponse<Carousel> getCarouselById(@PathVariable("id") Long id) {
33+
try {
34+
Carousel carousel = carouselService.findCarouselById(id);
35+
return APIResponse.success(carousel);
36+
} catch (Exception ex) {
37+
return APIResponse.error(EnumStatus.SERVICE_ERROR, ex.getMessage());
2738
}
2839
}
2940
}

llmall-commodity/src/main/java/biz/llmall/commodity/model/service/ICarouselService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
public interface ICarouselService {
99
List<Carousel> findCarousels(Date currentDatetime);
10+
Carousel findCarouselById(Long id);
1011
}

llmall-commodity/src/main/java/biz/llmall/commodity/model/service/impl/BrandService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import biz.llmall.commodity.model.service.IBrandService;
44
import biz.llmall.common.entity.commodity.Brand;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.annotation.Cacheable;
67
import org.springframework.stereotype.Service;
78

89
import java.util.List;
@@ -13,11 +14,13 @@ public class BrandService implements IBrandService {
1314
private BrandMapper brandMapper;
1415

1516
@Override
17+
@Cacheable("findBrands")
1618
public List<Brand> findBrands() {
1719
return brandMapper.findBrands();
1820
}
1921

2022
@Override
23+
@Cacheable("findBrandById")
2124
public Brand findBrandById(Long id) {
2225
return brandMapper.selectBrandByPrimaryKey(id);
2326
}

llmall-commodity/src/main/java/biz/llmall/commodity/model/service/impl/CarouselService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import biz.llmall.commodity.model.service.ICarouselService;
44
import biz.llmall.common.entity.carousel.Carousel;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.annotation.Cacheable;
67
import org.springframework.stereotype.Service;
78

89
import java.util.Date;
@@ -14,7 +15,14 @@ public class CarouselService implements ICarouselService {
1415
private CarouselMapper carouselMapper;
1516

1617
@Override
18+
@Cacheable("findCarousels")
1719
public List<Carousel> findCarousels(Date currentDatetime) {
1820
return carouselMapper.findCarousels(currentDatetime);
1921
}
22+
23+
@Override
24+
@Cacheable("findCarouselById")
25+
public Carousel findCarouselById(Long id) {
26+
return carouselMapper.selectCarouselByPrimaryKey(id);
27+
}
2028
}

llmall-commodity/src/main/java/biz/llmall/commodity/model/service/impl/CategoryService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import biz.llmall.commodity.model.service.ICategoryService;
44
import biz.llmall.common.entity.commodity.Category;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.annotation.Cacheable;
67
import org.springframework.stereotype.Service;
78

89
import java.util.List;
@@ -12,11 +13,13 @@ public class CategoryService implements ICategoryService {
1213
@Autowired private CategoryMapper categoryMapper;
1314

1415
@Override
16+
@Cacheable("getCategories")
1517
public List<Category> getCategories() {
1618
return categoryMapper.findCategories();
1719
}
1820

1921
@Override
22+
@Cacheable("findCategoryById")
2023
public Category findCategoryById(Long id) {
2124
return categoryMapper.selectCategoryByPrimaryKey(id);
2225
}

llmall-commodity/src/main/java/biz/llmall/commodity/model/service/impl/CommodityService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import biz.llmall.commodity.model.service.ICommodityService;
44
import biz.llmall.common.entity.commodity.Commodity;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.annotation.Cacheable;
67
import org.springframework.stereotype.Service;
78

89
import java.util.List;
@@ -13,11 +14,13 @@ public class CommodityService implements ICommodityService {
1314
private CommodityMapper commodityMapper;
1415

1516
@Override
17+
@Cacheable("findCommoditiesTopX")
1618
public List<Commodity> findCommoditiesTopX(int max) {
1719
return commodityMapper.findCommoditiesTopX(max);
1820
}
1921

2022
@Override
23+
@Cacheable("findCommodityByPrimaryId")
2124
public Commodity findCommodityByPrimaryId(Long id) {
2225
return commodityMapper.selectCommodityByPrimaryKey(id);
2326
}

llmall-commodity/src/main/resources/application.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ spring:
1111
auto: true
1212
devtools:
1313
livereload:
14-
enabled: true
14+
enabled: false
1515
restart:
1616
exclude: static/**
17+
enabled: false
18+
remote:
19+
restart:
20+
enabled: false
1721
http:
1822
encoding:
1923
charset: UTF-8
@@ -31,7 +35,6 @@ mybatis:
3135
cache-enabled: true
3236

3337
logging:
34-
path: /var/log/llmall
3538
config: classpath:logback-spring.xml
3639

3740
#swagger config
@@ -63,4 +66,6 @@ spring:
6366
host: 127.0.0.1
6467
port: 6379
6568
password: ge*u0Oj9X
69+
database: 0
70+
6671
---

llmall-commodity/src/main/resources/mappers/CarouselMapper.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<sql id="Carousel_Base_Column_List">
2020
id, start_time, end_time, round_at_last, auto, `order`, create_time, update_time
2121
</sql>
22-
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="CarouselBaseResultMap">
22+
<select id="selectCarouselByPrimaryKey" parameterType="java.lang.Long" resultMap="CarouselBaseResultMap">
2323
select
2424
<include refid="Carousel_Base_Column_List"/>
2525
from carousel
@@ -46,7 +46,7 @@
4646
from carousel_image
4747
where carousel_id = #{id,jdbcType=BIGINT}
4848
</select>
49-
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
49+
<delete id="deleteCarouselByPrimaryKey" parameterType="java.lang.Long">
5050
delete from carousel
5151
where id = #{id,jdbcType=BIGINT}
5252
</delete>
@@ -58,7 +58,7 @@
5858
#{roundAtLast,jdbcType=BIT}, #{auto,jdbcType=BIT},#{order,jdbcType=BIT}, #{createTime,jdbcType=TIMESTAMP},
5959
#{updateTime,jdbcType=TIMESTAMP})
6060
</insert>
61-
<update id="updateByPrimaryKey" parameterType="biz.llmall.common.entity.carousel.Carousel">
61+
<update id="updateCarouselByPrimaryKey" parameterType="biz.llmall.common.entity.carousel.Carousel">
6262
update carousel
6363
set start_time = #{startTime,jdbcType=TIMESTAMP},
6464
end_time = #{endTime,jdbcType=TIMESTAMP},

llmall-commodity/src/main/resources/mappers/CategoryMapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@
5656
<select id="findCategories" resultMap="CategoryMainMap">
5757
select * from category t1
5858
where 1=1
59-
and t1.parent_id = 0;
59+
and t1.parent_id = 0
6060
</select>
6161
</mapper>

0 commit comments

Comments
 (0)