{"id":18021150,"url":"https://github.com/weasley-j/sessionstore-spring-parent","last_synced_at":"2025-04-04T17:22:44.144Z","repository":{"id":186657279,"uuid":"675513695","full_name":"Weasley-J/sessionstore-spring-parent","owner":"Weasley-J","description":"一个用于将特定类型的数据和业务数据解耦并独立存储的spring-boot-starter，允许选择性地将数据存放到独立的Redis数据库中，基于Redisson实现，可以充分利用Redis的0~15号索引数据库","archived":false,"fork":false,"pushed_at":"2023-08-10T15:28:31.000Z","size":38,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T02:44:58.197Z","etag":null,"topics":["session-store"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Weasley-J.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-07T05:18:36.000Z","updated_at":"2023-10-15T09:30:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"cbd359a5-3958-4497-aaa3-080164939df2","html_url":"https://github.com/Weasley-J/sessionstore-spring-parent","commit_stats":null,"previous_names":["weasley-j/sessionstore-spring-parent"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Weasley-J%2Fsessionstore-spring-parent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Weasley-J%2Fsessionstore-spring-parent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Weasley-J%2Fsessionstore-spring-parent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Weasley-J%2Fsessionstore-spring-parent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Weasley-J","download_url":"https://codeload.github.com/Weasley-J/sessionstore-spring-parent/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247217691,"owners_count":20903108,"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":["session-store"],"created_at":"2024-10-30T06:08:54.785Z","updated_at":"2025-04-04T17:22:44.104Z","avatar_url":"https://github.com/Weasley-J.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sessionstore-spring-parent\n\n[![Maven Central](https://img.shields.io/maven-central/v/io.github.weasley-j/sessionstore-spring-starter)](https://search.maven.org/artifact/io.github.weasley-j/sessionstore-spring-starter)\n\n\u003e 一个用于将**特定类型的数据**和**业务数据**解耦并独立存储的`spring-boot-starter`，允许选择性地将数据存放到独立的`Redis`\n\u003e 数据库中，基于`Redisson`实现，可以充分利用Redis的0~15号索引数据库。\n\n\n\n常见可解耦的数据类型：\n\n- 邮件队列：\n- 认证与授权数据：用户的角色、权限信息可以独立存储，以提供更高的安全性和可扩展性\n- 用户配置数据：个性化设置、首选项、主题选择等数据\n- 统计数据：程序的统计信息，访问量、用户活跃度\n- 外部服务数据： 一些应用可能需要与外部服务进行数据交互，这些数据可以独立存储或通过外部服务的API进行管理\n\n\n\n`spring-boot`适配情况: \n\n| spring-boot版本       | JDK版本        | 适配 |\n|---------------------|--------------|----|\n| `spring-boot 2.x.x` | `\u003e= JDK 1.8` | ✅  |\n| `spring-boot 3.x.x` | ` \u003e= JDK 17` | ✅  |\n\n\n\n# 1 快速开始\n\n### 1.1 引入Maven依赖\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.weasley-j\u003c/groupId\u003e\n    \u003cartifactId\u003esessionstore-spring-starter\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### 1.2 配置Redis连接信息\n\n[元数配置类](https://github.com/Weasley-J/sessionstore-spring-parent/blob/main/sessionstore-spring-starter/src/main/java/io/github/weasleyj/sessionstore/config/SessionStoreRedisProperties.java#L20-L69)\n\n```yaml\nsession:\n  store:\n    enable: on\n    redis:\n      database: 1\n      ssl: off\n      host: 192.168.31.140\n      port: 6379\n      password: 123456\n```\n\n### 1.3 启用sessionstore\n\n创建配置类\n\n```java\nimport io.github.weasleyj.sessionstore.EnableSessionStore;\nimport org.springframework.context.annotation.Configuration;\n\n/**\n * Session Store Configuration\n */\n@Configuration\n@EnableSessionStore\npublic class SessionStoreConfig {\n}\n```\n\n或者，将注解添加启动类上：\n\n```java\n\n@EnableSessionStore\n@SpringBootApplication\npublic class SomeApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(SomeApplication.class, args);\n    }\n\n}\n```\n\n### 1.4 使用示例\n\n#### 1.4.1 使用配置文件中指定的`Redis`默认索引库\n\n- 注入默认`sessionStoreRedissonClient`实例对象, e.g:\n\n  ```java\n  @Autowired\n  private RedissonClient sessionStoreRedissonClient\n  ```\n\n- Session Store Demo Controller\n\n```java\nimport com.fasterxml.jackson.core.type.TypeReference;\nimport io.github.weasleyj.sessionstore.config.SessionStoreRedisProperties;\nimport io.github.weasleyj.simple.dto.UserDTO;\nimport io.github.weasleyj.simple.entity.User;\nimport org.redisson.api.RBucket;\nimport org.redisson.api.RedissonClient;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.PathVariable;\nimport org.springframework.web.bind.annotation.PostMapping;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport java.time.Duration;\n\nimport static io.github.weasleyj.sessionstore.support.CodecSupport.codec;\n\n\n/**\n * Session Store Demo Controller\n * \u003cp\u003e\n * 使用配置文件中指定的Redis默认索引库\n *\n * @author weasley\n * @version 1.0.0\n */\n@RestController\n@RequestMapping(\"/session/store/simple\")\npublic class SimpleController {\n    static final String KEY_PREFIX = SessionStoreRedisProperties.REDIS_KEY_PREFIX;\n\n    @Autowired\n    private RedissonClient sessionStoreRedissonClient;\n\n    /**\n     * 存入用户json\n     */\n    @PostMapping(\"/save\")\n    public User save() {\n        User user = new User();\n        RBucket\u003cUser\u003e bucket = sessionStoreRedissonClient.getBucket(KEY_PREFIX + \":\" + user.getId());\n        if (bucket.isExists()) {\n            return bucket.get();\n        }\n        bucket.set(user, Duration.ofMinutes(30));\n        return bucket.get();\n    }\n\n    /**\n     * 使用不同的DTO类型将用户数据取出\n     */\n    @PostMapping(\"/info1/{uid}\")\n    public UserDTO getUser1(@PathVariable(\"uid\") Long uid) {\n        RBucket\u003cUserDTO\u003e bucket = sessionStoreRedissonClient.getBucket(KEY_PREFIX + \":\" + uid, codec(UserDTO.class));\n        return bucket.get();\n    }\n\n    /**\n     * 使用不同的DTO类型将用户数据取出\n     */\n    @PostMapping(\"/info2/{uid}\")\n    public UserDTO getUser2(@PathVariable(\"uid\") Long uid) {\n        RBucket\u003cUserDTO\u003e bucket = sessionStoreRedissonClient.getBucket(KEY_PREFIX + \":\" + uid, codec(new TypeReference\u003cUserDTO\u003e() {\n        }));\n        return bucket.get();\n    }\n}\n```\n\n\n\n#### 1.4.2 使用`RedissonClientHelper#createRedissonClient(int)`创建指定索引库的客户端\n\n\u003e `RedissonClientHelper#createRedissonClient`创建的`RedissonClient`不受`Spring IOC`托管，使用完成后，使用完后需要关闭资源。\n\n代码示例：\n\n```java\nimport io.github.weasleyj.sessionstore.config.SessionStoreRedisProperties;\nimport io.github.weasleyj.sessionstore.support.CodecSupport;\nimport io.github.weasleyj.sessionstore.support.RedissonClientHelper;\nimport io.github.weasleyj.simple.dto.UserDTO;\nimport io.github.weasleyj.simple.entity.User;\nimport org.junit.jupiter.api.AfterEach;\nimport org.junit.jupiter.api.Assertions;\nimport org.junit.jupiter.api.BeforeEach;\nimport org.junit.jupiter.api.DisplayName;\nimport org.junit.jupiter.api.Test;\nimport org.redisson.api.RBucket;\nimport org.redisson.api.RedissonClient;\nimport org.springframework.boot.test.context.SpringBootTest;\n\nimport java.time.Duration;\n\n/**\n * RedissonClientHelper Test\n */\n@SpringBootTest\nclass RedissonClientHelperTest {\n\n    static final String KEY_PREFIX = SessionStoreRedisProperties.REDIS_KEY_PREFIX;\n\n    @Test\n    @DisplayName(\"创建Client并指定Redis索引库\")\n    void createRedissonClient() {\n        RedissonClient redissonClient = RedissonClientHelper.createRedissonClient(2);\n        User user = new User();\n        RBucket\u003cUser\u003e bucket = redissonClient.getBucket(KEY_PREFIX + \":\" + user.getId());\n        if (bucket.isExists()) {\n            user = bucket.get();\n        }\n        bucket.set(user, Duration.ofMinutes(30));\n        Assertions.assertNotNull(user);\n        System.out.println(user); // User(id=1, name=张三, age=22)\n        redissonClient.shutdown();\n    }\n\n    @Test\n    @DisplayName(\"创建Client并指定Redis索引库，使用其他对象取出数据\")\n    void createRedissonClient2() {\n        RedissonClient redissonClient = RedissonClientHelper.createRedissonClient(2);\n        RBucket\u003cUserDTO\u003e bucket = redissonClient.getBucket(KEY_PREFIX + \":\" + 1, CodecSupport.codec(UserDTO.class));\n        System.out.println(bucket.get()); // UserDTO(uid=null, name=张三, age=22)\n        Assertions.assertNotNull(bucket.get());\n        redissonClient.shutdown();\n    }\n\n}\n```\n\n`User` 与 `UserDTO` 结构对比:\n\n![image-20230807122436022](https://weasley.oss-cn-shanghai.aliyuncs.com/Photos/image-20230807122436022.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweasley-j%2Fsessionstore-spring-parent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweasley-j%2Fsessionstore-spring-parent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweasley-j%2Fsessionstore-spring-parent/lists"}