committed by
Andrew Shvayka
2 changed files with 72 additions and 10 deletions
@ -0,0 +1,63 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2021 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.attributes; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.cache.Cache; |
||||
|
import org.springframework.cache.CacheManager; |
||||
|
import org.springframework.context.annotation.Primary; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
||||
|
|
||||
|
import static org.thingsboard.server.common.data.CacheConstants.ATTRIBUTES_CACHE; |
||||
|
|
||||
|
@Service |
||||
|
@ConditionalOnProperty(prefix = "cache.attributes", value = "enabled", havingValue = "true") |
||||
|
@Primary |
||||
|
@Slf4j |
||||
|
public class AttributesCacheWrapper { |
||||
|
private final Cache attributesCache; |
||||
|
|
||||
|
public AttributesCacheWrapper(CacheManager cacheManager) { |
||||
|
this.attributesCache = cacheManager.getCache(ATTRIBUTES_CACHE); |
||||
|
} |
||||
|
|
||||
|
public Cache.ValueWrapper get(AttributeCacheKey attributeCacheKey) { |
||||
|
try { |
||||
|
return attributesCache.get(attributeCacheKey); |
||||
|
} catch (Exception e) { |
||||
|
log.debug("Failed to retrieve element from cache for key {}. Reason - {}.", attributeCacheKey, e.getMessage()); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void put(AttributeCacheKey attributeCacheKey, AttributeKvEntry attributeKvEntry) { |
||||
|
try { |
||||
|
attributesCache.put(attributeCacheKey, attributeKvEntry); |
||||
|
} catch (Exception e) { |
||||
|
log.debug("Failed to put element from cache for key {}. Reason - {}.", attributeCacheKey, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void evict(AttributeCacheKey attributeCacheKey) { |
||||
|
try { |
||||
|
attributesCache.evict(attributeCacheKey); |
||||
|
} catch (Exception e) { |
||||
|
log.debug("Failed to evict element from cache for key {}. Reason - {}.", attributeCacheKey, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue