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,4 +19,5 @@ public class CacheConstants { | ||
19 | public static final String DEVICE_CREDENTIALS_CACHE = "deviceCredentials"; | 19 | public static final String DEVICE_CREDENTIALS_CACHE = "deviceCredentials"; |
20 | public static final String RELATIONS_CACHE = "relations"; | 20 | public static final String RELATIONS_CACHE = "relations"; |
21 | public static final String DEVICE_CACHE = "devices"; | 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,7 +34,7 @@ public interface AssetService { | ||
34 | 34 | ||
35 | ListenableFuture<Asset> findAssetByIdAsync(AssetId assetId); | 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 | Asset saveAsset(Asset asset); | 39 | Asset saveAsset(Asset asset); |
40 | 40 |
@@ -21,6 +21,10 @@ import com.google.common.util.concurrent.Futures; | @@ -21,6 +21,10 @@ import com.google.common.util.concurrent.Futures; | ||
21 | import com.google.common.util.concurrent.ListenableFuture; | 21 | import com.google.common.util.concurrent.ListenableFuture; |
22 | import lombok.extern.slf4j.Slf4j; | 22 | import lombok.extern.slf4j.Slf4j; |
23 | import org.springframework.beans.factory.annotation.Autowired; | 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 | import org.springframework.stereotype.Service; | 28 | import org.springframework.stereotype.Service; |
25 | import org.springframework.util.StringUtils; | 29 | import org.springframework.util.StringUtils; |
26 | import org.thingsboard.server.common.data.Customer; | 30 | import org.thingsboard.server.common.data.Customer; |
@@ -48,15 +52,12 @@ import java.util.ArrayList; | @@ -48,15 +52,12 @@ import java.util.ArrayList; | ||
48 | import java.util.Collections; | 52 | import java.util.Collections; |
49 | import java.util.Comparator; | 53 | import java.util.Comparator; |
50 | import java.util.List; | 54 | import java.util.List; |
51 | -import java.util.Optional; | ||
52 | import java.util.stream.Collectors; | 55 | import java.util.stream.Collectors; |
53 | 56 | ||
57 | +import static org.thingsboard.server.common.data.CacheConstants.ASSET_CACHE; | ||
54 | import static org.thingsboard.server.dao.DaoUtil.toUUIDs; | 58 | import static org.thingsboard.server.dao.DaoUtil.toUUIDs; |
55 | import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; | 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 | @Service | 62 | @Service |
62 | @Slf4j | 63 | @Slf4j |
@@ -75,6 +76,9 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ | @@ -75,6 +76,9 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ | ||
75 | @Autowired | 76 | @Autowired |
76 | private CustomerDao customerDao; | 77 | private CustomerDao customerDao; |
77 | 78 | ||
79 | + @Autowired | ||
80 | + private CacheManager cacheManager; | ||
81 | + | ||
78 | @Override | 82 | @Override |
79 | public Asset findAssetById(AssetId assetId) { | 83 | public Asset findAssetById(AssetId assetId) { |
80 | log.trace("Executing findAssetById [{}]", assetId); | 84 | log.trace("Executing findAssetById [{}]", assetId); |
@@ -89,13 +93,16 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ | @@ -89,13 +93,16 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ | ||
89 | return assetDao.findByIdAsync(assetId.getId()); | 93 | return assetDao.findByIdAsync(assetId.getId()); |
90 | } | 94 | } |
91 | 95 | ||
96 | + @Cacheable(cacheNames = ASSET_CACHE, key = "{#tenantId, #name}") | ||
92 | @Override | 97 | @Override |
93 | - public Optional<Asset> findAssetByTenantIdAndName(TenantId tenantId, String name) { | 98 | + public Asset findAssetByTenantIdAndName(TenantId tenantId, String name) { |
94 | log.trace("Executing findAssetByTenantIdAndName [{}][{}]", tenantId, name); | 99 | log.trace("Executing findAssetByTenantIdAndName [{}][{}]", tenantId, name); |
95 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | 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 | @Override | 106 | @Override |
100 | public Asset saveAsset(Asset asset) { | 107 | public Asset saveAsset(Asset asset) { |
101 | log.trace("Executing saveAsset [{}]", asset); | 108 | log.trace("Executing saveAsset [{}]", asset); |
@@ -122,6 +129,14 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ | @@ -122,6 +129,14 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ | ||
122 | log.trace("Executing deleteAsset [{}]", assetId); | 129 | log.trace("Executing deleteAsset [{}]", assetId); |
123 | validateId(assetId, INCORRECT_ASSET_ID + assetId); | 130 | validateId(assetId, INCORRECT_ASSET_ID + assetId); |
124 | deleteEntityRelations(assetId); | 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 | assetDao.removeById(assetId.getId()); | 140 | assetDao.removeById(assetId.getId()); |
126 | } | 141 | } |
127 | 142 |