# aiwiown-spring-cache **Repository Path**: dbin0123/aiwiown-spring-cache ## Basic Information - **Project Name**: aiwiown-spring-cache - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-08-02 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # aiwiown-spring-cache #### 介绍 aiwiown-cache 缓存框架 支持本地ehcache缓存,redis缓存 开箱即用! #### 使用说明 1. 导入依赖 maven ``` com.aiwiown aiwiown-spring-cache 1.0.2-2.0.1 ``` 2. 添加配置文件 ``` aiwiown: cache: # 缓存仓库 local(本地ehcache),remote(远程redis),local_remote store: local_remote # 项目名称 prod-name: demo # 远程缓存 redis: # redis密码 password: rds731wanda # redis host node: "10.1.64.79:9101,10.1.64.79:9102,10.1.64.79:9103,10.1.64.79:9104,10.1.64.79:9105,10.1.64.79:9106" # redis 库,集群环境配置无效 database: 1 # 本地缓存 ehcache: # 缓存存放目录 disk-path: D://ehcache/data store-cfg: ehcache.cfg ``` 3. 本地缓存配置 ``` 新增ehcache.cfg 配置文件 新增本地缓存配置信息 aiwiown: ehcache: store: PUB_01: maxEntriesLocalHeap: 10000 eternal: false diskSpoolBufferSizeMB: 30 maxEntriesLocalDisk: 10000000 diskExpiryThreadIntervalSeconds: 120 memoryStoreEvictionPolicy: LRU persistence: strategy: localTempSwap PUB: maxEntriesLocalHeap: 10000 eternal: false diskSpoolBufferSizeMB: 30 maxEntriesLocalDisk: 10000000 diskExpiryThreadIntervalSeconds: 120 memoryStoreEvictionPolicy: LRU persistence: strategy: localTempSwap ``` 3. 启动类添加启用缓存注解 ``` @EnableAiwiownCache ``` 4. 具体缓存注解 | 注解 | 描述 | | ---- | ---- | | @EnableAiwiownCache| 开启缓存| | @AiwiownCache | 指定方法添加注解缓存返回值 | | @AiwiownCacheKey | 指定缓存KEY | 5. @AiwiownCache ``` key 缓存前缀key storeName 缓存空间 store 缓存存储空间 本地,远程 expire 缓存过期时间(单位s), -1删除缓存, 0缓存不过期, >0 过期时间 ``` 6. @AiwiownCacheKey ``` value 缓存具体key 对象使用"#对象.属性"取值, 不支持拼接字符串 ``` 7. 示例代码 eg1: 一般缓存key ```$xslt @Override @AiwiownCache(store = CacheStore.remote,key = "cache0") public String cache0(@AiwiownCacheKey String cache) { System.out.println("======================="); return cache; } ``` eg2: 设置过期时间 ```$xslt @Override @AiwiownCache(store = CacheStore.remote,key = "cache1",expire = 60) public String cache1(@AiwiownCacheKey String cache) { System.out.println("======================="); return cache; } ``` eg3: 根据对象属性值作为缓存key ```$xslt @Override @AiwiownCache(store = CacheStore.remote,key = "cache-user",expire = 60000) public User cacheUser1(@AiwiownCacheKey("#user.id")User user) { System.out.println(user.toString()); return user; } ``` eg4: 删除缓存 ```$xslt @Override @AiwiownCache(store = CacheStore.remote,key = "cache0",expire = -1) public void delete(@AiwiownCacheKey String cache) { System.out.println("==========删除缓存============="); } ```