Skip to content

Commit

Permalink
feat: add sdk_key_map cache & modify init data
Browse files Browse the repository at this point in the history
  • Loading branch information
jianggang committed Aug 18, 2022
1 parent 1d6c264 commit b792530
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/featureprobe/api/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching
@SpringBootApplication
@OpenAPIDefinition(info = @Info(title = "Feature Probe API", version = "1.0", description = "Feature Probe API"))
public class Application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.featureprobe.sdk.server.FeatureProbe;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -52,6 +53,7 @@ public class EnvironmentService {
private static final String LIMITER_TOGGLE_KEY = "FeatureProbe_env_limiter";

@Transactional(rollbackFor = Exception.class)
@CacheEvict(value="all_sdk_key_map", allEntries=true)
public EnvironmentResponse create(String projectKey, EnvironmentCreateRequest createRequest) {
validateLimit(projectKey);
Project project = projectRepository.findByKey(projectKey).get();
Expand Down
76 changes: 38 additions & 38 deletions src/main/java/com/featureprobe/api/service/GuestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,44 @@ public class GuestService {
"`description`, `return_type`, `disabled_serve`, `variations`, `project_key`, `archived`, " +
"`client_availability`, `deleted`, `modified_by`, `created_by`, `created_time`, `modified_time`) " +
"VALUES (${organization_id}, 'feature toggle01', 'feature_toggle01', '', 'boolean', 0, " +
"'[{\\\"value\\\":\\\"false\\\", \\\"name\\\":\\\"不可见\\\"," +
"\\\"description\\\":\\\"运营活动对用户不可见\\\"},{\\\"value\\\":" +
"\\\"true\\\",\\\"name\\\":\\\"可见\\\",\\\"description\\\":\\\"运营活动对用户可见\\\"}]', " +
"'[{\\\"value\\\":\\\"false\\\", \\\"name\\\":\\\"disabled\\\"," +
"\\\"description\\\":\\\"\\\"},{\\\"value\\\":" +
"\\\"true\\\",\\\"name\\\":\\\"enabled\\\",\\\"description\\\":\\\"\\\"}]', " +
"'${project_key}', 0, 1, 0, ${user_id}, ${user_id}, now(), now())",

"INSERT INTO `targeting` (`organization_id`, `toggle_key`, `environment_key`, `project_key`, `version`, " +
"`disabled`, `content`, `deleted`, `modified_by`, `created_by`, `created_time`, " +
"`modified_time`) VALUES (${organization_id}, 'feature_toggle01', 'online', '${project_key}', " +
"1, 1, '{\\\"rules\\\":[],\\\"disabledServe\\\":{\\\"select\\\":0},\\\"defaultServe\\\":" +
"{\\\"select\\\":1},\\\"variations\\\":[{\\\"value\\\":\\\"false\\\",\\\"name\\\":" +
"\\\"不可见\\\",\\\"description\\\":\\\"运营活动对用户不可见\\\"},{\\\"value\\\":\\\"true\\\"," +
"\\\"name\\\":\\\"可见\\\",\\\"description\\\":\\\"运营活动对用户可见\\\"}]}', 0, " +
"\\\"disabled\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\"," +
"\\\"name\\\":\\\"enabled\\\",\\\"description\\\":\\\"\\\"}]}', 0, " +
"${user_id}, ${user_id}, now(), now())",

"INSERT INTO `variation_history` (`organization_id`, `project_key`, `toggle_key`, `environment_key`, " +
"`toggle_version`, `value`, `value_index`, `name`) VALUES (${organization_id}, '${project_key}', " +
"'feature_toggle01', 'online', 1, 'true', 1, '可见')",
"'feature_toggle01', 'online', 1, 'true', 1, 'enabled')",

"INSERT INTO `variation_history` (`organization_id`, `project_key`, `toggle_key`, `environment_key`, " +
"`toggle_version`, `value`, `value_index`, `name`) VALUES (${organization_id}, '${project_key}', " +
"'feature_toggle01', 'online', 1, 'false', 0, '不可见')",
"'feature_toggle01', 'online', 1, 'false', 0, 'disabled')",

"INSERT INTO `targeting_version` (`organization_id`, `project_key`, `environment_key`, `toggle_key`, " +
"`comment`, `content`, `disabled`, `version`, `deleted`, `modified_time`, `created_by`, " +
"`created_time`, `modified_by`) VALUES (${organization_id}, '${project_key}', 'online', " +
"'feature_toggle01', '', '{\\\"rules\\\":[],\\\"disabledServe\\\":{\\\"select\\\":0}," +
"\\\"defaultServe\\\":{\\\"select\\\":1},\\\"variations\\\":[{\\\"value\\\":\\\"false\\\"," +
"\\\"name\\\":\\\"不可见\\\",\\\"description\\\":\\\"运营活动对用户不可见\\\"}," +
"{\\\"value\\\":\\\"true\\\",\\\"name\\\":\\\"可见\\\",\\\"description\\\":\\\"" +
"运营活动对用户可见\\\"}]}', 1, 1, 0, now(), ${user_id}, now(), ${user_id})",
"\\\"name\\\":\\\"disabled\\\",\\\"description\\\":\\\"\\\"}," +
"{\\\"value\\\":\\\"true\\\",\\\"name\\\":\\\"enabled\\\",\\\"description\\\":\\\"" +
"\\\"}]}', 1, 1, 0, now(), ${user_id}, now(), ${user_id})",


"INSERT INTO `toggle` (`organization_id`, `name`, `key`, `description`, `return_type`, `disabled_serve`, " +
"`variations`, `project_key`, `archived`, `client_availability`, `deleted`, `modified_by`, " +
"`created_by`, `created_time`, `modified_time`) VALUES (${organization_id}, 'feature toggle02', " +
"'feature_toggle02', '', 'boolean', 0, '[{\\\"value\\\":\\\"false\\\",\\\"name\\\":\\\"" +
"不可见\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\",\\\"name\\\":" +
"\\\"可见\\\",\\\"description\\\":\\\"\\\"}]', '${project_key}', " +
"disabled\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\",\\\"name\\\":" +
"\\\"enabled\\\",\\\"description\\\":\\\"\\\"}]', '${project_key}', " +
"0, 1, 0, ${user_id}, ${user_id}, now(), now())",

"INSERT INTO `targeting` (`organization_id`, `toggle_key`, `environment_key`, `project_key`, `version`, " +
Expand All @@ -92,19 +92,19 @@ public class GuestService {
"\\\"subject\\\":\\\"userId\\\",\\\"predicate\\\":\\\"is one of\\\"," +
"\\\"objects\\\":[\\\"00001\\\",\\\"00002\\\"],\\\"segmentType\\\":false," +
"\\\"numberType\\\":false,\\\"datetimeType\\\":false,\\\"semVerType\\\":false}]," +
"\\\"name\\\":\\\"测试组可见\\\",\\\"serve\\\":{\\\"select\\\":1},\\\"notEmptyConditions\\\":true}]," +
"\\\"name\\\":\\\"\\\",\\\"serve\\\":{\\\"select\\\":1},\\\"notEmptyConditions\\\":true}]," +
"\\\"disabledServe\\\":{\\\"select\\\":0},\\\"defaultServe\\\":{\\\"select\\\":0}," +
"\\\"variations\\\":[{\\\"value\\\":\\\"false\\\",\\\"name\\\":\\\"不可见\\\"," +
"\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\",\\\"name\\\":\\\"可见\\\"," +
"\\\"variations\\\":[{\\\"value\\\":\\\"false\\\",\\\"name\\\":\\\"disabled\\\"," +
"\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\",\\\"name\\\":\\\"enabled\\\"," +
"\\\"description\\\":\\\"\\\"}]}', 0, ${user_id}, ${user_id}, now(), now())",

"INSERT INTO `variation_history` (`organization_id`, `project_key`, `toggle_key`, `environment_key`, " +
"`toggle_version`, `value`, `value_index`, `name`) VALUES (${organization_id}, '${project_key}', " +
"'feature_toggle02', 'online', 1, 'true', 1, '可见')",
"'feature_toggle02', 'online', 1, 'true', 1, 'enabled')",

"INSERT INTO `variation_history` (`organization_id`, `project_key`, `toggle_key`, `environment_key`, " +
"`toggle_version`, `value`, `value_index`, `name`) VALUES (${organization_id}, '${project_key}', " +
"'feature_toggle02', 'online', 1, 'false', 0, '不可见')",
"'feature_toggle02', 'online', 1, 'false', 0, 'disabled')",

"INSERT INTO `targeting_version` (`organization_id`, `project_key`, `environment_key`, `toggle_key`, " +
"`comment`, `content`, `disabled`, `version`, `deleted`, `modified_time`, `created_by`, " +
Expand All @@ -113,52 +113,52 @@ public class GuestService {
"\\\"subject\\\":\\\"userId\\\",\\\"predicate\\\":\\\"is one of\\\"," +
"\\\"objects\\\":[\\\"00001\\\",\\\"00002\\\"],\\\"segmentType\\\":false," +
"\\\"numberType\\\":false,\\\"datetimeType\\\":false,\\\"semVerType\\\":false}]," +
"\\\"name\\\":\\\"测试组可见\\\",\\\"serve\\\":{\\\"select\\\":1}," +
"\\\"name\\\":\\\"\\\",\\\"serve\\\":{\\\"select\\\":1}," +
"\\\"notEmptyConditions\\\":true}],\\\"disabledServe\\\":{\\\"select\\\":0}," +
"\\\"defaultServe\\\":{\\\"select\\\":0},\\\"variations\\\":[{\\\"value\\\":\\\"false\\\"," +
"\\\"name\\\":\\\"不可见\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\"," +
"\\\"name\\\":\\\"可见\\\",\\\"description\\\":\\\"\\\"}]}', 0, 1, 0, now(), ${user_id}, " +
"\\\"name\\\":\\\"disabled\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\"," +
"\\\"name\\\":\\\"enabled\\\",\\\"description\\\":\\\"\\\"}]}', 0, 1, 0, now(), ${user_id}, " +
"now(), ${user_id})",

"INSERT INTO `toggle` (`organization_id`, `name`, `key`, `description`, `return_type`, `disabled_serve`, " +
"`variations`, `project_key`, `archived`, `client_availability`, `deleted`, `modified_by`, " +
"`created_by`, `created_time`, `modified_time`) VALUES (${organization_id}, 'feature toggle03', " +
"'feature_toggle03', '', 'boolean', 0, '[{\\\"value\\\":\\\"false\\\",\\\"name\\\":\\\"不可见\\\"," +
"\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\",\\\"name\\\":\\\"可见\\\"," +
"'feature_toggle03', '', 'boolean', 0, '[{\\\"value\\\":\\\"false\\\",\\\"name\\\":\\\"disabled\\\"," +
"\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\",\\\"name\\\":\\\"enabled\\\"," +
"\\\"description\\\":\\\"\\\"}]', '${project_key}', 0, 1, 0, ${user_id}, ${user_id}, now(), now())",

"INSERT INTO `targeting` (`organization_id`, `toggle_key`, `environment_key`, `project_key`, `version`, " +
"`disabled`, `content`, `deleted`, `modified_by`, `created_by`, `created_time`, `modified_time`) " +
"VALUES (${organization_id}, 'feature_toggle03', 'online', '${project_key}', 1, 0, " +
"'{\\\"rules\\\":[], \\\"disabledServe\\\":{\\\"select\\\":0},\\\"defaultServe\\\":" +
"{\\\"split\\\":[6000,4000]}, \\\"variations\\\":[{\\\"value\\\":\\\"false\\\",\\\"name\\\":" +
"\\\"不可见\\\",\\\"description\\\":\\\"运营活动对用户不可见\\\"},{\\\"value\\\":\\\"true\\\"," +
"\\\"name\\\":\\\"可见\\\",\\\"description\\\":\\\"运营活动对用户可见\\\"}]}', 0, " +
"\\\"disabled\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"true\\\"," +
"\\\"name\\\":\\\"enabled\\\",\\\"description\\\":\\\"\\\"}]}', 0, " +
"${user_id}, ${user_id}, now(), now())",

"INSERT INTO `variation_history` (`organization_id`, `project_key`, `toggle_key`, `environment_key`, " +
"`toggle_version`, `value`, `value_index`, `name`) VALUES (${organization_id}, '${project_key}', " +
"'feature_toggle03', 'online', 1, 'true', 1, '可见')",
"'feature_toggle03', 'online', 1, 'true', 1, 'enabled')",

"INSERT INTO `variation_history` (`organization_id`, `project_key`, `toggle_key`, `environment_key`, " +
"`toggle_version`, `value`, `value_index`, `name`) VALUES (${organization_id}, '${project_key}', " +
"'feature_toggle03', 'online', 1, 'false', 0, '不可见')",
"'feature_toggle03', 'online', 1, 'false', 0, 'disabled')",

"INSERT INTO `targeting_version` (`organization_id`, `project_key`, `environment_key`, `toggle_key`, " +
"`comment`, `content`, `disabled`, `version`, `deleted`, `modified_time`, `created_by`, " +
"`created_time`, `modified_by`) VALUES (${organization_id}, '${project_key}', 'online', " +
"'feature_toggle03', '', '{\\\"rules\\\":[],\\\"disabledServe\\\":{\\\"select\\\":0}," +
"\\\"defaultServe\\\":{\\\"split\\\":[8000,2000]},\\\"variations\\\":[{\\\"value\\\":\\\"false\\\"," +
"\\\"name\\\":\\\"不可见\\\",\\\"description\\\":\\\"运营活动对用户不可见\\\"},{\\\"value\\\"" +
":\\\"true\\\",\\\"name\\\":\\\"可见\\\",\\\"description\\\":\\\"运营活动对用户可见\\\"}]}', 0, " +
"\\\"name\\\":\\\"disabled\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\"" +
":\\\"true\\\",\\\"name\\\":\\\"enabled\\\",\\\"description\\\":\\\"\\\"}]}', 0, " +
"1, 0, now(), ${user_id}, now(), ${user_id})",

"INSERT INTO `toggle` (`organization_id`, `name`, `key`, `description`, `return_type`, `disabled_serve`, " +
"`variations`, `project_key`, `archived`, `client_availability`, `deleted`, `modified_by`, " +
"`created_by`, `created_time`, `modified_time`) VALUES (${organization_id}, 'feature toggle04', " +
"'feature_toggle04', '', 'number', 1, '[{\\\"value\\\":\\\"10\\\",\\\"name\\\":\\\"$10\\\"," +
"\\\"description\\\":\\\"商品价格为10美元\\\"},{\\\"value\\\":\\\"20\\\",\\\"name\\\":\\\"$20\\\"," +
"\\\"description\\\":\\\"商品价格为10美元\\\"}]', '${project_key}', 0, 1, 0, ${user_id}, " +
"\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"20\\\",\\\"name\\\":\\\"$20\\\"," +
"\\\"description\\\":\\\"\\\"}]', '${project_key}', 0, 1, 0, ${user_id}, " +
"${user_id}, now(), now())",

"INSERT INTO `targeting` (`organization_id`, `toggle_key`, `environment_key`, `project_key`, `version`, " +
Expand All @@ -168,16 +168,16 @@ public class GuestService {
"\\\"subject\\\":\\\"userId\\\",\\\"predicate\\\":\\\"is one of\\\"," +
"\\\"objects\\\":[\\\"00001\\\",\\\"00002\\\"],\\\"segmentType\\\":false," +
"\\\"numberType\\\":false,\\\"datetimeType\\\":false,\\\"semVerType\\\":false}]," +
"\\\"name\\\":\\\"00001, 00002 用户商品价格为$10\\\",\\\"serve\\\":{\\\"select\\\":0}," +
"\\\"name\\\":\\\"\\\",\\\"serve\\\":{\\\"select\\\":0}," +
"\\\"notEmptyConditions\\\":true},{\\\"conditions\\\":[{\\\"type\\\":\\\"string\\\"," +
"\\\"subject\\\":\\\"userId\\\",\\\"predicate\\\":\\\"is one of\\\"," +
"\\\"objects\\\":[\\\"00003\\\"],\\\"segmentType\\\":false,\\\"numberType\\\":false," +
"\\\"datetimeType\\\":false,\\\"semVerType\\\":false}],\\\"name\\\":\\\"00003 用户商品价格为$20\\\"," +
"\\\"datetimeType\\\":false,\\\"semVerType\\\":false}],\\\"name\\\":\\\"\\\"," +
"\\\"serve\\\":{\\\"select\\\":1},\\\"notEmptyConditions\\\":true}]," +
"\\\"disabledServe\\\":{\\\"select\\\":1},\\\"defaultServe\\\":{\\\"select\\\":1}," +
"\\\"variations\\\":[{\\\"value\\\":\\\"10\\\",\\\"name\\\":\\\"$10\\\"," +
"\\\"description\\\":\\\"商品价格为10美元\\\"},{\\\"value\\\":\\\"20\\\",\\\"name\\\":\\\"$20\\\"," +
"\\\"description\\\":\\\"商品价格为20美元\\\"}]}', 0, ${user_id}, ${user_id}, now(), now())",
"\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"20\\\",\\\"name\\\":\\\"$20\\\"," +
"\\\"description\\\":\\\"\\\"}]}', 0, ${user_id}, ${user_id}, now(), now())",

"INSERT INTO `variation_history` (`organization_id`, `project_key`, `toggle_key`, `environment_key`, " +
"`toggle_version`, `value`, `value_index`, `name`) VALUES (${organization_id}, '${project_key}', " +
Expand All @@ -193,16 +193,16 @@ public class GuestService {
"'feature_toggle04', '', '{\\\"rules\\\":[{\\\"conditions\\\":[{\\\"type\\\":\\\"string\\\"," +
"\\\"subject\\\":\\\"userId\\\",\\\"predicate\\\":\\\"is one of\\\",\\\"objects\\\":[\\\"00001\\\"," +
"\\\"00002\\\"],\\\"segmentType\\\":false,\\\"numberType\\\":false,\\\"datetimeType\\\":false," +
"\\\"semVerType\\\":false}],\\\"name\\\":\\\"00001, 00002 用户商品价格为$10\\\"," +
"\\\"semVerType\\\":false}],\\\"name\\\":\\\"\\\"," +
"\\\"serve\\\":{\\\"select\\\":0},\\\"notEmptyConditions\\\":true}," +
"{\\\"conditions\\\":[{\\\"type\\\":\\\"string\\\",\\\"subject\\\":\\\"userId\\\"," +
"\\\"predicate\\\":\\\"is one of\\\",\\\"objects\\\":[\\\"00003\\\"],\\\"segmentType\\\":false," +
"\\\"numberType\\\":false,\\\"datetimeType\\\":false,\\\"semVerType\\\":false}]," +
"\\\"name\\\":\\\"00003 用户商品价格为$20\\\",\\\"serve\\\":{\\\"select\\\":1}," +
"\\\"name\\\":\\\"\\\",\\\"serve\\\":{\\\"select\\\":1}," +
"\\\"notEmptyConditions\\\":true}],\\\"disabledServe\\\":{\\\"select\\\":1}," +
"\\\"defaultServe\\\":{\\\"select\\\":1},\\\"variations\\\":[{\\\"value\\\":\\\"10\\\"," +
"\\\"name\\\":\\\"$10\\\",\\\"description\\\":\\\"商品价格为10美元\\\"},{\\\"value\\\":\\\"20\\\"," +
"\\\"name\\\":\\\"$20\\\",\\\"description\\\":\\\"商品价格为20美元\\\"}]}', 0, 1, 0, now(), " +
"\\\"name\\\":\\\"$10\\\",\\\"description\\\":\\\"\\\"},{\\\"value\\\":\\\"20\\\"," +
"\\\"name\\\":\\\"$20\\\",\\\"description\\\":\\\"\\\"}]}', 0, 1, 0, now(), " +
"${user_id}, now(), ${user_id})"
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
Expand All @@ -43,6 +44,7 @@ public class ProjectService {
private static final String LIMITER_TOGGLE_KEY = "FeatureProbe_project_limiter";

@Transactional(rollbackFor = Exception.class)
@CacheEvict(value="all_sdk_key_map", allEntries=true)
public ProjectResponse create(ProjectCreateRequest createRequest) {
validateLimit();
return ProjectMapper.INSTANCE.entityToResponse(createProject(createRequest));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.featureprobe.api.service.aspect.ExcludeTenant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class ServerService {
@PersistenceContext
public EntityManager entityManager;


@Cacheable(value="all_sdk_key_map")
public SdkKeyResponse queryAllSdkKeys() {
SdkKeyResponse sdkKeyResponse = new SdkKeyResponse();
List<Environment> environments = environmentRepository.findAll();
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config/application-online.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ app:
- /api/server/toggles
- /api/server/sdk_keys
- /api/server/events
- /api/v3/api-docs.yaml
- /api/actuator/health
guest-disabled: true
guest-default-password: Guest_12345
Loading

0 comments on commit b792530

Please sign in to comment.