added cache for TBResourceInfo
This commit is contained in:
parent
0d661ba6cc
commit
eab633632a
@ -497,6 +497,9 @@ cache:
|
||||
entityCount:
|
||||
timeToLiveInMinutes: "${CACHE_SPECS_ENTITY_COUNT_TTL:1440}"
|
||||
maxSize: "${CACHE_SPECS_ENTITY_COUNT_MAX_SIZE:100000}"
|
||||
resourceInfo:
|
||||
timeToLiveInMinutes: "${CACHE_SPECS_RESOURCE_INFO_TTL:1440}"
|
||||
maxSize: "${CACHE_SPECS_USER_SETTINGS_MAX_SIZE:100000}"
|
||||
|
||||
# deliberately placed outside 'specs' group above
|
||||
notificationRules:
|
||||
|
||||
45
common/cache/src/main/java/org/thingsboard/server/cache/resourceinfo/ResourceInfoCacheKey.java
vendored
Normal file
45
common/cache/src/main/java/org/thingsboard/server/cache/resourceinfo/ResourceInfoCacheKey.java
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright © 2016-2023 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.cache.resourceinfo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.thingsboard.server.common.data.id.DeviceId;
|
||||
import org.thingsboard.server.common.data.id.TbResourceId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
@Builder
|
||||
public class ResourceInfoCacheKey implements Serializable {
|
||||
|
||||
private final TenantId tenantId;
|
||||
private final TbResourceId tbResourceId;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return tenantId + "_" + tbResourceId;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright © 2016-2023 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.cache.resourceinfo;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.server.cache.CaffeineTbTransactionalCache;
|
||||
import org.thingsboard.server.common.data.CacheConstants;
|
||||
import org.thingsboard.server.common.data.TbResourceInfo;
|
||||
|
||||
|
||||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true)
|
||||
@Service("ResourceInfoCache")
|
||||
public class ResourceInfoCaffeineCache extends CaffeineTbTransactionalCache<ResourceInfoCacheKey, TbResourceInfo> {
|
||||
|
||||
public ResourceInfoCaffeineCache(CacheManager cacheManager) {
|
||||
super(cacheManager, CacheConstants.RESOURCE_INFO_CACHE);
|
||||
}
|
||||
|
||||
}
|
||||
23
common/cache/src/main/java/org/thingsboard/server/cache/resourceinfo/ResourceInfoEvictEvent.java
vendored
Normal file
23
common/cache/src/main/java/org/thingsboard/server/cache/resourceinfo/ResourceInfoEvictEvent.java
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright © 2016-2023 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.cache.resourceinfo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ResourceInfoEvictEvent {
|
||||
private final ResourceInfoCacheKey key;
|
||||
}
|
||||
35
common/cache/src/main/java/org/thingsboard/server/cache/resourceinfo/ResourceInfoRedisCache.java
vendored
Normal file
35
common/cache/src/main/java/org/thingsboard/server/cache/resourceinfo/ResourceInfoRedisCache.java
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright © 2016-2023 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.cache.resourceinfo;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.server.cache.CacheSpecsMap;
|
||||
import org.thingsboard.server.cache.RedisTbTransactionalCache;
|
||||
import org.thingsboard.server.cache.TBRedisCacheConfiguration;
|
||||
import org.thingsboard.server.cache.TbFSTRedisSerializer;
|
||||
import org.thingsboard.server.common.data.CacheConstants;
|
||||
import org.thingsboard.server.common.data.TbResourceInfo;
|
||||
|
||||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis")
|
||||
@Service("ResourceInfoCache")
|
||||
public class ResourceInfoRedisCache extends RedisTbTransactionalCache<ResourceInfoCacheKey, TbResourceInfo> {
|
||||
|
||||
public ResourceInfoRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) {
|
||||
super(CacheConstants.RESOURCE_INFO_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbFSTRedisSerializer<>());
|
||||
}
|
||||
}
|
||||
@ -44,4 +44,5 @@ public class CacheConstants {
|
||||
public static final String USER_SETTINGS_CACHE = "userSettings";
|
||||
public static final String DASHBOARD_TITLES_CACHE = "dashboardTitles";
|
||||
public static final String ENTITY_COUNT_CACHE = "entityCount";
|
||||
public static final String RESOURCE_INFO_CACHE = "resourceInfo";
|
||||
}
|
||||
|
||||
@ -20,7 +20,10 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
import org.thingsboard.server.cache.resourceinfo.ResourceInfoEvictEvent;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.cache.resourceinfo.ResourceInfoCacheKey;
|
||||
import org.thingsboard.server.common.data.ResourceType;
|
||||
import org.thingsboard.server.common.data.TbResource;
|
||||
import org.thingsboard.server.common.data.TbResourceInfo;
|
||||
@ -31,6 +34,7 @@ import org.thingsboard.server.common.data.id.TbResourceId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.page.PageLink;
|
||||
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
|
||||
import org.thingsboard.server.dao.exception.DataValidationException;
|
||||
import org.thingsboard.server.dao.service.DataValidator;
|
||||
import org.thingsboard.server.dao.service.PaginatedRemover;
|
||||
@ -45,7 +49,7 @@ import static org.thingsboard.server.dao.service.Validator.validateId;
|
||||
@Service("TbResourceDaoService")
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class BaseResourceService implements ResourceService {
|
||||
public class BaseResourceService extends AbstractCachedEntityService<ResourceInfoCacheKey, TbResourceInfo, ResourceInfoEvictEvent> implements ResourceService {
|
||||
|
||||
public static final String INCORRECT_RESOURCE_ID = "Incorrect resourceId ";
|
||||
private final TbResourceDao resourceDao;
|
||||
@ -55,10 +59,12 @@ public class BaseResourceService implements ResourceService {
|
||||
@Override
|
||||
public TbResource saveResource(TbResource resource) {
|
||||
resourceValidator.validate(resource, TbResourceInfo::getTenantId);
|
||||
|
||||
try {
|
||||
return resourceDao.save(resource.getTenantId(), resource);
|
||||
TbResource saved = resourceDao.save(resource.getTenantId(), resource);
|
||||
publishEvictEvent(new ResourceInfoEvictEvent(new ResourceInfoCacheKey(resource.getTenantId(), resource.getId())));
|
||||
return saved;
|
||||
} catch (Exception t) {
|
||||
publishEvictEvent(new ResourceInfoEvictEvent(new ResourceInfoCacheKey(resource.getTenantId(), resource.getId())));
|
||||
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
|
||||
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {
|
||||
String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";
|
||||
@ -86,7 +92,9 @@ public class BaseResourceService implements ResourceService {
|
||||
public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) {
|
||||
log.trace("Executing findResourceInfoById [{}] [{}]", tenantId, resourceId);
|
||||
Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
|
||||
return resourceInfoDao.findById(tenantId, resourceId.getId());
|
||||
|
||||
return cache.getAndPutInTransaction(new ResourceInfoCacheKey(tenantId, resourceId),
|
||||
() -> resourceInfoDao.findById(tenantId, resourceId.getId()), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -169,13 +177,9 @@ public class BaseResourceService implements ResourceService {
|
||||
}
|
||||
};
|
||||
|
||||
protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) {
|
||||
if (t instanceof ConstraintViolationException) {
|
||||
return Optional.of((ConstraintViolationException) t);
|
||||
} else if (t.getCause() instanceof ConstraintViolationException) {
|
||||
return Optional.of((ConstraintViolationException) (t.getCause()));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
@TransactionalEventListener(classes = ResourceInfoCacheKey.class)
|
||||
@Override
|
||||
public void handleEvictEvent(ResourceInfoEvictEvent event) {
|
||||
cache.evict(event.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user