Showing
1 changed file
with
14 additions
and
62 deletions
... | ... | @@ -15,7 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.entityview; |
17 | 17 | |
18 | -import com.google.common.util.concurrent.FutureCallback; | |
19 | 18 | import com.google.common.util.concurrent.Futures; |
20 | 19 | import com.google.common.util.concurrent.ListenableFuture; |
21 | 20 | import lombok.extern.slf4j.Slf4j; |
... | ... | @@ -24,6 +23,8 @@ import org.springframework.beans.factory.annotation.Autowired; |
24 | 23 | import org.springframework.cache.Cache; |
25 | 24 | import org.springframework.cache.CacheManager; |
26 | 25 | import org.springframework.cache.annotation.CacheEvict; |
26 | +import org.springframework.cache.annotation.Cacheable; | |
27 | +import org.springframework.cache.annotation.Caching; | |
27 | 28 | import org.springframework.stereotype.Service; |
28 | 29 | import org.thingsboard.server.common.data.Customer; |
29 | 30 | import org.thingsboard.server.common.data.DataConstants; |
... | ... | @@ -40,7 +41,6 @@ import org.thingsboard.server.common.data.page.TextPageData; |
40 | 41 | import org.thingsboard.server.common.data.page.TextPageLink; |
41 | 42 | import org.thingsboard.server.common.data.relation.EntityRelation; |
42 | 43 | import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
43 | -import org.thingsboard.server.dao.DaoUtil; | |
44 | 44 | import org.thingsboard.server.dao.attributes.AttributesService; |
45 | 45 | import org.thingsboard.server.dao.customer.CustomerDao; |
46 | 46 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
... | ... | @@ -49,12 +49,10 @@ import org.thingsboard.server.dao.service.DataValidator; |
49 | 49 | import org.thingsboard.server.dao.service.PaginatedRemover; |
50 | 50 | import org.thingsboard.server.dao.tenant.TenantDao; |
51 | 51 | |
52 | -import javax.annotation.Nullable; | |
53 | 52 | import java.util.ArrayList; |
54 | 53 | import java.util.Arrays; |
55 | 54 | import java.util.Collection; |
56 | 55 | import java.util.List; |
57 | -import java.util.UUID; | |
58 | 56 | import java.util.concurrent.ExecutionException; |
59 | 57 | import java.util.stream.Collectors; |
60 | 58 | |
... | ... | @@ -74,7 +72,6 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
74 | 72 | public static final String INCORRECT_PAGE_LINK = "Incorrect page link "; |
75 | 73 | public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; |
76 | 74 | public static final String INCORRECT_ENTITY_VIEW_ID = "Incorrect entityViewId "; |
77 | - private static final int DEFAULT_LIMIT = 100; | |
78 | 75 | |
79 | 76 | @Autowired |
80 | 77 | private EntityViewDao entityViewDao; |
... | ... | @@ -91,12 +88,15 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
91 | 88 | @Autowired |
92 | 89 | private CacheManager cacheManager; |
93 | 90 | |
94 | - @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityView.tenantId, #entityView.name}") | |
91 | + @Caching(evict = { | |
92 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityView.tenantId, #entityView.entityId}"), | |
93 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityView.id}")}) | |
95 | 94 | @Override |
96 | 95 | public EntityView saveEntityView(EntityView entityView) { |
97 | 96 | log.trace("Executing save entity view [{}]", entityView); |
98 | 97 | entityViewValidator.validate(entityView); |
99 | 98 | EntityView savedEntityView = entityViewDao.save(entityView); |
99 | + | |
100 | 100 | List<ListenableFuture<List<Void>>> futures = new ArrayList<>(); |
101 | 101 | if (savedEntityView.getKeys() != null) { |
102 | 102 | futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.CLIENT_SCOPE, savedEntityView.getKeys().getAttributes().getCs())); |
... | ... | @@ -121,6 +121,7 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
121 | 121 | return saveEntityView(entityView); |
122 | 122 | } |
123 | 123 | |
124 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") | |
124 | 125 | @Override |
125 | 126 | public EntityView unassignEntityViewFromCustomer(EntityViewId entityViewId) { |
126 | 127 | EntityView entityView = findEntityViewById(entityViewId); |
... | ... | @@ -136,34 +137,11 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
136 | 137 | new CustomerEntityViewsUnAssigner(tenantId).removeEntities(customerId); |
137 | 138 | } |
138 | 139 | |
139 | - | |
140 | + @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") | |
140 | 141 | @Override |
141 | 142 | public EntityView findEntityViewById(EntityViewId entityViewId) { |
142 | 143 | log.trace("Executing findEntityViewById [{}]", entityViewId); |
143 | 144 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); |
144 | - List<Object> ids = Arrays.asList(entityViewId.getId()); | |
145 | - Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | |
146 | - EntityView fromCache = cache.get(ids, EntityView.class); | |
147 | - if (fromCache != null) { | |
148 | - return fromCache; | |
149 | - } else { | |
150 | - ListenableFuture<EntityView> entityViewFuture | |
151 | - = Futures.immediateFuture(entityViewDao.findById(entityViewId.getId())); | |
152 | - Futures.addCallback(entityViewFuture, | |
153 | - new FutureCallback<EntityView>() { | |
154 | - @Override | |
155 | - public void onSuccess(@Nullable EntityView result) { | |
156 | - cache.putIfAbsent(ids, result); | |
157 | - } | |
158 | - @Override | |
159 | - public void onFailure(Throwable t) {} | |
160 | - }); | |
161 | - try { | |
162 | - return entityViewFuture.get(); | |
163 | - } catch (Exception e) { | |
164 | - log.error(e.getMessage()); | |
165 | - } | |
166 | - } | |
167 | 145 | return entityViewDao.findById(entityViewId.getId()); |
168 | 146 | } |
169 | 147 | |
... | ... | @@ -203,7 +181,6 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
203 | 181 | } |
204 | 182 | return Futures.successfulAsList(futures); |
205 | 183 | }); |
206 | - | |
207 | 184 | return entityViews; |
208 | 185 | } |
209 | 186 | |
... | ... | @@ -214,39 +191,24 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
214 | 191 | return entityViewDao.findByIdAsync(entityViewId.getId()); |
215 | 192 | } |
216 | 193 | |
194 | + @Cacheable(cacheNames = ENTITY_VIEW_CACHE, key = "{#tenantId, #entityId}") | |
217 | 195 | @Override |
218 | 196 | public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(TenantId tenantId, EntityId entityId) { |
219 | 197 | log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId); |
220 | 198 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
221 | 199 | validateId(entityId.getId(), "Incorrect entityId" + entityId); |
222 | - | |
223 | - List<Object> tenantAndEntityIds = Arrays.asList(tenantId, entityId); | |
224 | - Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | |
225 | - List<EntityView> fromCache = cache.get(tenantAndEntityIds, List.class); | |
226 | - if (fromCache != null) { | |
227 | - return Futures.immediateFuture(fromCache); | |
228 | - } else { | |
229 | - ListenableFuture<List<EntityView>> entityViewsFuture = | |
230 | - entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId()); | |
231 | - Futures.addCallback(entityViewsFuture, | |
232 | - new FutureCallback<List<EntityView>>() { | |
233 | - @Override | |
234 | - public void onSuccess(@Nullable List<EntityView> result) { | |
235 | - cache.putIfAbsent(tenantAndEntityIds, result); | |
236 | - } | |
237 | - @Override | |
238 | - public void onFailure(Throwable t) {} | |
239 | - }); | |
240 | - return entityViewsFuture; | |
241 | - } | |
200 | + return entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId()); | |
242 | 201 | } |
243 | 202 | |
203 | + @CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}") | |
244 | 204 | @Override |
245 | 205 | public void deleteEntityView(EntityViewId entityViewId) { |
246 | 206 | log.trace("Executing deleteEntityView [{}]", entityViewId); |
247 | 207 | validateId(entityViewId, INCORRECT_ENTITY_VIEW_ID + entityViewId); |
248 | 208 | deleteEntityRelations(entityViewId); |
249 | - cacheEvict(entityViewId, cacheManager.getCache(ENTITY_VIEW_CACHE)); | |
209 | + Cache cache = cacheManager.getCache(ENTITY_VIEW_CACHE); | |
210 | + EntityView entityView = entityViewDao.findById(entityViewId.getId()); | |
211 | + cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getEntityId())); | |
250 | 212 | entityViewDao.removeById(entityViewId.getId()); |
251 | 213 | } |
252 | 214 | |
... | ... | @@ -254,19 +216,9 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti |
254 | 216 | public void deleteEntityViewsByTenantId(TenantId tenantId) { |
255 | 217 | log.trace("Executing deleteEntityViewsByTenantId, tenantId [{}]", tenantId); |
256 | 218 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
257 | - entityViewDao.findEntityViewsByTenantId(tenantId.getId(), new TextPageLink(DEFAULT_LIMIT)).stream() | |
258 | - .map(view -> view.getId()) | |
259 | - .collect(Collectors.toList()) | |
260 | - .forEach(id -> cacheEvict(id, cacheManager.getCache(ENTITY_VIEW_CACHE))); | |
261 | 219 | tenantEntityViewRemover.removeEntities(tenantId); |
262 | 220 | } |
263 | 221 | |
264 | - private void cacheEvict(EntityViewId entityViewId, Cache cache) { | |
265 | - EntityView entityView = entityViewDao.findById(entityViewId.getId()); | |
266 | - cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getName())); | |
267 | - cache.evict(Arrays.asList(entityView.getTenantId(), entityView.getEntityId())); | |
268 | - } | |
269 | - | |
270 | 222 | private ListenableFuture<List<Void>> copyAttributesFromEntityToEntityView(EntityView entityView, String scope, Collection<String> keys) { |
271 | 223 | if (keys != null && !keys.isEmpty()) { |
272 | 224 | ListenableFuture<List<AttributeKvEntry>> getAttrFuture = attributesService.find(entityView.getEntityId(), scope, keys); | ... | ... |