first commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
target/
|
||||
@@ -0,0 +1,21 @@
|
||||
# brick-common-cache
|
||||
|
||||
缓存模块 —— 提供统一的 CacheService 抽象,屏蔽底层缓存实现。
|
||||
|
||||
## 变体
|
||||
|
||||
| 分支 | 说明 |
|
||||
|------|------|
|
||||
| `master` | 主分支 |
|
||||
| `java-caffeine` | Caffeine 本地缓存 |
|
||||
|
||||
## 依赖
|
||||
|
||||
- `brick-common-core`
|
||||
|
||||
## 使用
|
||||
|
||||
```bash
|
||||
brick add cache # 默认变体 (master)
|
||||
brick add cache:caffeine # Caffeine 本地缓存变体
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
# brick-common-cache 编码规范
|
||||
|
||||
## 职责边界
|
||||
|
||||
cache 模块提供统一的缓存抽象,业务代码只与 `CacheService` 接口交互,不感知底层实现。
|
||||
|
||||
## 使用准则
|
||||
|
||||
- 所有缓存操作通过 `CacheService`,不得直接注入 Redis/Redisson/Caffeine 客户端。
|
||||
- 缓存 Key 统一使用 `CacheKey` 构造,禁止裸字符串拼接。
|
||||
- 缓存是可重建的临时数据,不可替代数据库持久化。
|
||||
|
||||
## 正例
|
||||
|
||||
```java
|
||||
@Autowired
|
||||
private CacheService cacheService;
|
||||
|
||||
CacheKey key = CacheKey.of("user", userId);
|
||||
User user = cacheService.get(key, User.class);
|
||||
```
|
||||
|
||||
## 反例
|
||||
|
||||
```java
|
||||
// 直接注入底层客户端
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
// 裸字符串拼接 Key
|
||||
String key = "user:" + userId;
|
||||
User user = (User) redisTemplate.opsForValue().get(key);
|
||||
```
|
||||
@@ -0,0 +1,13 @@
|
||||
module: cache
|
||||
summary: 统一缓存访问层,屏蔽具体缓存实现。
|
||||
dos:
|
||||
- 所有缓存读写必须通过 CacheService。
|
||||
- 缓存 key 统一通过 CacheKey 构造,禁止裸字符串拼接。
|
||||
donts:
|
||||
- 不要在业务代码里直接注入 RedisTemplate / RedissonClient。
|
||||
- 不要把缓存当作数据库,缓存必须可重建。
|
||||
examples:
|
||||
- path: src/main/java/{{basePkgPath}}/cache/CacheService.java
|
||||
note: 缓存访问入口。
|
||||
- path: src/main/java/{{basePkgPath}}/cache/CacheKey.java
|
||||
note: 缓存 Key 构造器。
|
||||
@@ -0,0 +1,25 @@
|
||||
name: brick-common-cache
|
||||
version: 0.0.0
|
||||
description: 缓存模块,提供统一的 CacheService 抽象,屏蔽具体缓存实现
|
||||
language: java
|
||||
defaultBranch: master
|
||||
module: cache
|
||||
|
||||
variants:
|
||||
- branch: master
|
||||
label: master
|
||||
- branch: java-caffeine
|
||||
label: Caffeine 本地缓存实现
|
||||
|
||||
variables:
|
||||
- name: basePackage
|
||||
required: true
|
||||
- name: projectName
|
||||
required: true
|
||||
- name: javaVersion
|
||||
required: true
|
||||
- name: groupId
|
||||
required: true
|
||||
|
||||
dependsOn:
|
||||
- brick-common-core
|
||||
@@ -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>
|
||||
|
||||
<groupId>com.synoth.brick</groupId>
|
||||
<artifactId>brick-common-cache</artifactId>
|
||||
<version>0.0.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.4.0</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.synoth.brick</groupId>
|
||||
<artifactId>brick-common-core</artifactId>
|
||||
<version>0.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.synoth.brick.cache;
|
||||
|
||||
/**
|
||||
* 缓存 Key 构造器,禁止裸字符串拼接。
|
||||
*/
|
||||
public final class CacheKey {
|
||||
|
||||
private CacheKey() {
|
||||
}
|
||||
|
||||
// TODO: 提供静态工厂方法构造带命名空间的缓存 Key
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.synoth.brick.cache;
|
||||
|
||||
/**
|
||||
* 统一缓存访问入口,屏蔽底层缓存实现。
|
||||
*/
|
||||
public interface CacheService {
|
||||
|
||||
// TODO: get / set / delete / exists 等方法签名
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
<groupId>{{groupId}}</groupId>
|
||||
<artifactId>{{projectName}}-common-cache</artifactId>
|
||||
<version>0.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.4.0</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>{{javaVersion}}</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>{{groupId}}</groupId>
|
||||
<artifactId>{{projectName}}-common-core</artifactId>
|
||||
<version>0.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package {{basePackage}}.cache;
|
||||
|
||||
/**
|
||||
* 缓存 Key 构造器,禁止裸字符串拼接。
|
||||
*/
|
||||
public final class CacheKey {
|
||||
|
||||
private CacheKey() {
|
||||
}
|
||||
|
||||
// TODO: 提供静态工厂方法构造带命名空间的缓存 Key
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package {{basePackage}}.cache;
|
||||
|
||||
/**
|
||||
* 统一缓存访问入口,屏蔽底层缓存实现。
|
||||
*/
|
||||
public interface CacheService {
|
||||
|
||||
// TODO: get / set / delete / exists 等方法签名
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user