diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml
index 18e35c6744..5e1cfde664 100644
--- a/application/src/main/resources/thingsboard.yml
+++ b/application/src/main/resources/thingsboard.yml
@@ -77,6 +77,8 @@ http:
# MQTT server parameters
mqtt:
+ # Enable/disable mqtt transport protocol.
+ enabled: "${MQTT_ENABLED:true}"
bind_address: "${MQTT_BIND_ADDRESS:0.0.0.0}"
bind_port: "${MQTT_BIND_PORT:1883}"
adaptor: "${MQTT_ADAPTOR_NAME:JsonMqttAdaptor}"
@@ -102,6 +104,8 @@ mqtt:
# CoAP server parameters
coap:
+ # Enable/disable coap transport protocol.
+ enabled: "${COAP_ENABLED:true}"
bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}"
bind_port: "${COAP_BIND_PORT:5683}"
adaptor: "${COAP_ADAPTOR_NAME:JsonCoapAdaptor}"
@@ -208,6 +212,18 @@ cache:
policy: "${CACHE_DEVICE_CREDENTIAL_MAX_SIZE_POLICY:PER_NODE}"
size: "${CACHE_DEVICE_CREDENTIAL_MAX_SIZE_SIZE:1000000}"
+caching:
+ specs:
+ relations:
+ timeToLiveInMinutes: 1440
+ maxSize: 100000
+ deviceCredentials:
+ timeToLiveInMinutes: 1440
+ maxSize: 100000
+ devices:
+ timeToLiveInMinutes: 1440
+ maxSize: 100000
+
# Check new version updates parameters
updates:
# Enable/disable updates checking.
diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java
index dfcc63ffea..f6fd9a93e8 100644
--- a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java
+++ b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java
@@ -17,4 +17,6 @@ package org.thingsboard.server.common.data;
public class CacheConstants {
public static final String DEVICE_CREDENTIALS_CACHE = "deviceCredentials";
+ public static final String RELATIONS_CACHE = "relations";
+ public static final String DEVICE_CACHE = "devices";
}
diff --git a/dao/pom.xml b/dao/pom.xml
index d9463e4f9c..75d69343e5 100644
--- a/dao/pom.xml
+++ b/dao/pom.xml
@@ -148,6 +148,10 @@
com.hazelcast
hazelcast
+
+ com.github.ben-manes.caffeine
+ caffeine
+
com.hazelcast
hazelcast-spring
@@ -174,6 +178,10 @@
hsqldb
test
+
+ org.springframework
+ spring-context-support
+
diff --git a/dao/src/main/java/org/thingsboard/server/dao/cache/CacheSpecs.java b/dao/src/main/java/org/thingsboard/server/dao/cache/CacheSpecs.java
new file mode 100644
index 0000000000..82cbebf375
--- /dev/null
+++ b/dao/src/main/java/org/thingsboard/server/dao/cache/CacheSpecs.java
@@ -0,0 +1,24 @@
+/**
+ * Copyright © 2016-2017 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.dao.cache;
+
+import lombok.Data;
+
+@Data
+public class CacheSpecs {
+ private Integer timeToLiveInMinutes;
+ private Integer maxSize;
+}
diff --git a/dao/src/main/java/org/thingsboard/server/dao/cache/PreviousDeviceCredentialsIdKeyGenerator.java b/dao/src/main/java/org/thingsboard/server/dao/cache/PreviousDeviceCredentialsIdKeyGenerator.java
index aa4004343d..6b4fccdde1 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/cache/PreviousDeviceCredentialsIdKeyGenerator.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/cache/PreviousDeviceCredentialsIdKeyGenerator.java
@@ -23,6 +23,8 @@ import java.lang.reflect.Method;
public class PreviousDeviceCredentialsIdKeyGenerator implements KeyGenerator {
+ private static final String NOT_VALID_DEVICE = "notValidDeviceCredentialsId";
+
@Override
public Object generate(Object o, Method method, Object... objects) {
DeviceCredentialsService deviceCredentialsService = (DeviceCredentialsService) o;
@@ -33,6 +35,6 @@ public class PreviousDeviceCredentialsIdKeyGenerator implements KeyGenerator {
return oldDeviceCredentials.getCredentialsId();
}
}
- return null;
+ return NOT_VALID_DEVICE;
}
}
diff --git a/dao/src/main/java/org/thingsboard/server/dao/cache/ServiceCacheConfiguration.java b/dao/src/main/java/org/thingsboard/server/dao/cache/ServiceCacheConfiguration.java
index 35391785d0..ba21cb204c 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/cache/ServiceCacheConfiguration.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/cache/ServiceCacheConfiguration.java
@@ -15,76 +15,57 @@
*/
package org.thingsboard.server.dao.cache;
-import com.hazelcast.config.*;
-import com.hazelcast.core.Hazelcast;
-import com.hazelcast.core.HazelcastInstance;
-import com.hazelcast.instance.GroupProperty;
-import com.hazelcast.spring.cache.HazelcastCacheManager;
-import com.hazelcast.zookeeper.ZookeeperDiscoveryProperties;
-import com.hazelcast.zookeeper.ZookeeperDiscoveryStrategyFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.Ticker;
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.interceptor.KeyGenerator;
+import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.thingsboard.server.common.data.CacheConstants;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
@Configuration
+@ConfigurationProperties(prefix = "caching")
@EnableCaching
-@ConditionalOnProperty(prefix = "cache", value = "enabled", havingValue = "true")
+@Data
public class ServiceCacheConfiguration {
- private static final String HAZELCAST_CLUSTER_NAME = "hazelcast";
-
- @Value("${cache.device_credentials.max_size.size}")
- private Integer cacheDeviceCredentialsMaxSizeSize;
- @Value("${cache.device_credentials.max_size.policy}")
- private String cacheDeviceCredentialsMaxSizePolicy;
- @Value("${cache.device_credentials.time_to_live}")
- private Integer cacheDeviceCredentialsTTL;
-
- @Value("${zk.enabled}")
- private boolean zkEnabled;
- @Value("${zk.url}")
- private String zkUrl;
- @Value("${zk.zk_dir}")
- private String zkDir;
+ private Map specs;
@Bean
- public HazelcastInstance hazelcastInstance() {
- Config config = new Config();
-
- if (zkEnabled) {
- addZkConfig(config);
+ public CacheManager cacheManager() {
+ SimpleCacheManager manager = new SimpleCacheManager();
+ if (specs != null) {
+ List caches =
+ specs.entrySet().stream()
+ .map(entry -> buildCache(entry.getKey(),
+ entry.getValue()))
+ .collect(Collectors.toList());
+ manager.setCaches(caches);
}
-
- config.addMapConfig(createDeviceCredentialsCacheConfig());
-
- return Hazelcast.newHazelcastInstance(config);
+ return manager;
}
- private void addZkConfig(Config config) {
- config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
- config.setProperty(GroupProperty.DISCOVERY_SPI_ENABLED.getName(), Boolean.TRUE.toString());
- DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(new ZookeeperDiscoveryStrategyFactory());
- discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_URL.key(), zkUrl);
- discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.ZOOKEEPER_PATH.key(), zkDir);
- discoveryStrategyConfig.addProperty(ZookeeperDiscoveryProperties.GROUP.key(), HAZELCAST_CLUSTER_NAME);
- config.getNetworkConfig().getJoin().getDiscoveryConfig().addDiscoveryStrategyConfig(discoveryStrategyConfig);
+ private CaffeineCache buildCache(String name, CacheSpecs cacheSpec) {
+ final Caffeine