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,10 +61,6 @@ | ||
61 | <artifactId>postgresql</artifactId> | 61 | <artifactId>postgresql</artifactId> |
62 | </dependency> | 62 | </dependency> |
63 | <dependency> | 63 | <dependency> |
64 | - <groupId>org.springframework</groupId> | ||
65 | - <artifactId>spring-test</artifactId> | ||
66 | - </dependency> | ||
67 | - <dependency> | ||
68 | <groupId>junit</groupId> | 64 | <groupId>junit</groupId> |
69 | <artifactId>junit</artifactId> | 65 | <artifactId>junit</artifactId> |
70 | <scope>test</scope> | 66 | <scope>test</scope> |
@@ -177,10 +173,6 @@ | @@ -177,10 +173,6 @@ | ||
177 | <groupId>com.h2database</groupId> | 173 | <groupId>com.h2database</groupId> |
178 | <artifactId>h2</artifactId> | 174 | <artifactId>h2</artifactId> |
179 | </dependency> | 175 | </dependency> |
180 | - <dependency> | ||
181 | - <groupId>org.springframework.boot</groupId> | ||
182 | - <artifactId>spring-boot-starter-data-jpa</artifactId> | ||
183 | - </dependency> | ||
184 | </dependencies> | 176 | </dependencies> |
185 | <build> | 177 | <build> |
186 | <plugins> | 178 | <plugins> |
@@ -57,16 +57,25 @@ public class ServiceCacheConfiguration { | @@ -57,16 +57,25 @@ public class ServiceCacheConfiguration { | ||
57 | Config config = new Config(); | 57 | Config config = new Config(); |
58 | 58 | ||
59 | if (zkEnabled) { | 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 | MapConfig deviceCredentialsCacheConfig = new MapConfig(CacheConstants.DEVICE_CREDENTIALS_CACHE); | 79 | MapConfig deviceCredentialsCacheConfig = new MapConfig(CacheConstants.DEVICE_CREDENTIALS_CACHE); |
71 | deviceCredentialsCacheConfig.setTimeToLiveSeconds(cacheDeviceCredentialsTTL); | 80 | deviceCredentialsCacheConfig.setTimeToLiveSeconds(cacheDeviceCredentialsTTL); |
72 | deviceCredentialsCacheConfig.setEvictionPolicy(EvictionPolicy.LRU); | 81 | deviceCredentialsCacheConfig.setEvictionPolicy(EvictionPolicy.LRU); |
@@ -75,9 +84,7 @@ public class ServiceCacheConfiguration { | @@ -75,9 +84,7 @@ public class ServiceCacheConfiguration { | ||
75 | cacheDeviceCredentialsMaxSizeSize, | 84 | cacheDeviceCredentialsMaxSizeSize, |
76 | MaxSizeConfig.MaxSizePolicy.valueOf(cacheDeviceCredentialsMaxSizePolicy)) | 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 | @Bean | 90 | @Bean |
@@ -15,25 +15,18 @@ | @@ -15,25 +15,18 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.dao.device; | 16 | package org.thingsboard.server.dao.device; |
17 | 17 | ||
18 | -import org.springframework.cache.annotation.CacheEvict; | ||
19 | -import org.springframework.cache.annotation.Cacheable; | ||
20 | import org.thingsboard.server.common.data.id.DeviceId; | 18 | import org.thingsboard.server.common.data.id.DeviceId; |
21 | import org.thingsboard.server.common.data.security.DeviceCredentials; | 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 | public interface DeviceCredentialsService { | 21 | public interface DeviceCredentialsService { |
26 | 22 | ||
27 | DeviceCredentials findDeviceCredentialsByDeviceId(DeviceId deviceId); | 23 | DeviceCredentials findDeviceCredentialsByDeviceId(DeviceId deviceId); |
28 | 24 | ||
29 | - @Cacheable(cacheNames = DEVICE_CREDENTIALS_CACHE, unless="#result == null") | ||
30 | DeviceCredentials findDeviceCredentialsByCredentialsId(String credentialsId); | 25 | DeviceCredentials findDeviceCredentialsByCredentialsId(String credentialsId); |
31 | 26 | ||
32 | - @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, keyGenerator="previousDeviceCredentialsId", beforeInvocation = true) | ||
33 | DeviceCredentials updateDeviceCredentials(DeviceCredentials deviceCredentials); | 27 | DeviceCredentials updateDeviceCredentials(DeviceCredentials deviceCredentials); |
34 | 28 | ||
35 | DeviceCredentials createDeviceCredentials(DeviceCredentials deviceCredentials); | 29 | DeviceCredentials createDeviceCredentials(DeviceCredentials deviceCredentials); |
36 | 30 | ||
37 | - @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, key="#deviceCredentials.credentialsId") | ||
38 | void deleteDeviceCredentials(DeviceCredentials deviceCredentials); | 31 | void deleteDeviceCredentials(DeviceCredentials deviceCredentials); |
39 | } | 32 | } |
@@ -18,6 +18,8 @@ package org.thingsboard.server.dao.device; | @@ -18,6 +18,8 @@ package org.thingsboard.server.dao.device; | ||
18 | 18 | ||
19 | import lombok.extern.slf4j.Slf4j; | 19 | import lombok.extern.slf4j.Slf4j; |
20 | import org.springframework.beans.factory.annotation.Autowired; | 20 | import org.springframework.beans.factory.annotation.Autowired; |
21 | +import org.springframework.cache.annotation.CacheEvict; | ||
22 | +import org.springframework.cache.annotation.Cacheable; | ||
21 | import org.springframework.stereotype.Service; | 23 | import org.springframework.stereotype.Service; |
22 | import org.springframework.util.StringUtils; | 24 | import org.springframework.util.StringUtils; |
23 | import org.thingsboard.server.common.data.Device; | 25 | import org.thingsboard.server.common.data.Device; |
@@ -28,6 +30,7 @@ import org.thingsboard.server.dao.EncryptionUtil; | @@ -28,6 +30,7 @@ import org.thingsboard.server.dao.EncryptionUtil; | ||
28 | import org.thingsboard.server.dao.exception.DataValidationException; | 30 | import org.thingsboard.server.dao.exception.DataValidationException; |
29 | import org.thingsboard.server.dao.service.DataValidator; | 31 | import org.thingsboard.server.dao.service.DataValidator; |
30 | 32 | ||
33 | +import static org.thingsboard.server.common.data.CacheConstants.DEVICE_CREDENTIALS_CACHE; | ||
31 | import static org.thingsboard.server.dao.service.Validator.validateId; | 34 | import static org.thingsboard.server.dao.service.Validator.validateId; |
32 | import static org.thingsboard.server.dao.service.Validator.validateString; | 35 | import static org.thingsboard.server.dao.service.Validator.validateString; |
33 | 36 | ||
@@ -49,6 +52,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { | @@ -49,6 +52,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { | ||
49 | } | 52 | } |
50 | 53 | ||
51 | @Override | 54 | @Override |
55 | + @Cacheable(cacheNames = DEVICE_CREDENTIALS_CACHE, unless="#result == null") | ||
52 | public DeviceCredentials findDeviceCredentialsByCredentialsId(String credentialsId) { | 56 | public DeviceCredentials findDeviceCredentialsByCredentialsId(String credentialsId) { |
53 | log.trace("Executing findDeviceCredentialsByCredentialsId [{}]", credentialsId); | 57 | log.trace("Executing findDeviceCredentialsByCredentialsId [{}]", credentialsId); |
54 | validateString(credentialsId, "Incorrect credentialsId " + credentialsId); | 58 | validateString(credentialsId, "Incorrect credentialsId " + credentialsId); |
@@ -56,6 +60,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { | @@ -56,6 +60,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { | ||
56 | } | 60 | } |
57 | 61 | ||
58 | @Override | 62 | @Override |
63 | + @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, keyGenerator="previousDeviceCredentialsId", beforeInvocation = true) | ||
59 | public DeviceCredentials updateDeviceCredentials(DeviceCredentials deviceCredentials) { | 64 | public DeviceCredentials updateDeviceCredentials(DeviceCredentials deviceCredentials) { |
60 | return saveOrUpdare(deviceCredentials); | 65 | return saveOrUpdare(deviceCredentials); |
61 | } | 66 | } |
@@ -82,6 +87,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { | @@ -82,6 +87,7 @@ public class DeviceCredentialsServiceImpl implements DeviceCredentialsService { | ||
82 | } | 87 | } |
83 | 88 | ||
84 | @Override | 89 | @Override |
90 | + @CacheEvict(cacheNames = DEVICE_CREDENTIALS_CACHE, key="#deviceCredentials.credentialsId") | ||
85 | public void deleteDeviceCredentials(DeviceCredentials deviceCredentials) { | 91 | public void deleteDeviceCredentials(DeviceCredentials deviceCredentials) { |
86 | log.trace("Executing deleteDeviceCredentials [{}]", deviceCredentials); | 92 | log.trace("Executing deleteDeviceCredentials [{}]", deviceCredentials); |
87 | deviceCredentialsDao.removeById(deviceCredentials.getUuidId()); | 93 | deviceCredentialsDao.removeById(deviceCredentials.getUuidId()); |
@@ -15,13 +15,9 @@ | @@ -15,13 +15,9 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.dao.service; | 16 | package org.thingsboard.server.dao.service; |
17 | 17 | ||
18 | -import com.datastax.driver.core.utils.UUIDs; | ||
19 | import com.hazelcast.core.HazelcastInstance; | 18 | import com.hazelcast.core.HazelcastInstance; |
20 | import org.apache.commons.lang3.RandomStringUtils; | 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 | import org.springframework.aop.framework.Advised; | 21 | import org.springframework.aop.framework.Advised; |
26 | import org.springframework.aop.support.AopUtils; | 22 | import org.springframework.aop.support.AopUtils; |
27 | import org.springframework.beans.factory.annotation.Autowired; | 23 | import org.springframework.beans.factory.annotation.Autowired; |
@@ -56,7 +52,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { | @@ -56,7 +52,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { | ||
56 | @Autowired | 52 | @Autowired |
57 | private HazelcastInstance hazelcastInstance; | 53 | private HazelcastInstance hazelcastInstance; |
58 | 54 | ||
59 | - private UUID deviceId = UUIDs.timeBased(); | 55 | + private UUID deviceId = UUID.randomUUID(); |
60 | 56 | ||
61 | @Before | 57 | @Before |
62 | public void setup() throws Exception { | 58 | public void setup() throws Exception { |
@@ -115,7 +111,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { | @@ -115,7 +111,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { | ||
115 | 111 | ||
116 | when(deviceCredentialsDao.findByDeviceId(deviceId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1)); | 112 | when(deviceCredentialsDao.findByDeviceId(deviceId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1)); |
117 | 113 | ||
118 | - UUID deviceCredentialsId = UUIDs.timeBased(); | 114 | + UUID deviceCredentialsId = UUID.randomUUID(); |
119 | when(deviceCredentialsDao.findById(deviceCredentialsId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1)); | 115 | when(deviceCredentialsDao.findById(deviceCredentialsId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1)); |
120 | when(deviceService.findDeviceById(new DeviceId(deviceId))).thenReturn(new Device()); | 116 | when(deviceService.findDeviceById(new DeviceId(deviceId))).thenReturn(new Device()); |
121 | 117 | ||
@@ -140,7 +136,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { | @@ -140,7 +136,7 @@ public class DeviceCredentialsCacheTest extends AbstractServiceTest { | ||
140 | } | 136 | } |
141 | 137 | ||
142 | private DeviceCredentials createDummyDeviceCredentialsEntity(String deviceCredentialsId) { | 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 | result.setCredentialsId(deviceCredentialsId); | 140 | result.setCredentialsId(deviceCredentialsId); |
145 | return result; | 141 | return result; |
146 | } | 142 | } |