refactoring
This commit is contained in:
parent
eab633632a
commit
e2ba34bbf3
@ -499,7 +499,7 @@ cache:
|
|||||||
maxSize: "${CACHE_SPECS_ENTITY_COUNT_MAX_SIZE:100000}"
|
maxSize: "${CACHE_SPECS_ENTITY_COUNT_MAX_SIZE:100000}"
|
||||||
resourceInfo:
|
resourceInfo:
|
||||||
timeToLiveInMinutes: "${CACHE_SPECS_RESOURCE_INFO_TTL:1440}"
|
timeToLiveInMinutes: "${CACHE_SPECS_RESOURCE_INFO_TTL:1440}"
|
||||||
maxSize: "${CACHE_SPECS_USER_SETTINGS_MAX_SIZE:100000}"
|
maxSize: "${CACHE_SPECS_RESOURCE_INFO_MAX_SIZE:100000}"
|
||||||
|
|
||||||
# deliberately placed outside 'specs' group above
|
# deliberately placed outside 'specs' group above
|
||||||
notificationRules:
|
notificationRules:
|
||||||
|
|||||||
@ -13,21 +13,16 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.thingsboard.server.cache.resourceinfo;
|
package org.thingsboard.server.cache.resourceInfo;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
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.TbResourceId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode
|
||||||
@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.thingsboard.server.cache.resourceinfo;
|
package org.thingsboard.server.cache.resourceInfo;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
@ -13,11 +13,14 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.thingsboard.server.cache.resourceinfo;
|
package org.thingsboard.server.cache.resourceInfo;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.thingsboard.server.common.data.id.TbResourceId;
|
||||||
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class ResourceInfoEvictEvent {
|
public class ResourceInfoEvictEvent {
|
||||||
private final ResourceInfoCacheKey key;
|
private final TenantId tenantId;
|
||||||
|
private final TbResourceId resourceId;
|
||||||
}
|
}
|
||||||
@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.thingsboard.server.cache.resourceinfo;
|
package org.thingsboard.server.cache.resourceInfo;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
@ -21,9 +21,10 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.hibernate.exception.ConstraintViolationException;
|
import org.hibernate.exception.ConstraintViolationException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.event.TransactionalEventListener;
|
import org.springframework.transaction.event.TransactionalEventListener;
|
||||||
import org.thingsboard.server.cache.resourceinfo.ResourceInfoEvictEvent;
|
import org.thingsboard.server.cache.device.DeviceCacheKey;
|
||||||
|
import org.thingsboard.server.cache.resourceInfo.ResourceInfoEvictEvent;
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.cache.resourceinfo.ResourceInfoCacheKey;
|
import org.thingsboard.server.cache.resourceInfo.ResourceInfoCacheKey;
|
||||||
import org.thingsboard.server.common.data.ResourceType;
|
import org.thingsboard.server.common.data.ResourceType;
|
||||||
import org.thingsboard.server.common.data.TbResource;
|
import org.thingsboard.server.common.data.TbResource;
|
||||||
import org.thingsboard.server.common.data.TbResourceInfo;
|
import org.thingsboard.server.common.data.TbResourceInfo;
|
||||||
@ -61,10 +62,10 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
|
|||||||
resourceValidator.validate(resource, TbResourceInfo::getTenantId);
|
resourceValidator.validate(resource, TbResourceInfo::getTenantId);
|
||||||
try {
|
try {
|
||||||
TbResource saved = resourceDao.save(resource.getTenantId(), resource);
|
TbResource saved = resourceDao.save(resource.getTenantId(), resource);
|
||||||
publishEvictEvent(new ResourceInfoEvictEvent(new ResourceInfoCacheKey(resource.getTenantId(), resource.getId())));
|
publishEvictEvent(new ResourceInfoEvictEvent(resource.getTenantId(), resource.getId()));
|
||||||
return saved;
|
return saved;
|
||||||
} catch (Exception t) {
|
} catch (Exception t) {
|
||||||
publishEvictEvent(new ResourceInfoEvictEvent(new ResourceInfoCacheKey(resource.getTenantId(), resource.getId())));
|
publishEvictEvent(new ResourceInfoEvictEvent(resource.getTenantId(), resource.getId()));
|
||||||
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
|
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
|
||||||
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {
|
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {
|
||||||
String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";
|
String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";
|
||||||
@ -177,9 +178,11 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@TransactionalEventListener(classes = ResourceInfoCacheKey.class)
|
@TransactionalEventListener(classes = ResourceInfoEvictEvent.class)
|
||||||
@Override
|
@Override
|
||||||
public void handleEvictEvent(ResourceInfoEvictEvent event) {
|
public void handleEvictEvent(ResourceInfoEvictEvent event) {
|
||||||
cache.evict(event.getKey());
|
if (event.getResourceId() != null) {
|
||||||
|
cache.evict(new ResourceInfoCacheKey(event.getTenantId(), event.getResourceId()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user