{"id":19270017,"url":"https://github.com/yonyong/spring-cache-plus","last_synced_at":"2025-02-23T20:14:07.806Z","repository":{"id":57743405,"uuid":"458521784","full_name":"yonyong/spring-cache-plus","owner":"yonyong","description":"spring-cache-plus - More Flexible Caching Components","archived":false,"fork":false,"pushed_at":"2022-02-13T03:24:40.000Z","size":16,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-05T13:08:41.360Z","etag":null,"topics":["cache","java","redis","spring","spring-boot","spring-cache"],"latest_commit_sha":null,"homepage":"https://www.cnblogs.com/yonyong","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yonyong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-12T12:56:02.000Z","updated_at":"2024-06-16T16:17:42.000Z","dependencies_parsed_at":"2022-08-25T23:13:32.352Z","dependency_job_id":null,"html_url":"https://github.com/yonyong/spring-cache-plus","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yonyong%2Fspring-cache-plus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yonyong%2Fspring-cache-plus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yonyong%2Fspring-cache-plus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yonyong%2Fspring-cache-plus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yonyong","download_url":"https://codeload.github.com/yonyong/spring-cache-plus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240371758,"owners_count":19790888,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","java","redis","spring","spring-boot","spring-cache"],"created_at":"2024-11-09T20:22:55.809Z","updated_at":"2025-02-23T20:14:07.769Z","avatar_url":"https://github.com/yonyong.png","language":null,"readme":"*_[EN](https://gitee.com/yonyong/spring-cache-plus/blob/master/README_EN.md) | 中文_*\n\n# spring-cache-plus - 更灵活的缓存组件\n\n## 一、引言\n\n### 1. 介绍\n\n\u003e spring cache组件对超时设置不太友善，由此痛点，借鉴spring cache的思路重新设计了一套更灵活的缓存组件。\n\u003e\n\u003e **默认缓存存储使用redis**，也可以通过简单配置接入其他缓存框架。\n\n### 2. 使用场景\n\u003e 与spring cache组件类似，以注解方式来操作缓存。\n\n### 3. 优势\n\u003e 1.支持设置缓存失效时长；\n\u003e 2.使用注解操作缓存，用法简单；\n\u003e 3.基于spring生态开箱即用，疾速上手，最大程度降低代码的侵入性；\n\u003e 4.缓存框架灵活开放，支持各缓存框架极简接入（默认使用redis缓存）。\n\n## 二、快速入门\n\n\n### 1.引入依赖\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003etop.yonyong\u003c/groupId\u003e\n  \u003cartifactId\u003espring-cache-plus\u003c/artifactId\u003e\n  \u003cversion\u003e1.0.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n**友情提示**：\n\nredis以及AOP依赖  **非必须** 添加，本框架已默认接入依赖：\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-starter-aop\u003c/artifactId\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-starter-data-redis\u003c/artifactId\u003e\n\u003c/dependency\u003e\n```\n\n### 2.配置redis信息\n\n```properties\n# spring redis默认配置即可\nspring.redis.host=127.0.0.1\nspring.redis.port=6379\nspring.redis.password=123456\nspring.redis.database=0\nspring.redis.timeout=3\n```\n\n### 3. 使用注解操作缓存\n\n```java\n@Repository\npublic class DaoImpl implements Dao{\n\n    @Override\n    @CacheGet(key = \"#name\",timeout = 60)\n    public String get(String name) {\n        return \"get\" + name;\n    }\n\n    @Override\n    @CacheSet(key = \"#user.name\",timeout = 6,timeunit = TimeUnit.MINUTES)\n    public User setUser(User user) {\n        user.setId(\"SET\");\n        return user;\n    }\n\n    @Override\n    @CacheDel(key = \"#user.name\")\n    public List\u003cUser\u003e delList(User user) {\n        return null;\n    }\n\n}\n```\n\n#### CacheGet、CacheSet及CacheDel注解属性：\n\n##### CacheGet\n\n| 属性名称     | 属性类型 | 默认值           | 属性备注                                                     |\n| ------------ | -------- | ---------------- | ------------------------------------------------------------ |\n| prefix       | String   | \"\"               | 最终存到缓存中的键名前缀                                     |\n| key          | String   | \"\"               | 最终存到缓存中的键名后缀（支持EL语法）                       |\n| condition    | String   | \"\"               | 当满足条件才会执行缓存相关操作（支持EL语法）                 |\n| selectIfNull | boolean  | true             | 缓存查询不到时，是否需要从持久层查询                         |\n| setIfNull    | boolean  | true             | 缓存查询不到时场景下，若持久层存在数据，是否需要额外存储到缓存中 |\n| timeout      | long     | -1               | 缓存超时/失效时间                                            |\n| timeunit     | TimeUnit | TimeUnit.SECONDS | 缓存超时时间单位 默认秒                                      |\n\n##### CacheSet\n\n| 属性名称  | 属性类型 | 默认值           | 属性备注                                     |\n| --------- | -------- | ---------------- | -------------------------------------------- |\n| prefix    | String   | \"\"               | 最终存到缓存中的键名前缀                     |\n| key       | String   | \"\"               | 最终存到缓存中的键名后缀（支持EL语法）       |\n| condition | String   | \"\"               | 当满足条件才会执行缓存相关操作（支持EL语法） |\n| timeout   | long     | -1               | 缓存超时/失效时间                            |\n| timeunit  | TimeUnit | TimeUnit.SECONDS | 缓存超时时间单位 默认秒                      |\n\n##### CacheDel\n\n| 属性名称  | 属性类型 | 默认值 | 属性备注                                     |\n| --------- | -------- | ------ | -------------------------------------------- |\n| prefix    | String   | \"\"     | 最终存到缓存中的键名前缀                     |\n| key       | String   | \"\"     | 最终存到缓存中的键名后缀（支持EL语法）       |\n| condition | String   | \"\"     | 当满足条件才会执行缓存相关操作（支持EL语法） |\n\n\n## 三、自定义配置\n\n### 1. 自定义redisTemplate 配置\n\n支持自定义redisTemplate配置\n\n#### 1.1 排除默认redisTemlate配置类\n\n```java\n@SpringBootApplication(exclude = RedisCacheConfig.class)\n```\n\n#### 1.2 自定义redis配置\n\n```java\n@Configuration\npublic class RedisConfig {\n\n    @Bean\n    public RedisTemplate\u003cString, Object\u003e init(RedisConnectionFactory redisConnectionFactory) {\n        RedisTemplate\u003cString, Object\u003e redisTemplate = new RedisTemplate\u003c\u003e();\n        redisTemplate.setConnectionFactory(redisConnectionFactory);\n        redisTemplate.setKeySerializer(new StringRedisSerializer());\n        // 自定义配置 ......\n        return redisTemplate;\n    }\n}\n```\n\n### 2. 自行实现redis 操作API\n\n#### 2.1 排除RedisCacheTemplate\n\n```java\n@SpringBootApplication(exclude = RedisCacheConfig.class)\n```\n\n#### 2.2 实现YangCacheTemplate\n\n```java\n@Component\n@AllArgsConstructor\n@ConditionalOnProperty(prefix = \"system.cache.client.redis\", name = \"enable\", havingValue = \"true\", matchIfMissing = true)\npublic class RedisCacheTemplate implements YangCacheTemplate {\n\n    private final RedisTemplate client;\n\n    @Override\n    public boolean exist(String key) {\n        \n        // client.exist ......\n    }\n\n    @Override\n    public boolean set(String key, Object value, long time, TimeUnit timeUnit) {\n        // 设置缓存 set k,v\n        // client.set ......\n    }\n\n    @Override\n    public boolean del(String key) {\n        // 删除缓存 del k\n        // client.del ......\n    }\n\n    @Override\n    public Object get(String key) {\n        // 查询缓存 get k\n        // client.del ......\n    }\n}\n```\n\n\n\n### 3. 替换redis为其他缓存框架\n\n#### 3.1 禁用redis\n\n配置文件添加配置\n\n```properties\n# application.properties redis配置开关\nsystem.cache.client.redis.enable=false\n```\n\n#### 3.2 实现 YangCacheTemplate\n\n```java\n@Component\n@AllArgsConstructor\n@ConditionalOnProperty(prefix = \"system.cache.client.redis\", name = \"enable\", havingValue = \"true\", matchIfMissing = true)\npublic class RedisCacheTemplate implements YangCacheTemplate {\n\n    private final 第三方缓存框架Client client;\n\n    @Override\n    public boolean exist(String key) {\n        \n        // client.exist ......\n    }\n\n    @Override\n    public boolean set(String key, Object value, long time, TimeUnit timeUnit) {\n        // 设置缓存 set k,v\n        // client.set ......\n    }\n\n    @Override\n    public boolean del(String key) {\n        // 删除缓存 del k\n        // client.del ......\n    }\n\n    @Override\n    public Object get(String key) {\n        // 查询缓存 get k\n        // client.del ......\n    }\n}\n```\n\n\n\n## 联系我\n\n\u003e Github： https://github.com/yonyong\n\u003e\n\u003e Wechat：young2365878736\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyonyong%2Fspring-cache-plus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyonyong%2Fspring-cache-plus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyonyong%2Fspring-cache-plus/lists"}