View Javadoc
1   package pk.lucidxpo.ynami.spring.features;
2   
3   import org.springframework.beans.factory.annotation.Value;
4   import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
5   import org.springframework.context.annotation.Bean;
6   import org.springframework.context.annotation.Configuration;
7   import org.togglz.core.repository.StateRepository;
8   import org.togglz.core.repository.cache.CachingStateRepository;
9   
10  import javax.sql.DataSource;
11  
12  import static org.togglz.core.repository.jdbc.JDBCStateRepository.newBuilder;
13  
14  @Configuration
15  @ConditionalOnProperty(name = "config.togglz.enabled", havingValue = "true")
16  public class TogglzConfiguration {
17      /*
18       * The CachingStateRepository will act as a cache for persistentStateRepository.
19       * It will cache the results for 'cachingStateRepositoryTtl' milliseconds. If you omit the timeout, lookups will be cached
20       * until setFeatureState() is called for the specific feature. Using the cache without a timeout only makes sense
21       * if the repository state is never modified directly (by modifying the database table for example).
22       */
23      @Bean
24      @ConditionalOnProperty(name = "config.persistable.feature.toggles", havingValue = "true")
25      public StateRepository getStateRepository(final DataSource dataSource,
26                                                @Value("${togglz.table.name}") final String tableName,
27                                                @Value("${togglz.caching.state.repository.ttl}") final Long cachingStateRepositoryTtl) {
28          final StateRepository persistentStateRepository = newBuilder(dataSource)
29                  .tableName(tableName)
30                  .createTable(false)
31                  .build();
32          return new CachingStateRepository(persistentStateRepository, cachingStateRepositoryTtl);
33      }
34  }