# zcache-spring-boot-starter
**Repository Path**: mirrors_gspandy/zcache-spring-boot-starter
## Basic Information
- **Project Name**: zcache-spring-boot-starter
- **Description**: 一款基于 caffeine 实现的简单分布式缓存。
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-09-24
- **Last Updated**: 2025-10-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# ZCache
一款基于 caffeine 实现的简单分布式缓存。
ZCache 缓存默认采取懒加载的模式,数据变更时直接失效缓存。
ZCache 基于 Redis 的 PubSub 特性,实现集群中其他服务本地缓存失效通知,
从而保证所有服务本地缓存的一致性。
## 使用方法
1. 下载本项目打开后,install 到本地或者 deploy 到 maven 私服
2. 需要使用 ZCache 的项目引入如下依赖,项目需同时依赖 spring-data-redis
```xml
pw.nullpointer
zcache-spring-boot-starter
0.0.1
org.springframework.boot
spring-boot-starter-data-redis
```
3. 配置文件开启 ZCache
```yaml
zcache:
enabled: true
```
4. 新增 AbstractCache 的实现类,覆写相应方法。
```java
@Component
public class UserCache extends AbstractCache {
private Cache cache = Caffeine.newBuilder()
.initialCapacity(1)
.maximumSize(500)
.expireAfterWrite(1, TimeUnit.HOURS)
.build();
@Resource
private UserMapper userMapper;
@Nonnull
@Override
protected Cache getCache() {
// 自定义 Cache 实例
return cache;
}
@Nonnull
@Override
protected Function super Long, ? extends User> getLoadFunction() {
// 自定义 Cache Loader
return key -> {
log.info("load cache from db. key: {}", key);
return userMapper.findById(key);
};
}
}
```
5. 使用
```java
@Resource
private UserCache userCache;
// cache中不存该key时,使用getLoadFunction中的方法加载数据
User user = userCache.get(userId);
// 通过Redis的发布订阅发布通知,接收到通知的服务会自动淘汰本地缓存
userCache.invalidate(userId);
```
## example
[https://github.com/Mosiki/ZCache-Example](https://github.com/Mosiki/ZCache-Example)
## 联系方式
QQ群号:967808880
邮箱:vcmq@foxmail.com