Cache assets when finding them by tenant and name cfr the caching of devices.

This commit is contained in:
Jan Bols 2018-07-19 10:36:18 +02:00
parent 8765575c17
commit 4e91b28a66
3 changed files with 24 additions and 8 deletions

View File

@ -19,4 +19,5 @@ public class CacheConstants {
public static final String DEVICE_CREDENTIALS_CACHE = "deviceCredentials"; public static final String DEVICE_CREDENTIALS_CACHE = "deviceCredentials";
public static final String RELATIONS_CACHE = "relations"; public static final String RELATIONS_CACHE = "relations";
public static final String DEVICE_CACHE = "devices"; public static final String DEVICE_CACHE = "devices";
public static final String ASSET_CACHE = "assets";
} }

View File

@ -34,7 +34,7 @@ public interface AssetService {
ListenableFuture<Asset> findAssetByIdAsync(AssetId assetId); ListenableFuture<Asset> findAssetByIdAsync(AssetId assetId);
Optional<Asset> findAssetByTenantIdAndName(TenantId tenantId, String name); Asset findAssetByTenantIdAndName(TenantId tenantId, String name);
Asset saveAsset(Asset asset); Asset saveAsset(Asset asset);

View File

@ -21,6 +21,10 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
@ -48,15 +52,12 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.thingsboard.server.common.data.CacheConstants.ASSET_CACHE;
import static org.thingsboard.server.dao.DaoUtil.toUUIDs; import static org.thingsboard.server.dao.DaoUtil.toUUIDs;
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
import static org.thingsboard.server.dao.service.Validator.validateId; import static org.thingsboard.server.dao.service.Validator.*;
import static org.thingsboard.server.dao.service.Validator.validateIds;
import static org.thingsboard.server.dao.service.Validator.validatePageLink;
import static org.thingsboard.server.dao.service.Validator.validateString;
@Service @Service
@Slf4j @Slf4j
@ -75,6 +76,9 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
@Autowired @Autowired
private CustomerDao customerDao; private CustomerDao customerDao;
@Autowired
private CacheManager cacheManager;
@Override @Override
public Asset findAssetById(AssetId assetId) { public Asset findAssetById(AssetId assetId) {
log.trace("Executing findAssetById [{}]", assetId); log.trace("Executing findAssetById [{}]", assetId);
@ -89,13 +93,16 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
return assetDao.findByIdAsync(assetId.getId()); return assetDao.findByIdAsync(assetId.getId());
} }
@Cacheable(cacheNames = ASSET_CACHE, key = "{#tenantId, #name}")
@Override @Override
public Optional<Asset> findAssetByTenantIdAndName(TenantId tenantId, String name) { public Asset findAssetByTenantIdAndName(TenantId tenantId, String name) {
log.trace("Executing findAssetByTenantIdAndName [{}][{}]", tenantId, name); log.trace("Executing findAssetByTenantIdAndName [{}][{}]", tenantId, name);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
return assetDao.findAssetsByTenantIdAndName(tenantId.getId(), name); return assetDao.findAssetsByTenantIdAndName(tenantId.getId(), name)
.orElse(null);
} }
@CacheEvict(cacheNames = ASSET_CACHE, key = "{#asset.tenantId, #asset.name}")
@Override @Override
public Asset saveAsset(Asset asset) { public Asset saveAsset(Asset asset) {
log.trace("Executing saveAsset [{}]", asset); log.trace("Executing saveAsset [{}]", asset);
@ -122,6 +129,14 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
log.trace("Executing deleteAsset [{}]", assetId); log.trace("Executing deleteAsset [{}]", assetId);
validateId(assetId, INCORRECT_ASSET_ID + assetId); validateId(assetId, INCORRECT_ASSET_ID + assetId);
deleteEntityRelations(assetId); deleteEntityRelations(assetId);
Cache cache = cacheManager.getCache(ASSET_CACHE);
Asset asset = assetDao.findById(assetId.getId());
List<Object> list = new ArrayList<>();
list.add(asset.getTenantId());
list.add(asset.getName());
cache.evict(list);
assetDao.removeById(assetId.getId()); assetDao.removeById(assetId.getId());
} }