Commit 4e91b28a66b629194a6b784343d4376ee4e8673b
1 parent
8765575c
Cache assets when finding them by tenant and name cfr the caching of devices.
Showing
3 changed files
with
24 additions
and
8 deletions
... | ... | @@ -19,4 +19,5 @@ public class CacheConstants { |
19 | 19 | public static final String DEVICE_CREDENTIALS_CACHE = "deviceCredentials"; |
20 | 20 | public static final String RELATIONS_CACHE = "relations"; |
21 | 21 | public static final String DEVICE_CACHE = "devices"; |
22 | + public static final String ASSET_CACHE = "assets"; | |
22 | 23 | } | ... | ... |
... | ... | @@ -34,7 +34,7 @@ public interface AssetService { |
34 | 34 | |
35 | 35 | ListenableFuture<Asset> findAssetByIdAsync(AssetId assetId); |
36 | 36 | |
37 | - Optional<Asset> findAssetByTenantIdAndName(TenantId tenantId, String name); | |
37 | + Asset findAssetByTenantIdAndName(TenantId tenantId, String name); | |
38 | 38 | |
39 | 39 | Asset saveAsset(Asset asset); |
40 | 40 | ... | ... |
... | ... | @@ -21,6 +21,10 @@ import com.google.common.util.concurrent.Futures; |
21 | 21 | import com.google.common.util.concurrent.ListenableFuture; |
22 | 22 | import lombok.extern.slf4j.Slf4j; |
23 | 23 | import org.springframework.beans.factory.annotation.Autowired; |
24 | +import org.springframework.cache.Cache; | |
25 | +import org.springframework.cache.CacheManager; | |
26 | +import org.springframework.cache.annotation.CacheEvict; | |
27 | +import org.springframework.cache.annotation.Cacheable; | |
24 | 28 | import org.springframework.stereotype.Service; |
25 | 29 | import org.springframework.util.StringUtils; |
26 | 30 | import org.thingsboard.server.common.data.Customer; |
... | ... | @@ -48,15 +52,12 @@ import java.util.ArrayList; |
48 | 52 | import java.util.Collections; |
49 | 53 | import java.util.Comparator; |
50 | 54 | import java.util.List; |
51 | -import java.util.Optional; | |
52 | 55 | import java.util.stream.Collectors; |
53 | 56 | |
57 | +import static org.thingsboard.server.common.data.CacheConstants.ASSET_CACHE; | |
54 | 58 | import static org.thingsboard.server.dao.DaoUtil.toUUIDs; |
55 | 59 | import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
56 | -import static org.thingsboard.server.dao.service.Validator.validateId; | |
57 | -import static org.thingsboard.server.dao.service.Validator.validateIds; | |
58 | -import static org.thingsboard.server.dao.service.Validator.validatePageLink; | |
59 | -import static org.thingsboard.server.dao.service.Validator.validateString; | |
60 | +import static org.thingsboard.server.dao.service.Validator.*; | |
60 | 61 | |
61 | 62 | @Service |
62 | 63 | @Slf4j |
... | ... | @@ -75,6 +76,9 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ |
75 | 76 | @Autowired |
76 | 77 | private CustomerDao customerDao; |
77 | 78 | |
79 | + @Autowired | |
80 | + private CacheManager cacheManager; | |
81 | + | |
78 | 82 | @Override |
79 | 83 | public Asset findAssetById(AssetId assetId) { |
80 | 84 | log.trace("Executing findAssetById [{}]", assetId); |
... | ... | @@ -89,13 +93,16 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ |
89 | 93 | return assetDao.findByIdAsync(assetId.getId()); |
90 | 94 | } |
91 | 95 | |
96 | + @Cacheable(cacheNames = ASSET_CACHE, key = "{#tenantId, #name}") | |
92 | 97 | @Override |
93 | - public Optional<Asset> findAssetByTenantIdAndName(TenantId tenantId, String name) { | |
98 | + public Asset findAssetByTenantIdAndName(TenantId tenantId, String name) { | |
94 | 99 | log.trace("Executing findAssetByTenantIdAndName [{}][{}]", tenantId, name); |
95 | 100 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
96 | - return assetDao.findAssetsByTenantIdAndName(tenantId.getId(), name); | |
101 | + return assetDao.findAssetsByTenantIdAndName(tenantId.getId(), name) | |
102 | + .orElse(null); | |
97 | 103 | } |
98 | 104 | |
105 | + @CacheEvict(cacheNames = ASSET_CACHE, key = "{#asset.tenantId, #asset.name}") | |
99 | 106 | @Override |
100 | 107 | public Asset saveAsset(Asset asset) { |
101 | 108 | log.trace("Executing saveAsset [{}]", asset); |
... | ... | @@ -122,6 +129,14 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ |
122 | 129 | log.trace("Executing deleteAsset [{}]", assetId); |
123 | 130 | validateId(assetId, INCORRECT_ASSET_ID + assetId); |
124 | 131 | deleteEntityRelations(assetId); |
132 | + | |
133 | + Cache cache = cacheManager.getCache(ASSET_CACHE); | |
134 | + Asset asset = assetDao.findById(assetId.getId()); | |
135 | + List<Object> list = new ArrayList<>(); | |
136 | + list.add(asset.getTenantId()); | |
137 | + list.add(asset.getName()); | |
138 | + cache.evict(list); | |
139 | + | |
125 | 140 | assetDao.removeById(assetId.getId()); |
126 | 141 | } |
127 | 142 | ... | ... |