|
7 | 7 |
|
8 | 8 | #include <boost/json.hpp> |
9 | 9 |
|
| 10 | +#ifdef LD_REDIS_SUPPORT_ENABLED |
| 11 | +#include <launchdarkly/server_side/integrations/redis/redis_source.hpp> |
| 12 | +#endif |
| 13 | + |
10 | 14 | using launchdarkly::LogLevel; |
11 | 15 | using namespace launchdarkly::server_side; |
12 | 16 |
|
@@ -154,6 +158,56 @@ std::optional<std::string> EntityManager::create(ConfigParams const& in) { |
154 | 158 | } |
155 | 159 | } |
156 | 160 |
|
| 161 | +#ifdef LD_REDIS_SUPPORT_ENABLED |
| 162 | + if (in.persistentDataStore) { |
| 163 | + if (in.persistentDataStore->store.type == "redis") { |
| 164 | + std::string prefix = |
| 165 | + in.persistentDataStore->store.prefix.value_or("launchdarkly"); |
| 166 | + |
| 167 | + auto redis_result = launchdarkly::server_side::integrations:: |
| 168 | + RedisDataSource::Create(in.persistentDataStore->store.dsn, |
| 169 | + prefix); |
| 170 | + |
| 171 | + if (!redis_result) { |
| 172 | + LD_LOG(logger_, LogLevel::kWarn) |
| 173 | + << "entity_manager: couldn't create Redis data source: " |
| 174 | + << redis_result.error(); |
| 175 | + return std::nullopt; |
| 176 | + } |
| 177 | + |
| 178 | + auto lazy_load = config::builders::LazyLoadBuilder(); |
| 179 | + lazy_load.Source(std::move(*redis_result)); |
| 180 | + |
| 181 | + // Configure cache mode |
| 182 | + // Default is 5 minutes, but contract tests may specify: |
| 183 | + // - "off": disable caching (fetch from DB every time) |
| 184 | + // - "ttl": custom TTL in milliseconds |
| 185 | + // - "infinite": never expire cached items |
| 186 | + if (in.persistentDataStore->cache.mode == "off") { |
| 187 | + lazy_load.CacheRefresh(std::chrono::seconds(0)); |
| 188 | + } else if (in.persistentDataStore->cache.mode == "ttl") { |
| 189 | + if (in.persistentDataStore->cache.ttlMs) { |
| 190 | + lazy_load.CacheRefresh(std::chrono::milliseconds( |
| 191 | + *in.persistentDataStore->cache.ttlMs)); |
| 192 | + } |
| 193 | + } else if (in.persistentDataStore->cache.mode == "infinite") { |
| 194 | + // Use a very large TTL to effectively never expire |
| 195 | + lazy_load.CacheRefresh(std::chrono::hours(24 * 365)); |
| 196 | + } |
| 197 | + // If no mode specified, the default 5-minute TTL is used |
| 198 | + |
| 199 | + config_builder.DataSystem().Method( |
| 200 | + config::builders::DataSystemBuilder::LazyLoad( |
| 201 | + std::move(lazy_load))); |
| 202 | + } else { |
| 203 | + LD_LOG(logger_, LogLevel::kWarn) |
| 204 | + << "entity_manager: unsupported persistent store type: " |
| 205 | + << in.persistentDataStore->store.type; |
| 206 | + return std::nullopt; |
| 207 | + } |
| 208 | + } |
| 209 | +#endif |
| 210 | + |
157 | 211 | auto config = config_builder.Build(); |
158 | 212 | if (!config) { |
159 | 213 | LD_LOG(logger_, LogLevel::kWarn) |
|
0 commit comments