21 changed files with 412 additions and 160 deletions
@ -1,59 +0,0 @@ |
|||
package org.thingsboard.server.cache; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.cache.Cache; |
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.ConcurrentMap; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true) |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class CaffeineTbTransactionalCacheManager implements TbTransactionalCache { |
|||
|
|||
private final CacheManager cacheManager; |
|||
private final ConcurrentMap<String, CaffeineCacheTransactionStorage> caches = new ConcurrentHashMap<>(); |
|||
|
|||
@Override |
|||
public <K extends Serializable> Cache.ValueWrapper get(String cacheName, K key) { |
|||
return cacheManager.getCache(cacheName).get(key); |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable, V extends Serializable> void putIfAbsent(String cacheName, K key, V value) { |
|||
getCache(cacheName).putIfAbsent(key, value); |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable> void evict(String cacheName, K key) { |
|||
getCache(cacheName).evict(key); |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable> TbCacheTransaction newTransactionForKey(String cacheName, K key) { |
|||
return getCache(cacheName).newTransaction(Collections.singletonList(key)); |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable> TbCacheTransaction newTransactionForKeys(String cacheName, List<K> keys) { |
|||
return getCache(cacheName).newTransaction(keys); |
|||
} |
|||
|
|||
private CaffeineCacheTransactionStorage getCache(String cacheName) { |
|||
return caches.computeIfAbsent(cacheName, cn -> new CaffeineCacheTransactionStorage(cacheName, this)); |
|||
} |
|||
|
|||
<K extends Serializable, V extends Serializable> void doPutIfAbsent(String cacheName, Object key, Object value) { |
|||
cacheManager.getCache(cacheName).putIfAbsent(key, value); |
|||
} |
|||
|
|||
<K extends Serializable> void doEvict(String cacheName, K key) { |
|||
cacheManager.getCache(cacheName).evict(key); |
|||
} |
|||
} |
|||
@ -1,42 +0,0 @@ |
|||
package org.thingsboard.server.cache; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.cache.Cache; |
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.List; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class RedisTbTransactionalCacheManager implements TbTransactionalCache { |
|||
|
|||
private final CacheManager cacheManager; |
|||
|
|||
@Override |
|||
public <K extends Serializable> Cache.ValueWrapper get(String cacheName, K key) { |
|||
return cacheManager.getCache(cacheName).get(key); |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable, V extends Serializable> void putIfAbsent(String cacheName, K key, V value) { |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable> void evict(String cacheName, K key) { |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable> TbCacheTransaction newTransactionForKey(String cacheName, K key) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public <K extends Serializable> TbCacheTransaction newTransactionForKeys(String cacheName, List<K> keys) { |
|||
return null; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.cache; |
|||
|
|||
public interface TbCacheValueWrapper<T> { |
|||
|
|||
T get(); |
|||
|
|||
} |
|||
@ -1,20 +1,35 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.cache; |
|||
|
|||
import org.springframework.cache.Cache; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.List; |
|||
|
|||
public interface TbTransactionalCache { |
|||
public interface TbTransactionalCache<K extends Serializable, V extends Serializable> { |
|||
|
|||
String getCacheName(); |
|||
|
|||
<K extends Serializable> Cache.ValueWrapper get(String cacheName, K key); |
|||
TbCacheValueWrapper<V> get(K key); |
|||
|
|||
<K extends Serializable, V extends Serializable> void putIfAbsent(String cacheName, K key, V value); |
|||
void putIfAbsent(K key, V value); |
|||
|
|||
<K extends Serializable> void evict(String cacheName, K key); |
|||
void evict(K key); |
|||
|
|||
<K extends Serializable> TbCacheTransaction newTransactionForKey(String cacheName, K key); |
|||
TbCacheTransaction<K, V> newTransactionForKey(K key); |
|||
|
|||
<K extends Serializable> TbCacheTransaction newTransactionForKeys(String cacheName, List<K> keys); |
|||
TbCacheTransaction<K, V> newTransactionForKeys(List<K> keys); |
|||
|
|||
} |
|||
|
|||
@ -0,0 +1,33 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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 org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.CacheConstants; |
|||
import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
|||
import org.thingsboard.server.dao.cache.CaffeineTbTransactionalCache; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true) |
|||
@Service("AttributeCache") |
|||
public class AttributeCaffeineCache extends CaffeineTbTransactionalCache<AttributeCacheKey, AttributeKvEntry> { |
|||
|
|||
public AttributeCaffeineCache(CacheManager cacheManager) { |
|||
super(cacheManager, CacheConstants.ATTRIBUTES_CACHE); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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 org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.data.redis.connection.RedisConnectionFactory; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.CacheConstants; |
|||
import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
|||
import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") |
|||
@Service("AttributeCache") |
|||
public class AttributeRedisCache extends RedisTbTransactionalCache<AttributeCacheKey, AttributeKvEntry> { |
|||
|
|||
public AttributeRedisCache(CacheManager cacheManager, RedisConnectionFactory connectionFactory) { |
|||
super(cacheManager, CacheConstants.ATTRIBUTES_CACHE, connectionFactory); |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.cache; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.thingsboard.server.cache.TbCacheTransaction; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.HashMap; |
|||
import java.util.HashSet; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.locks.Lock; |
|||
import java.util.concurrent.locks.ReentrantLock; |
|||
|
|||
@RequiredArgsConstructor |
|||
class CaffeineCacheTransactionStorage<K extends Serializable, V extends Serializable> { |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.cache; |
|||
|
|||
import lombok.Getter; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.data.redis.connection.RedisConnectionFactory; |
|||
import org.thingsboard.server.cache.TbCacheTransaction; |
|||
import org.thingsboard.server.cache.TbCacheValueWrapper; |
|||
import org.thingsboard.server.cache.TbTransactionalCache; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.List; |
|||
|
|||
@RequiredArgsConstructor |
|||
public abstract class RedisTbTransactionalCache<K extends Serializable, V extends Serializable> implements TbTransactionalCache<K, V> { |
|||
|
|||
private final CacheManager cacheManager; |
|||
@Getter |
|||
private final String cacheName; |
|||
private final RedisConnectionFactory connectionFactory; |
|||
|
|||
@Override |
|||
public TbCacheValueWrapper<V> get(K key) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public void putIfAbsent(K key, V value) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void evict(K key) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public TbCacheTransaction<K, V> newTransactionForKey(K key) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public TbCacheTransaction<K, V> newTransactionForKeys(List<K> keys) { |
|||
return null; |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
/** |
|||
* Copyright © 2016-2022 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.cache; |
|||
|
|||
import lombok.AccessLevel; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.cache.Cache; |
|||
import org.thingsboard.server.cache.TbCacheValueWrapper; |
|||
|
|||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
|||
public class SimpleTbCacheValueWrapper<T> implements TbCacheValueWrapper<T> { |
|||
|
|||
private final T value; |
|||
|
|||
@Override |
|||
public T get() { |
|||
return value; |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
public static <T> SimpleTbCacheValueWrapper<T> wrap(Cache.ValueWrapper source) { |
|||
return source == null ? null : new SimpleTbCacheValueWrapper<>((T) source.get()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue