parent
4e8922da81
commit
7b38972c73
|
@ -0,0 +1,31 @@
|
||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-common</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>muyu-common-cache</artifactId>
|
||||||
|
|
||||||
|
<description>
|
||||||
|
muyu-common-cache缓存基准
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- common 缓存 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-common-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.muyu.common.cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 缓存接口基类
|
||||||
|
* @Date 2024-3-26 下午 03:25
|
||||||
|
*/
|
||||||
|
public interface Cache <K, V> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key前缀
|
||||||
|
* @return key前缀
|
||||||
|
*/
|
||||||
|
public String keyPre();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
* @param key ID
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
public String encode(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解码
|
||||||
|
* @param redisKey 数据库键
|
||||||
|
* @return ID
|
||||||
|
*/
|
||||||
|
public K decode(String redisKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Key获取value值
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
public V get(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存添加/修改
|
||||||
|
* @param key 键
|
||||||
|
* @param value 值
|
||||||
|
*/
|
||||||
|
public void put(K key, V value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键删除
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public void remove(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存时间
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public void refreshTime (K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存数据
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public void refreshData (K key);
|
||||||
|
}
|
|
@ -0,0 +1,96 @@
|
||||||
|
package com.muyu.common.cache.abs;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.Cache;
|
||||||
|
import com.muyu.common.redis.service.RedisService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 缓存抽象类
|
||||||
|
* @Date 2024-3-27 下午 03:10
|
||||||
|
*/
|
||||||
|
public abstract class CacheAbs<K, V> implements Cache<K, V> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
* @param key ID
|
||||||
|
* @return 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encode (K key) {
|
||||||
|
return keyPre() + key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Key获取value值
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public V get (K key) {
|
||||||
|
V value = redisService.getCacheObject(encode(key));
|
||||||
|
if (value == null){
|
||||||
|
value = getData(key);
|
||||||
|
if (value == null){
|
||||||
|
value = defaultValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.put(key, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存添加/修改
|
||||||
|
* @param key 键
|
||||||
|
* @param value 值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void put (K key, V value) {
|
||||||
|
this.redisService.setCacheObject(encode(key), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过键删除
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void remove (K key) {
|
||||||
|
this.redisService.deleteObject(encode(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void refreshTime (K key) {
|
||||||
|
this.redisService.expire(encode(key), 60, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存数据
|
||||||
|
*
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void refreshData (K key) {
|
||||||
|
this.put(key, getData(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从数据库获取数据
|
||||||
|
* @param key ID
|
||||||
|
* @return 缓存对象
|
||||||
|
*/
|
||||||
|
public abstract V getData(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认值
|
||||||
|
*/
|
||||||
|
public abstract V defaultValue();
|
||||||
|
}
|
|
@ -18,6 +18,7 @@
|
||||||
<module>muyu-common-datascope</module>
|
<module>muyu-common-datascope</module>
|
||||||
<module>muyu-common-datasource</module>
|
<module>muyu-common-datasource</module>
|
||||||
<module>muyu-common-system</module>
|
<module>muyu-common-system</module>
|
||||||
|
<module>muyu-common-cache</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<artifactId>muyu-common</artifactId>
|
<artifactId>muyu-common</artifactId>
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-product</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>muyu-product-cache</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 缓存基准模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-common-cache</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 商品模块 common 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-product-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,61 @@
|
||||||
|
package com.muyu.product.cache;
|
||||||
|
|
||||||
|
import com.muyu.common.cache.abs.CacheAbs;
|
||||||
|
import com.muyu.common.core.text.Convert;
|
||||||
|
import com.muyu.product.domain.ProjectInfo;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 商品缓存
|
||||||
|
* @Date 2024-3-27 下午 03:30
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ProjectInfoCache extends CacheAbs<Long, ProjectInfo> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProjectInfoData projectInfoData;
|
||||||
|
/**
|
||||||
|
* key前缀
|
||||||
|
*
|
||||||
|
* @return key前缀
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String keyPre () {
|
||||||
|
return "project:info:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解码
|
||||||
|
*
|
||||||
|
* @param redisKey 数据库键
|
||||||
|
*
|
||||||
|
* @return ID
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Long decode (String redisKey) {
|
||||||
|
return Convert.toLong(redisKey.replace(keyPre(),""), 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从数据库获取数据
|
||||||
|
*
|
||||||
|
* @param key ID
|
||||||
|
*
|
||||||
|
* @return 缓存对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ProjectInfo getData (Long key) {
|
||||||
|
return projectInfoData.getData(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ProjectInfo defaultValue () {
|
||||||
|
return new ProjectInfo();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.muyu.product.cache;
|
||||||
|
|
||||||
|
import com.muyu.product.domain.ProjectInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 缓存数据获取
|
||||||
|
* @Date 2024-3-27 下午 03:34
|
||||||
|
*/
|
||||||
|
public interface ProjectInfoData {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从数据库获取数据
|
||||||
|
* @param key ID
|
||||||
|
* @return 缓存对象
|
||||||
|
*/
|
||||||
|
public ProjectInfo getData (Long key);
|
||||||
|
}
|
|
@ -18,6 +18,12 @@
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- 商品模块 缓存 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-product-cache</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 商品模块 common 依赖 -->
|
<!-- 商品模块 common 依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.muyu.product.cache.impl;
|
||||||
|
|
||||||
|
import com.muyu.product.cache.ProjectInfoData;
|
||||||
|
import com.muyu.product.domain.ProjectInfo;
|
||||||
|
import com.muyu.product.service.ProjectInfoService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 缓存数据获取
|
||||||
|
* @Date 2024-3-27 下午 03:37
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ProjectInfoDataImpl implements ProjectInfoData {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProjectInfoService projectInfoService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从数据库获取数据
|
||||||
|
* @param key ID
|
||||||
|
* @return 缓存对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ProjectInfo getData (Long key) {
|
||||||
|
return projectInfoService.getById(key);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package com.muyu.product.controller;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import com.muyu.product.cache.ProjectInfoCache;
|
||||||
import com.muyu.product.domain.resp.ProjectDetailResp;
|
import com.muyu.product.domain.resp.ProjectDetailResp;
|
||||||
import io.swagger.annotations.*;
|
import io.swagger.annotations.*;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -40,6 +41,9 @@ public class ProjectInfoController extends BaseController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProjectInfoService projectInfoService;
|
private ProjectInfoService projectInfoService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProjectInfoCache projectInfoCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询商品信息列表
|
* 查询商品信息列表
|
||||||
*/
|
*/
|
||||||
|
@ -73,6 +77,17 @@ public class ProjectInfoController extends BaseController {
|
||||||
@GetMapping(value = "/{id}")
|
@GetMapping(value = "/{id}")
|
||||||
@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
|
@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
|
||||||
public Result<ProjectInfo> getInfo(@PathVariable("id") Long id) {
|
public Result<ProjectInfo> getInfo(@PathVariable("id") Long id) {
|
||||||
|
return Result.success(projectInfoCache.get(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取商品信息详细信息
|
||||||
|
*/
|
||||||
|
@ApiOperation("获取商品信息详细信息")
|
||||||
|
@RequiresPermissions("product:info:query")
|
||||||
|
@GetMapping(value = "/cache/{id}")
|
||||||
|
@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
|
||||||
|
public Result<ProjectInfo> getCacheInfo(@PathVariable("id") Long id) {
|
||||||
return Result.success(projectInfoService.getById(id));
|
return Result.success(projectInfoService.getById(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
<module>muyu-product-common</module>
|
<module>muyu-product-common</module>
|
||||||
<module>muyu-product-remote</module>
|
<module>muyu-product-remote</module>
|
||||||
<module>muyu-product-server</module>
|
<module>muyu-product-server</module>
|
||||||
|
<module>muyu-product-cache</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
14
pom.xml
14
pom.xml
|
@ -150,6 +150,13 @@
|
||||||
<version>${muyu.version}</version>
|
<version>${muyu.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 缓存基准模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-common-cache</artifactId>
|
||||||
|
<version>${muyu.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 接口模块 -->
|
<!-- 接口模块 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
|
@ -220,6 +227,13 @@
|
||||||
<version>${muyu.version}</version>
|
<version>${muyu.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 商品模块 缓存 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-product-cache</artifactId>
|
||||||
|
<version>${muyu.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue