Solon v3.0.3

多种 Redis 接口适配复用一份配置

</> markdown

以 CacheService ,Sa-Token Dao,及原生客户端三者复用为例

1、基于 redisx

依赖包配置

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-cache-jedis</artifactId>
    <!-- 或者用 (如果不需要 CacheService,直接用它)
    <artifactId>redisx</artifactId>
    -->
</dependency>

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>sa-token-solon-plugin</artifactId>
</dependency>

应用配置(参考:https://gitee.com/noear/redisx

demo.redis:
  server: "localhost:6379"
  db: 0 #默认为 0,可不配置
  password: ""
  maxTotal: 200 #默认为 200,可不配

代码应用

@Configuration
public class Config {
    // 构建 redis client(如直接用)
    @Bean
    public RedisClient redisClient(@Inject("${demo.redis}") RedisClient client) {
        return client;
    }
    
    //构建 Cache Service(给 @Cache 用)
    @Bean
    public CacheService cacheService(@Inject RedisClient client){
        return new RedisCacheService(client, 30);
    }
    
    //构建 SaToken Dao
    @Bean
    public SaTokenDao saTokenDao(@Inject RedisClient client){
        return new SaTokenDaoOfRedis(client, 30);
    }
}

2、基于 redisson

依赖包配置

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-cache-redisson</artifactId> 
    <!-- 或者用 (如果不需要 CacheService,直接用它)
    <artifactId>redisson-solon-plugin</artifactId>
    -->
</dependency>

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>sa-token-solon-plugin</artifactId>
</dependency>

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>sa-token-dao-redisson-jackson</artifactId>
</dependency>

应用配置(参考:redisson-solon-plugin

demo.redis:
  config: |
    singleServerConfig:
      password: "123456"
      address: "redis://localhost:6379"
      database: 0

代码应用

@Configuration
public class Config {
    // 构建 redis client(如直接用);RedissonClientOriginalSupplier 支持 Redisson 的原始风格配置
    @Bean
    public RedissonClient redisClient(@Inject("${demo.redis}") RedissonClientOriginalSupplier supplier) {
        return supplier.get();
    }
    
    //构建 Cache Service(给 @Cache 用)
    @Bean
    public CacheService cacheService(@Inject RedissonClient client){
        return new RedissonCacheService(client, 30);
    }
    
    //构建 SaToken Dao //v2.4.3 后支持
    @Bean
    public SaTokenDao saTokenDao(@Inject RedissonClient client){
        return new SaTokenDaoOfRedissonJackson(client, 30);
    }
}