ProjectInfoCache建立缓存基准

master
Jiang Peng 2024-03-27 20:33:47 +08:00
parent 9e739fcfd2
commit d2549b730d
13 changed files with 336 additions and 0 deletions

View File

@ -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>

View File

@ -0,0 +1,57 @@
package com.muyu.common.cache;
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);
/**
* Keyvalue
* @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);
}

View File

@ -0,0 +1,91 @@
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;
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;
}
/**
* Keyvalue
* @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();
}

View File

@ -18,6 +18,7 @@
<module>muyu-common-datascope</module>
<module>muyu-common-datasource</module>
<module>muyu-common-system</module>
<module>muyu-common-cache</module>
</modules>
<artifactId>muyu-common</artifactId>

View File

@ -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>

View File

@ -0,0 +1,51 @@
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;
@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();
}
}

View File

@ -0,0 +1,14 @@
package com.muyu.product.cache;
import com.muyu.product.domain.ProjectInfo;
public interface ProjectInfoData {
/**
*
* @param key ID
* @return
*/
public ProjectInfo getData (Long key);
}

View File

@ -8,4 +8,5 @@ package com.muyu.product.domain.base;
public interface CategoryBase {
public Long getBaseId();
}

View File

@ -17,6 +17,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 商品模块 common 依赖 -->
<dependency>
@ -24,6 +25,12 @@
<artifactId>muyu-product-common</artifactId>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-product-cache</artifactId>
<version>3.6.3</version>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>

View File

@ -0,0 +1,24 @@
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;
@Service
public class ProjectInfoDataImpl implements ProjectInfoData {
@Autowired
private ProjectInfoService projectInfoService;
/**
*
* @param key ID
* @return
*/
@Override
public ProjectInfo getData(Long key) {
return projectInfoService.getById(key);
}
}

View File

@ -4,6 +4,7 @@ import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.muyu.product.cache.ProjectInfoCache;
import com.muyu.product.domain.AsProductAttributeInfo;
import com.muyu.product.domain.ProjectSkuInfo;
import com.muyu.product.domain.model.CategoryInfoSaveModel;
@ -53,6 +54,9 @@ public class ProjectInfoController extends BaseController {
@Autowired
private ProjectSkuInfoService projectSkuInfoService;
@Autowired
private ProjectInfoCache projectInfoCache;
/**
*
*/
@ -77,6 +81,7 @@ public class ProjectInfoController extends BaseController {
ExcelUtil<ProjectInfo> util = new ExcelUtil<ProjectInfo>(ProjectInfo.class);
util.exportExcel(response, list, "商品信息数据");
}
/**
*
*/
@ -88,6 +93,17 @@ public class ProjectInfoController extends BaseController {
return Result.success(projectInfoService.getDetailInfo(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));
}
/**
*
*/

View File

@ -15,6 +15,7 @@
<module>muyu-product-common</module>
<module>muyu-product-remote</module>
<module>muyu-product-server</module>
<module>muyu-product-cache</module>
</modules>
<properties>

View File

@ -143,6 +143,13 @@
<version>${transmittable-thread-local.version}</version>
</dependency>
<!-- 缓存基准模块 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-common-cache</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- 核心模块 -->
<dependency>
<groupId>com.muyu</groupId>