|
| 1 | +/* |
| 2 | + * Copyright 2024 dengliming. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.github.dengliming.redismodule.springboot.starter.autoconfigure; |
| 18 | + |
| 19 | +import io.github.dengliming.redismodule.redisai.client.RedisAIClient; |
| 20 | +import io.github.dengliming.redismodule.redisbloom.client.RedisBloomClient; |
| 21 | +import io.github.dengliming.redismodule.redisearch.client.RediSearchClient; |
| 22 | +import io.github.dengliming.redismodule.redisgears.client.RedisGearsClient; |
| 23 | +import io.github.dengliming.redismodule.redisgraph.client.RedisGraphClient; |
| 24 | +import io.github.dengliming.redismodule.redisjson.client.RedisJSONClient; |
| 25 | +import io.github.dengliming.redismodule.redistimeseries.client.RedisTimeSeriesClient; |
| 26 | +import io.github.dengliming.redismodule.springboot.starter.env.RedisModuleProperties; |
| 27 | +import org.redisson.Redisson; |
| 28 | +import org.redisson.config.Config; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 32 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | + |
| 38 | +@ConditionalOnProperty( |
| 39 | + prefix = RedisModuleProperties.PREFIX, |
| 40 | + name = "enabled", |
| 41 | + havingValue = "true", |
| 42 | + matchIfMissing = true |
| 43 | +) |
| 44 | +@ConditionalOnClass(Redisson.class) |
| 45 | +@EnableConfigurationProperties(RedisModuleProperties.class) |
| 46 | +@Configuration |
| 47 | +public class RedisModuleAutoConfiguration { |
| 48 | + private final RedisModuleProperties redisModuleProperties; |
| 49 | + |
| 50 | + public RedisModuleAutoConfiguration(RedisModuleProperties redisModuleProperties) { |
| 51 | + this.redisModuleProperties = redisModuleProperties; |
| 52 | + } |
| 53 | + |
| 54 | + @ConditionalOnProperty( |
| 55 | + prefix = RedisModuleProperties.PREFIX + ".redisearch", |
| 56 | + name = "enabled", |
| 57 | + havingValue = "true" |
| 58 | + ) |
| 59 | + @Bean(destroyMethod = "shutdown") |
| 60 | + @ConditionalOnMissingBean |
| 61 | + public RediSearchClient rediSearchClient() { |
| 62 | + return new RediSearchClient(parseRedisModuleConfig(redisModuleProperties.getRedisearch().getConfig())); |
| 63 | + } |
| 64 | + |
| 65 | + @ConditionalOnProperty( |
| 66 | + prefix = RedisModuleProperties.PREFIX + ".redisai", |
| 67 | + name = "enabled", |
| 68 | + havingValue = "true" |
| 69 | + ) |
| 70 | + @Bean(destroyMethod = "shutdown") |
| 71 | + @ConditionalOnMissingBean |
| 72 | + public RedisAIClient redisAIClient() { |
| 73 | + return new RedisAIClient(parseRedisModuleConfig(redisModuleProperties.getRedisai().getConfig())); |
| 74 | + } |
| 75 | + |
| 76 | + @ConditionalOnProperty( |
| 77 | + prefix = RedisModuleProperties.PREFIX + ".redisbloom", |
| 78 | + name = "enabled", |
| 79 | + havingValue = "true" |
| 80 | + ) |
| 81 | + @Bean(destroyMethod = "shutdown") |
| 82 | + @ConditionalOnMissingBean |
| 83 | + public RedisBloomClient redisBloomClient() { |
| 84 | + return new RedisBloomClient(parseRedisModuleConfig(redisModuleProperties.getRedisbloom().getConfig())); |
| 85 | + } |
| 86 | + |
| 87 | + @ConditionalOnProperty( |
| 88 | + prefix = RedisModuleProperties.PREFIX + ".redisgears", |
| 89 | + name = "enabled", |
| 90 | + havingValue = "true" |
| 91 | + ) |
| 92 | + @Bean(destroyMethod = "shutdown") |
| 93 | + @ConditionalOnMissingBean |
| 94 | + public RedisGearsClient redisGearsClient() { |
| 95 | + return new RedisGearsClient(parseRedisModuleConfig(redisModuleProperties.getRedisgears().getConfig())); |
| 96 | + } |
| 97 | + |
| 98 | + @ConditionalOnProperty( |
| 99 | + prefix = RedisModuleProperties.PREFIX + ".redisgraph", |
| 100 | + name = "enabled", |
| 101 | + havingValue = "true" |
| 102 | + ) |
| 103 | + @Bean(destroyMethod = "shutdown") |
| 104 | + @ConditionalOnMissingBean |
| 105 | + public RedisGraphClient redisGraphClient() { |
| 106 | + return new RedisGraphClient(parseRedisModuleConfig(redisModuleProperties.getRedisgraph().getConfig())); |
| 107 | + } |
| 108 | + |
| 109 | + @ConditionalOnProperty( |
| 110 | + prefix = RedisModuleProperties.PREFIX + ".redisjson", |
| 111 | + name = "enabled", |
| 112 | + havingValue = "true" |
| 113 | + ) |
| 114 | + @Bean(destroyMethod = "shutdown") |
| 115 | + @ConditionalOnMissingBean |
| 116 | + public RedisJSONClient redisJSONClient() { |
| 117 | + return new RedisJSONClient(parseRedisModuleConfig(redisModuleProperties.getRedisjson().getConfig())); |
| 118 | + } |
| 119 | + |
| 120 | + @ConditionalOnProperty( |
| 121 | + prefix = RedisModuleProperties.PREFIX + ".redistimeseries", |
| 122 | + name = "enabled", |
| 123 | + havingValue = "true" |
| 124 | + ) |
| 125 | + @Bean(destroyMethod = "shutdown") |
| 126 | + @ConditionalOnMissingBean |
| 127 | + public RedisTimeSeriesClient redisTimeSeriesClient() { |
| 128 | + return new RedisTimeSeriesClient(parseRedisModuleConfig(redisModuleProperties.getRedistimeseries().getConfig())); |
| 129 | + } |
| 130 | + |
| 131 | + private Config parseRedisModuleConfig(String configStr) { |
| 132 | + Config config; |
| 133 | + try { |
| 134 | + config = Config.fromYAML(configStr); |
| 135 | + } catch (IOException e) { |
| 136 | + try { |
| 137 | + config = Config.fromJSON(configStr); |
| 138 | + } catch (IOException ex) { |
| 139 | + ex.addSuppressed(e); |
| 140 | + throw new IllegalArgumentException("Can't parse config", ex); |
| 141 | + } |
| 142 | + } |
| 143 | + return config; |
| 144 | + } |
| 145 | +} |
0 commit comments