1 Star 1 Fork 23

何成 / aspect-cache

forked from gosalelab / aspect-cache 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

License JDK 1.8

Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持更多缓存方式。 缓存key使用Spring表达式(SpEL)解析生成。

使用

1.引入包

<dependency>
    <artifactId>aspect-cache</artifactId>
    <groupId>com.gosalelab</groupId>
    <version>1.0.0</version>
</dependency>

2. 在Spring Boot启动类上使用如下注解

@EnableAspectCache

3. 添加配置项,确认开启缓存和具体缓存方式

com.gosalelab.cache.enable=true

com.gosalelab.cache.provider=eh

com.gosalelab.cache.provider=redis

配置项说明

名称 配置项 数据类型 可选值 默认值 必填 说明
com.gosalelab.cache enable boolean true | false false Y 是否启用Aspect Cache
com.gosalelab.cache provider String eh | redis eh Y 默认使用EHCache,如果要使用redis,则改为redis即可
com.gosalelab.cache expire-time int / 3600秒(半小时) N 全局缓存时间
com.gosalelab.cache key-generator String / default N 默认缓存key生成类,可以通过扩展KeyGenerator接口,使用自定义类,具体扩展方法见“自定义缓存key生成类”描述
com.gosalelab.cache.ehcache default-cache-name String / ehcache_cache N EHCache缓存名称
com.gosalelab.cache.ehcache disk int / 200 N 可使用磁盘持久化多大,单位为:MB
com.gosalelab.cache.ehcache ehcache-file-name String / ehcache.xml N EHCache外部配置文件名,使用此配置项需要将com.gosalelab.cache.ehcache.use-xml-file-config设置为true
com.gosalelab.cache.ehcache max-entries-local-heap int / 1000 N 堆资源池可存储条目数量
com.gosalelab.cache.ehcache off-heap int / 20 N 非堆资源池存储大小,单位为:MB
com.gosalelab.cache.ehcache use-xml-file-config boolean true | false false N 是否使用xml配置文件
com.gosalelab.cache.redis-cache database int / 0 N 缓存存在redis哪一个数据库
com.gosalelab.cache.redis-cache host String / 127.0.0.1 N redis服务器地址
com.gosalelab.cache.redis-cache max-idle int / 100 N 最大允许空闲对象数
com.gosalelab.cache.redis-cache max-total int / 1000 N 最大活动对象数
com.gosalelab.cache.redis-cache max-wait-millis int / 1000 N 最大等待时间,单位:毫秒
com.gosalelab.cache.redis-cache min-idle int / 20 N 最小允许空闲对象数
com.gosalelab.cache.redis-cache password String / / N redis服务器登录密码
com.gosalelab.cache.redis-cache port int / 6379 N redis服务器连接端口号
com.gosalelab.cache.redis-cache timeout int / 2000 N 连接超时时间,单位:毫秒

自定义缓存key生成类

  1. 继承KeyGenerator接口,实现getKey方法,可参考默认实现方法。
  2. 在新的类上添加如下注解,注解名称命名规则为:自定义名称 + KeyGenerator,如:defaultKeyGenerator。
  3. 然后在配置文件中使用如下方式使用缓存key自定义类:com.gosalelab.cache.key-generator = xxx

自定义缓存提供类

  1. 继承CacheProvider接口,实现put、get、del方法
  2. 添加注解:@Component("xxxCacheProvider"),注解命名规则为:自定义名称 + CacheProvider
  3. 然后再配置文件中使用如下方式使用自定义缓存提供类:com.gosalelab.cache.provider = xxx

添加配置项

  1. properties文件夹下新增配置文件
  2. CacheProperties文件初始化新增配置类
  3. 在新初始化配置类中添加@NestedConfigurationProperty注解
  4. 重新编译项目文件
  5. 在application.properties中使用配置项,按照如下方式: com.gosalelab.cache.新增配置.具体配置项 = 新增配置值

注解使用

注解

  • 注解@CacheInject用于缓存写入和读取
  • 注解@CacheEvict用于删除缓存

注解@CacheInject配置项

  • key:缓存key
  • expire:缓存时间,单位:秒
  • pre:缓存key的前缀
  • desc:描述
  • opType:缓存操作方式,可选值有: CacheOpType.READ_WRITE:读和写缓存,CacheOpType.WRITE:只写缓存,CacheOpType.READ_ONLY:只读缓存

注解@CacheEvict

  • key:缓存key
  • pre:缓存key的前缀
  • desc:描述

具体参考自带Demo中的测试类:com.gosalelab.testcase.CacheTest

缓存表达式

默认使用SpEL表达式,具体的使用可以参考Demo中的测试项。

计划

  1. 增加加英文文档
  2. 添加缓存后台管理功能
  3. 进一步优化代码

其他

  • 如果在使用中遇到问题,欢迎在Github留言 Gitee留言
  • 同时也欢迎您提交代码,一起完善项目
Copyright (c) 2018. Gosalelab. China. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持更多缓存方式。 缓存key使用Spring表达式(SpEL)解析生成。 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/hecheng_admin/aspect-cache.git
git@gitee.com:hecheng_admin/aspect-cache.git
hecheng_admin
aspect-cache
aspect-cache
master

搜索帮助