Commit f7bbe2136bcaacee98777e2f0c96330b033500db
1 parent
410d197a
Fixed cache functionality. Moved annotation from interface to classes because of…
… Spring bug https://jira.spring.io/browse/SPR-15271
Showing
5 changed files
with
28 additions
and
34 deletions
... | ... | @@ -61,10 +61,6 @@ |
61 | 61 | <artifactId>postgresql</artifactId> |
62 | 62 | </dependency> |
63 | 63 | <dependency> |
64 | - <groupId>org.springframework</groupId> | |
65 | - <artifactId>spring-test</artifactId> | |
66 | - </dependency> | |
67 | - <dependency> | |
68 | 64 | <groupId>junit</groupId> |
69 | 65 | <artifactId>junit</artifactId> |
70 | 66 | <scope>test</scope> |
... | ... | @@ -177,10 +173,6 @@ |
177 | 173 | <groupId>com.h2database</groupId> |
178 | 174 | <artifactId>h2</artifactId> |
179 | 175 | </dependency> |
180 | - <dependency> | |
181 | - <groupId>org.springframework.boot</groupId> | |
182 | - <artifactId>spring-boot-starter-data-jpa</artifactId> | |
183 | - </dependency> | |
184 | 176 | </dependencies> |
185 | 177 | <build> |
186 | 178 | <plugins> | ... | ... |
... | ... | @@ -57,16 +57,25 @@ public class ServiceCacheConfiguration { |
57 | 57 | Config config = new Config(); |
58 | 58 | |
59 | 59 | if (zkEnabled) { |
60 | - config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); | |
61 | - | |
62 | - config.setProperty(GroupProperty.DISCOVERY_SPI_ENABLED.getName(), Boolean.TRUE.toString()); | |
63 | - DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory()); | |
64 | - discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_URL.key(), zkUrl); | |
65 | - discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_PATH.key(), zkDir); | |
66 | - discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.GROUP.key(), HAZELCAST_CLUSTER_NAME); | |
67 | - config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig); | |
60 | + addZkConfig(config); | |
68 | 61 | } |
69 | 62 | |
63 | + config.addMapConfig(createDeviceCredentialsCacheConfig()); | |
64 | + | |
65 | + return Hazelcast.newHazelcastInstance(config); | |
66 | + } | |
67 | + | |
68 | + private void addZkConfig(Config config) { | |
69 | + config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); | |
70 | + config.setProperty(GroupProperty.DISCOVERY_SPI_ENABLED.getName(), Boolean.TRUE.toString()); | |
71 | + DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory()); | |
72 | + discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_URL.key(), zkUrl); | |
73 | + discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_PATH.key(), zkDir); | |
74 | + discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.GROUP.key(), HAZELCAST_CLUSTER_NAME); | |
75 | + config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig); | |
76 | + } | |
77 | + | |
78 | + private MapConfig createDeviceCredentialsCacheConfig() { | |
70 | 79 | MapConfig deviceCredentialsCacheConfig = new MapConfig(CacheConstants.DEVICE_CREDENTIALS_CACHE); |
71 | 80 | deviceCredentialsCacheConfig.setTimeToLiveSeconds(cacheDeviceCredentialsTTL); |
72 | 81 | deviceCredentialsCacheConfig.setEvictionPolicy(EvictionPolicy.LRU); |
... | ... | @@ -75,9 +84,7 @@ public class ServiceCacheConfiguration { |
75 | 84 | cacheDeviceCredentialsMaxSizeSize, |
76 | 85 | MaxSizeConfig.MaxSizePolicy.valueOf(cacheDeviceCredentialsMaxSizePolicy)) |
77 | 86 | ); |
78 | - config.addMapConfig(deviceCredentialsCacheConfig); | |
79 | - | |
80 | - return Hazelcast.newHazelcastInstance(config); | |
87 | + return deviceCredentialsCacheConfig; | |
81 | 88 | } |
82 | 89 | |
83 | 90 | @Bean | ... | ... |
... | ... | @@ -15,25 +15,18 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.device; |
17 | 17 | |
18 | -import org.springframework.cache.annotation.CacheEvict; | |
19 | -import org.springframework.cache.annotation.Cacheable; | |
20 | 18 | import org.thingsboard.server.common.data.id.DeviceId; |
21 | 19 | import org.thingsboard.server.common.data.security.DeviceCredentials; |
22 | 20 | |
23 | -import static org.thingsboard.server.common.data.CacheConstants.DEVICE_CREDENTIALS_CACHE; | |
24 | - | |
25 | 21 | public interface DeviceCredentialsService { |
26 | 22 | |
27 | 23 | DeviceCredentials findDeviceCredentialsByDeviceId(DeviceId deviceId); |
28 | 24 | |
29 | - @Cacheable(cacheNames = DEVICE_CREDENTIALS_CACHE, unless="#result == null") | |
30 | 25 | DeviceCredentials findDeviceCredentialsByCredentialsId(String credentialsId); |
31 | 26 | |
32 | - @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, keyGenerator="previousDeviceCredentialsId", beforeInvocation = true) | |
33 | 27 | DeviceCredentials updateDeviceCredentials(DeviceCredentials deviceCredentials); |
34 | 28 | |
35 | 29 | DeviceCredentials createDeviceCredentials(DeviceCredentials deviceCredentials); |
36 | 30 | |
37 | - @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, key="#deviceCredentials.credentialsId") | |
38 | 31 | void deleteDeviceCredentials(DeviceCredentials deviceCredentials); |
39 | 32 | } | ... | ... |
... | ... | @@ -18,6 +18,8 @@ package org.thingsboard.server.dao.device; |
18 | 18 | |
19 | 19 | import lombok.extern.slf4j.Slf4j; |
20 | 20 | import org.springframework.beans.factory.annotation.Autowired; |
21 | +import org.springframework.cache.annotation.CacheEvict; | |
22 | +import org.springframework.cache.annotation.Cacheable; | |
21 | 23 | import org.springframework.stereotype.Service; |
22 | 24 | import org.springframework.util.StringUtils; |
23 | 25 | import org.thingsboard.server.common.data.Device; |
... | ... | @@ -28,6 +30,7 @@ import org.thingsboard.server.dao.EncryptionUtil; |
28 | 30 | import org.thingsboard.server.dao.exception.DataValidationException; |
29 | 31 | import org.thingsboard.server.dao.service.DataValidator; |
30 | 32 | |
33 | +import static org.thingsboard.server.common.data.CacheConstants.DEVICE_CREDENTIALS_CACHE; | |
31 | 34 | import static org.thingsboard.server.dao.service.Validator.validateId; |
32 | 35 | import static org.thingsboard.server.dao.service.Validator.validateString; |
33 | 36 | |
... | ... | @@ -49,6 +52,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { |
49 | 52 | } |
50 | 53 | |
51 | 54 | @Override |
55 | + @Cacheable(cacheNames = DEVICE_CREDENTIALS_CACHE, unless="#result == null") | |
52 | 56 | public DeviceCredentials findDeviceCredentialsByCredentialsId(String credentialsId) { |
53 | 57 | log.trace("Executing findDeviceCredentialsByCredentialsId [{}]", credentialsId); |
54 | 58 | validateString(credentialsId, "Incorrect credentialsId " + credentialsId); |
... | ... | @@ -56,6 +60,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { |
56 | 60 | } |
57 | 61 | |
58 | 62 | @Override |
63 | + @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, keyGenerator="previousDeviceCredentialsId", beforeInvocation = true) | |
59 | 64 | public DeviceCredentials updateDeviceCredentials(DeviceCredentials deviceCredentials) { |
60 | 65 | return saveOrUpdare(deviceCredentials); |
61 | 66 | } |
... | ... | @@ -82,6 +87,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { |
82 | 87 | } |
83 | 88 | |
84 | 89 | @Override |
90 | + @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, key="#deviceCredentials.credentialsId") | |
85 | 91 | public void deleteDeviceCredentials(DeviceCredentials deviceCredentials) { |
86 | 92 | log.trace("Executing deleteDeviceCredentials [{}]", deviceCredentials); |
87 | 93 | deviceCredentialsDao.removeById(deviceCredentials.getUuidId()); | ... | ... |
... | ... | @@ -15,13 +15,9 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.service; |
17 | 17 | |
18 | -import com.datastax.driver.core.utils.UUIDs; | |
19 | 18 | import com.hazelcast.core.HazelcastInstance; |
20 | 19 | import org.apache.commons.lang3.RandomStringUtils; |
21 | -import org.junit.After; | |
22 | -import org.junit.Assert; | |
23 | -import org.junit.Before; | |
24 | -import org.junit.Test; | |
20 | +import org.junit.*; | |
25 | 21 | import org.springframework.aop.framework.Advised; |
26 | 22 | import org.springframework.aop.support.AopUtils; |
27 | 23 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -56,7 +52,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { |
56 | 52 | @Autowired |
57 | 53 | private HazelcastInstance hazelcastInstance; |
58 | 54 | |
59 | - private UUID deviceId = UUIDs.timeBased(); | |
55 | + private UUID deviceId = UUID.randomUUID(); | |
60 | 56 | |
61 | 57 | @Before |
62 | 58 | public void setup() throws Exception { |
... | ... | @@ -115,7 +111,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { |
115 | 111 | |
116 | 112 | when(deviceCredentialsDao.findByDeviceId(deviceId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1)); |
117 | 113 | |
118 | - UUID deviceCredentialsId = UUIDs.timeBased(); | |
114 | + UUID deviceCredentialsId = UUID.randomUUID(); | |
119 | 115 | when(deviceCredentialsDao.findById(deviceCredentialsId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1)); |
120 | 116 | when(deviceService.findDeviceById(new DeviceId(deviceId))).thenReturn(new Device()); |
121 | 117 | |
... | ... | @@ -140,7 +136,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { |
140 | 136 | } |
141 | 137 | |
142 | 138 | private DeviceCredentials createDummyDeviceCredentialsEntity(String deviceCredentialsId) { |
143 | - DeviceCredentials result = new DeviceCredentials(new DeviceCredentialsId(UUIDs.timeBased())); | |
139 | + DeviceCredentials result = new DeviceCredentials(new DeviceCredentialsId(UUID.randomUUID())); | |
144 | 140 | result.setCredentialsId(deviceCredentialsId); |
145 | 141 | return result; |
146 | 142 | } | ... | ... |