commit 2f552baa5fee6eadc05d0595f3339c8e36de6b1b Author: yangzhaohan Date: Mon Jun 22 15:52:26 2026 +0800 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2c4099e --- /dev/null +++ b/README.md @@ -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 本地缓存变体 +``` diff --git a/ai/conventions.md b/ai/conventions.md new file mode 100644 index 0000000..e51252d --- /dev/null +++ b/ai/conventions.md @@ -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 redisTemplate; + +// 裸字符串拼接 Key +String key = "user:" + userId; +User user = (User) redisTemplate.opsForValue().get(key); +``` diff --git a/ai/module.yaml b/ai/module.yaml new file mode 100644 index 0000000..997d38b --- /dev/null +++ b/ai/module.yaml @@ -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 构造器。 diff --git a/manifest.yaml b/manifest.yaml new file mode 100644 index 0000000..19f1ed1 --- /dev/null +++ b/manifest.yaml @@ -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 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..06d9823 --- /dev/null +++ b/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com.synoth.brick + brick-common-cache + 0.0.0-SNAPSHOT + jar + + + org.springframework.boot + spring-boot-starter-parent + 3.4.0 + + + + + 17 + + + + + com.synoth.brick + brick-common-core + 0.0.0-SNAPSHOT + + + + diff --git a/src/main/java/com/synoth/brick/cache/CacheKey.java b/src/main/java/com/synoth/brick/cache/CacheKey.java new file mode 100644 index 0000000..375815d --- /dev/null +++ b/src/main/java/com/synoth/brick/cache/CacheKey.java @@ -0,0 +1,13 @@ +package com.synoth.brick.cache; + +/** + * 缓存 Key 构造器,禁止裸字符串拼接。 + */ +public final class CacheKey { + + private CacheKey() { + } + + // TODO: 提供静态工厂方法构造带命名空间的缓存 Key + +} diff --git a/src/main/java/com/synoth/brick/cache/CacheService.java b/src/main/java/com/synoth/brick/cache/CacheService.java new file mode 100644 index 0000000..3739877 --- /dev/null +++ b/src/main/java/com/synoth/brick/cache/CacheService.java @@ -0,0 +1,10 @@ +package com.synoth.brick.cache; + +/** + * 统一缓存访问入口,屏蔽底层缓存实现。 + */ +public interface CacheService { + + // TODO: get / set / delete / exists 等方法签名 + +} diff --git a/template/pom.xml b/template/pom.xml new file mode 100644 index 0000000..9fdf1e9 --- /dev/null +++ b/template/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + {{groupId}} + {{projectName}}-common-cache + 0.0.0 + jar + + + org.springframework.boot + spring-boot-starter-parent + 3.4.0 + + + + + {{javaVersion}} + + + + + {{groupId}} + {{projectName}}-common-core + 0.0.0 + + + org.springframework.boot + spring-boot-starter + + + + diff --git a/template/src/main/java/{{basePkgPath}}/cache/CacheKey.java b/template/src/main/java/{{basePkgPath}}/cache/CacheKey.java new file mode 100644 index 0000000..0379f51 --- /dev/null +++ b/template/src/main/java/{{basePkgPath}}/cache/CacheKey.java @@ -0,0 +1,13 @@ +package {{basePackage}}.cache; + +/** + * 缓存 Key 构造器,禁止裸字符串拼接。 + */ +public final class CacheKey { + + private CacheKey() { + } + + // TODO: 提供静态工厂方法构造带命名空间的缓存 Key + +} diff --git a/template/src/main/java/{{basePkgPath}}/cache/CacheService.java b/template/src/main/java/{{basePkgPath}}/cache/CacheService.java new file mode 100644 index 0000000..b2e8628 --- /dev/null +++ b/template/src/main/java/{{basePkgPath}}/cache/CacheService.java @@ -0,0 +1,10 @@ +package {{basePackage}}.cache; + +/** + * 统一缓存访问入口,屏蔽底层缓存实现。 + */ +public interface CacheService { + + // TODO: get / set / delete / exists 等方法签名 + +}