sarvenyanik commited on
Commit
8a69d1b
·
1 Parent(s): 85048d7

add recource service, update messagebox and update cache

Browse files
src/main/java/com/artandscience/webbff/webbff/api/controller/BackOfficeController.java ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ package com.artandscience.webbff.webbff.api.controller;
2
+
3
+ public class BackOfficeController {
4
+ }
src/main/java/com/artandscience/webbff/webbff/common/service/ExternalAttributeService.java CHANGED
@@ -5,6 +5,7 @@ import com.artandscience.webbff.webbff.common.base.response.MessageBox;
5
 
6
  public interface ExternalAttributeService {
7
 
 
8
  MessageBox getMessageBox(MessageModel messageModel, String... parameters);
9
 
10
  }
 
5
 
6
  public interface ExternalAttributeService {
7
 
8
+ String getResource(String resourceKey, String... parameters);
9
  MessageBox getMessageBox(MessageModel messageModel, String... parameters);
10
 
11
  }
src/main/java/com/artandscience/webbff/webbff/common/service/ResourceService.java ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ package com.artandscience.webbff.webbff.common.service;
2
+
3
+ public interface ResourceService {
4
+ String getResource(String resourceKey);
5
+
6
+ }
src/main/java/com/artandscience/webbff/webbff/common/service/StateManagementService.java CHANGED
@@ -1,5 +1,7 @@
1
  package com.artandscience.webbff.webbff.common.service;
2
 
 
 
3
  public interface StateManagementService {
4
 
5
  <T> void setState(String key, String exTime, T object);
@@ -12,4 +14,8 @@ public interface StateManagementService {
12
 
13
  void removeState(String key);
14
 
 
 
 
 
15
  }
 
1
  package com.artandscience.webbff.webbff.common.service;
2
 
3
+ import java.util.Collection;
4
+
5
  public interface StateManagementService {
6
 
7
  <T> void setState(String key, String exTime, T object);
 
14
 
15
  void removeState(String key);
16
 
17
+ void clearAllCache();
18
+
19
+ Collection<String> getAllCache();
20
+
21
  }
src/main/java/com/artandscience/webbff/webbff/common/service/impl/ExternalAttributeServiceImpl.java CHANGED
@@ -6,6 +6,7 @@ import com.artandscience.webbff.webbff.common.base.response.MessageBox;
6
  import com.artandscience.webbff.webbff.common.scope.RequestHeaderScope;
7
  import com.artandscience.webbff.webbff.common.service.ExternalAttributeService;
8
  import com.artandscience.webbff.webbff.common.service.MessageBoxService;
 
9
  import lombok.RequiredArgsConstructor;
10
  import lombok.extern.slf4j.Slf4j;
11
  import org.springframework.stereotype.Service;
@@ -17,6 +18,22 @@ public class ExternalAttributeServiceImpl implements ExternalAttributeService {
17
 
18
  private final RequestHeaderScope requestHeaderScope;
19
  private final MessageBoxService messageBoxService;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @Override
22
  public MessageBox getMessageBox(MessageModel messageModel, String... parameters) {
 
6
  import com.artandscience.webbff.webbff.common.scope.RequestHeaderScope;
7
  import com.artandscience.webbff.webbff.common.service.ExternalAttributeService;
8
  import com.artandscience.webbff.webbff.common.service.MessageBoxService;
9
+ import com.artandscience.webbff.webbff.common.service.ResourceService;
10
  import lombok.RequiredArgsConstructor;
11
  import lombok.extern.slf4j.Slf4j;
12
  import org.springframework.stereotype.Service;
 
18
 
19
  private final RequestHeaderScope requestHeaderScope;
20
  private final MessageBoxService messageBoxService;
21
+ private final ResourceService resourceService;
22
+
23
+ @Override
24
+ public String getResource(String resourceKey, String... parameters) {
25
+ try {
26
+ var requestHeader = requestHeaderScope.getRequestHeader();
27
+ var resource = resourceService.getResource(resourceKey + "." + requestHeader.getLanguage().toLowerCase());
28
+ if (resource != null && parameters != null && parameters.length != 0) {
29
+ return String.format(resource, parameters);
30
+ }
31
+ return resource;
32
+ } catch (Exception e) {
33
+ log.error("ExternalAttributeServiceImpl getResource() exception occurred for : " + resourceKey, e);
34
+ return "";
35
+ }
36
+ }
37
 
38
  @Override
39
  public MessageBox getMessageBox(MessageModel messageModel, String... parameters) {
src/main/java/com/artandscience/webbff/webbff/common/service/impl/MessageBoxServiceImpl.java CHANGED
@@ -89,7 +89,7 @@ public class MessageBoxServiceImpl implements MessageBoxService {
89
  };
90
  }
91
 
92
- @Scheduled(fixedDelayString = "${security.messagebox-cache-expire-time}")
93
  @CacheEvict(value = CACHE_NAME_MESSAGE_BOX, allEntries = true)
94
  public void clearCache() {
95
  log.debug("Cache '{}' cleared.", CACHE_NAME_MESSAGE_BOX);
 
89
  };
90
  }
91
 
92
+ @Scheduled(cron = "${artandscience-web.messagebox-cache-expire-time}")
93
  @CacheEvict(value = CACHE_NAME_MESSAGE_BOX, allEntries = true)
94
  public void clearCache() {
95
  log.debug("Cache '{}' cleared.", CACHE_NAME_MESSAGE_BOX);
src/main/java/com/artandscience/webbff/webbff/common/service/impl/ResourceServiceImpl.java ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.artandscience.webbff.webbff.common.service.impl;
2
+
3
+ import com.artandscience.webbff.webbff.common.service.ResourceService;
4
+ import com.artandscience.webbff.webbff.dao.entity.Resource;
5
+ import com.artandscience.webbff.webbff.dao.repository.ResourceRepository;
6
+ import lombok.RequiredArgsConstructor;
7
+ import lombok.extern.slf4j.Slf4j;
8
+ import org.springframework.cache.annotation.CacheEvict;
9
+ import org.springframework.cache.annotation.Cacheable;
10
+ import org.springframework.scheduling.annotation.Scheduled;
11
+ import org.springframework.stereotype.Service;
12
+
13
+ import java.util.Optional;
14
+
15
+ @Slf4j
16
+ @Service
17
+ @RequiredArgsConstructor
18
+ public class ResourceServiceImpl implements ResourceService {
19
+
20
+ private static final String CACHE_NAME_RESOURCE = "artandsciencewebbff-resource";
21
+ private final ResourceRepository resourceRepository;
22
+
23
+ @Override
24
+ @Cacheable(cacheNames = CACHE_NAME_RESOURCE, key = "#resourceKey", unless = "#result == null")
25
+ public String getResource(String resourceKey) {
26
+ try {
27
+ Optional<Resource> optionalResource = this.resourceRepository.findById(String.format("%s", resourceKey));
28
+ if (optionalResource.isPresent()) {
29
+ Resource resource = optionalResource.get();
30
+ return resource.getValue();
31
+ }
32
+ log.info("ResourceService resourceKey not found " + resourceKey);
33
+ return null;
34
+ } catch (Exception e) {
35
+ log.error("ResourceService - getResource - exception" + e.getMessage() + " Resource Key: " + resourceKey, e);
36
+ return null;
37
+ }
38
+ }
39
+
40
+ @Scheduled(cron = "${artandscience-web.resource-cache-expire-time}")
41
+ @CacheEvict(value = CACHE_NAME_RESOURCE, allEntries = true)
42
+ public void clearCache() {
43
+ log.debug("Cache '{}' cleared.", CACHE_NAME_RESOURCE);
44
+ }
45
+
46
+ }
src/main/java/com/artandscience/webbff/webbff/common/service/impl/StateManagementServiceImpl.java CHANGED
@@ -10,8 +10,10 @@ import io.micrometer.common.util.StringUtils;
10
  import lombok.RequiredArgsConstructor;
11
  import lombok.extern.slf4j.Slf4j;
12
  import org.springframework.beans.factory.annotation.Value;
 
13
  import org.springframework.stereotype.Service;
14
 
 
15
  import java.util.concurrent.TimeUnit;
16
 
17
  @Service
@@ -20,8 +22,8 @@ import java.util.concurrent.TimeUnit;
20
  public class StateManagementServiceImpl implements StateManagementService {
21
 
22
  private final RedisUtil redisUtil;
23
-
24
  private final RequestHeaderScope requestHeaderScope;
 
25
 
26
  @Value("${spring.data.redis.session-expire-time}")
27
  private String stateExpireTime;
@@ -79,7 +81,29 @@ public class StateManagementServiceImpl implements StateManagementService {
79
  }
80
 
81
  @Override
82
- public void removeState(String sessionKey) {
 
 
 
 
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  }
85
  }
 
10
  import lombok.RequiredArgsConstructor;
11
  import lombok.extern.slf4j.Slf4j;
12
  import org.springframework.beans.factory.annotation.Value;
13
+ import org.springframework.cache.CacheManager;
14
  import org.springframework.stereotype.Service;
15
 
16
+ import java.util.Collection;
17
  import java.util.concurrent.TimeUnit;
18
 
19
  @Service
 
22
  public class StateManagementServiceImpl implements StateManagementService {
23
 
24
  private final RedisUtil redisUtil;
 
25
  private final RequestHeaderScope requestHeaderScope;
26
+ private final CacheManager cacheManager;
27
 
28
  @Value("${spring.data.redis.session-expire-time}")
29
  private String stateExpireTime;
 
81
  }
82
 
83
  @Override
84
+ public void removeState(String key) {
85
+ var requestHeader = requestHeaderScope.getRequestHeader();
86
+ var sessionId = requestHeader.getSessionId();
87
+ redisUtil.removeValue(key + "-" + sessionId);
88
+ }
89
 
90
+ @Override
91
+ public void clearAllCache() {
92
+ var allCache = cacheManager.getCacheNames();
93
+
94
+ allCache.forEach(item -> {
95
+ var cache = cacheManager.getCache(item);
96
+ if (cache != null) {
97
+ cache.clear();
98
+ } else {
99
+ log.info("cache null");
100
+ }
101
+ });
102
+
103
+ }
104
+
105
+ @Override
106
+ public Collection<String> getAllCache() {
107
+ return cacheManager.getCacheNames();
108
  }
109
  }
src/main/java/com/artandscience/webbff/webbff/configuration/RedisLocalConfiguration.java CHANGED
@@ -6,11 +6,16 @@ import org.springframework.cache.annotation.EnableCaching;
6
  import org.springframework.context.annotation.Bean;
7
  import org.springframework.context.annotation.Configuration;
8
  import org.springframework.context.annotation.Profile;
 
 
 
9
  import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
10
  import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
 
11
  import org.springframework.data.redis.core.RedisTemplate;
12
  import org.springframework.data.redis.serializer.GenericToStringSerializer;
13
  import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
 
14
  import org.springframework.data.redis.serializer.StringRedisSerializer;
15
 
16
  @Configuration
@@ -29,23 +34,39 @@ public class RedisLocalConfiguration {
29
  private String redisPassword;
30
 
31
  @Bean
32
- protected JedisConnectionFactory redisConnectionFactory() {
33
- RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
34
- redisStandaloneConfiguration.setHostName(redisHostName);
35
- redisStandaloneConfiguration.setPort(redisPost);
36
- redisStandaloneConfiguration.setPassword(redisPassword);
37
- return new JedisConnectionFactory(redisStandaloneConfiguration);
38
  }
39
 
40
  @Bean
41
- public RedisTemplate<String,Object> redisTemplate() {
42
  RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
43
- redisTemplate.setConnectionFactory(redisConnectionFactory());
44
  redisTemplate.setKeySerializer(new StringRedisSerializer());
 
45
  redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
46
- redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
47
  redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
48
  return redisTemplate;
49
  }
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  }
 
6
  import org.springframework.context.annotation.Bean;
7
  import org.springframework.context.annotation.Configuration;
8
  import org.springframework.context.annotation.Profile;
9
+ import org.springframework.data.redis.cache.RedisCacheConfiguration;
10
+ import org.springframework.data.redis.cache.RedisCacheManager;
11
+ import org.springframework.data.redis.connection.RedisConnectionFactory;
12
  import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
13
  import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
14
+ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
15
  import org.springframework.data.redis.core.RedisTemplate;
16
  import org.springframework.data.redis.serializer.GenericToStringSerializer;
17
  import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
18
+ import org.springframework.data.redis.serializer.RedisSerializationContext;
19
  import org.springframework.data.redis.serializer.StringRedisSerializer;
20
 
21
  @Configuration
 
34
  private String redisPassword;
35
 
36
  @Bean
37
+ public RedisConnectionFactory redisConnectionFactory() {
38
+ RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
39
+ config.setHostName(redisHostName);
40
+ config.setPort(redisPost);
41
+ config.setPassword(redisPassword);
42
+ return new LettuceConnectionFactory(config); // ister Jedis de kullanabilirsin
43
  }
44
 
45
  @Bean
46
+ public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
47
  RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
48
+ redisTemplate.setConnectionFactory(connectionFactory);
49
  redisTemplate.setKeySerializer(new StringRedisSerializer());
50
+ redisTemplate.setHashKeySerializer(new StringRedisSerializer());
51
  redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
 
52
  redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
53
  return redisTemplate;
54
  }
55
 
56
+ @Bean
57
+ public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
58
+ RedisCacheConfiguration cacheConfig = RedisCacheConfiguration
59
+ .defaultCacheConfig()
60
+ .serializeKeysWith(
61
+ RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())
62
+ )
63
+ .serializeValuesWith(
64
+ RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer())
65
+ );
66
+
67
+ return RedisCacheManager.builder(connectionFactory)
68
+ .cacheDefaults(cacheConfig)
69
+ .build();
70
+ }
71
+
72
  }
src/main/java/com/artandscience/webbff/webbff/dao/entity/Resource.java ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.artandscience.webbff.webbff.dao.entity;
2
+
3
+ import jakarta.persistence.Entity;
4
+ import jakarta.persistence.Id;
5
+ import jakarta.persistence.Table;
6
+ import lombok.AllArgsConstructor;
7
+ import lombok.Builder;
8
+ import lombok.Data;
9
+ import lombok.NoArgsConstructor;
10
+
11
+ @Data
12
+ @Entity
13
+ @Builder
14
+ @NoArgsConstructor
15
+ @AllArgsConstructor
16
+ @Table(name = "resource", schema = "artandscience")
17
+ public class Resource {
18
+
19
+ @Id
20
+ private String key;
21
+ private String value;
22
+
23
+ }
src/main/java/com/artandscience/webbff/webbff/dao/repository/ResourceRepository.java ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ package com.artandscience.webbff.webbff.dao.repository;
2
+
3
+ import com.artandscience.webbff.webbff.dao.entity.Resource;
4
+ import org.springframework.data.jpa.repository.JpaRepository;
5
+ import org.springframework.stereotype.Repository;
6
+
7
+ @Repository
8
+ public interface ResourceRepository extends JpaRepository<Resource,String> {
9
+ }
src/main/resources/application.yml CHANGED
@@ -29,6 +29,11 @@ spring:
29
  session-expire-time: ${REDIS_STATE_EXPIRE_TIME}
30
  sentinel:
31
  master: ${SPRING_REDIS_SENTINEL_MASTER}
 
 
 
 
 
32
  server:
33
  port: 9090
34
  servlet:
 
29
  session-expire-time: ${REDIS_STATE_EXPIRE_TIME}
30
  sentinel:
31
  master: ${SPRING_REDIS_SENTINEL_MASTER}
32
+
33
+ artandscience-web:
34
+ messagebox-cache-expire-time: 0 0 3 * * *
35
+ resource-cache-expire-time: 0 0 3 * * *
36
+
37
  server:
38
  port: 9090
39
  servlet: