projectInfoCache

建立缓存基准
master
DongZeLiang 2024-03-27 16:22:23 +08:00
parent 4e8922da81
commit 7b38972c73
12 changed files with 369 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,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);
/**
* 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,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;
}
/**
* 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-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>

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,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();
}
}

View File

@ -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);
}

View File

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

View File

@ -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);
}
}

View File

@ -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));
} }

View File

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

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