refactor(商城): 缓存相关优化

master
waynaqua 3 years ago
parent f99851d487
commit c12509dead

@ -29,22 +29,23 @@ public class BannerController extends BaseController {
@PostMapping
public R addBanner(@Validated @RequestBody Banner banner) {
banner.setCreateTime(new Date());
return R.result(iBannerService.save(banner));
return R.result(iBannerService.saveBanner(banner));
}
@PutMapping
public R updateBanner(@Validated @RequestBody Banner banner) {
banner.setUpdateTime(new Date());
boolean b = iBannerService.updateBannerById(banner);
return R.result(iBannerService.updateById(banner));
}
@GetMapping("{bannerId}")
public R getBanner(@PathVariable Long bannerId) {
return R.success().add("data", iBannerService.getById(bannerId));
return R.success().add("data", iBannerService.getBannerById(bannerId));
}
@DeleteMapping("{bannerIds}")
public R deleteBanner(@PathVariable List<Long> bannerIds) {
return R.result(iBannerService.removeByIds(bannerIds));
return R.result(iBannerService.removeBannerById(bannerIds));
}
}

@ -74,7 +74,7 @@ public class BaseController {
}
return tPage;
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return getPage(1, 10);
}
}

@ -6,12 +6,14 @@ import com.baomidou.mybatisplus.annotation.TableName;
import com.wayn.common.base.entity.ShopBaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
@Data
@TableName("shop_banner")
@ToString
@EqualsAndHashCode(callSuper = true)
public class Banner extends ShopBaseEntity implements Serializable {

@ -25,4 +25,36 @@ public interface IBannerService extends IService<Banner> {
* @return banner
*/
IPage<Banner> listPage(Page<Banner> page, Banner banner);
/**
* banner
*
* @param banner banner
* @return boolean
*/
boolean saveBanner(Banner banner);
/**
* bannerIdbanner
*
* @param banner banner
* @return boolean
*/
boolean updateBannerById(Banner banner);
/**
* IDbanner
*
* @param bannerId bannerId
* @return banner
*/
Banner getBannerById(Long bannerId);
/**
* bannerIdsbanner
*
* @param bannerIds bannerIds
* @return boolean
*/
boolean removeBannerById(List<Long> bannerIds);
}

@ -7,6 +7,8 @@ import com.wayn.common.core.domain.shop.Banner;
import com.wayn.common.core.mapper.shop.BannerMapper;
import com.wayn.common.core.service.shop.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@ -22,8 +24,33 @@ public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> impleme
return bannerMapper.selectBannerList(banner);
}
@Cacheable(value = "bannerCache", key = "#root.method + '_' + #page.size + ':' + #page.current + '_' + #banner + ']'")
@Override
public IPage<Banner> listPage(Page<Banner> page, Banner banner) {
return bannerMapper.selectBannerListPage(page, banner);
}
@CacheEvict(value = "bannerCache", allEntries = true)
@Override
public boolean saveBanner(Banner banner) {
return save(banner);
}
@CacheEvict(value = "bannerCache", allEntries = true)
@Override
public boolean updateBannerById(Banner banner) {
return updateById(banner);
}
@Cacheable(value = "bannerCache", key = "#root.method + '_' + #bannerId")
@Override
public Banner getBannerById(Long bannerId) {
return getById(bannerId);
}
@CacheEvict(value = "bannerCache", allEntries = true)
@Override
public boolean removeBannerById(List<Long> bannerIds) {
return removeByIds(bannerIds);
}
}

@ -66,7 +66,7 @@ public class CacheConfig extends CachingConfigurerSupport {
private RedisCacheConfiguration defaultCacheConfig() {
return RedisCacheConfiguration.defaultCacheConfig()
.prefixCacheNameWith("redis:cache:")
.prefixCacheNameWith("redis:cache")
.entryTtl(Duration.ofSeconds(expire))
.disableCachingNullValues();
}

@ -1,80 +0,0 @@
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://127.0.0.1:3306/wayn_shop?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
# 初始连接数
initialSize: 10
# 最小连接池数量
minIdle: 20
# 最大连接池数量
maxActive: 30
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-password:
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
redis:
host: 127.0.0.1
port: 6379
password:
database: 0
timeout: 5000
expire: 400
# 配置rabbitMq 服务器
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# 消息确认配置项
# 确认消息已发送到交换机(Exchange)
publisher-confirm-type: correlated
# 确认消息已发送到队列(Queue)
publisher-returns: true
es:
host: 127.0.0.1
port: 9200
scheme: http
Loading…
Cancel
Save