切换缓存实现

This commit is contained in:
2024-09-20 21:57:24 +08:00
parent 8b7771fe67
commit c7297fc50a
6 changed files with 42 additions and 110 deletions

View File

@@ -115,4 +115,14 @@ rabbitmqctl set_permissions --vhost /coal/test datacollector '' 'sysExchange|amq
```
D:\dev\gost-windows-amd64.exe -L=tcp://:5672/192.168.59.128:5672
```
```
## 问题
```
(HTTP code 500) server error - Ports are not available: exposing port TCP 0.0.0.0:5672 -> 0.0.0.0:0: listen tcp 0.0.0.0:5672: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
net stop hns
net start hns
```

16
pom.xml
View File

@@ -430,14 +430,14 @@
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.hazelcast</groupId>-->
<!-- <artifactId>hazelcast</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.hazelcast</groupId>-->
<!-- <artifactId>hazelcast-spring</artifactId>-->
<!-- </dependency>-->
</dependencies>

View File

@@ -14,7 +14,7 @@ import cn.lihongjie.coal.spring.config.MultiLevelCache;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Splitter;
import com.hazelcast.spring.cache.HazelcastCache;
//import com.hazelcast.spring.cache.HazelcastCache;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@@ -147,18 +147,19 @@ public class CacheCtlService extends BaseService<CacheCtlEntity, CacheCtlReposit
.collect(Collectors.toList());
}else if (cache instanceof HazelcastCache hc){
return hc.getNativeCache().keySet().stream()
.map(
x -> {
CacheCtlDto dto = new CacheCtlDto(request.getId());
dto.setId(request.getId());
dto.setCacheKey(x.toString());
return dto;
})
.collect(Collectors.toList());
}
// else if (cache instanceof HazelcastCache hc){
//
// return hc.getNativeCache().keySet().stream()
// .map(
// x -> {
// CacheCtlDto dto = new CacheCtlDto(request.getId());
// dto.setId(request.getId());
// dto.setCacheKey(x.toString());
// return dto;
// })
// .collect(Collectors.toList());
// }
else {
throw new BizException("不支持的缓存类型");

View File

@@ -1,25 +1,18 @@
package cn.lihongjie.coal.spring.config;
import cn.lihongjie.coal.common.ReflectUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.redisson.api.*;
import org.redisson.spring.cache.RedissonCache;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
@@ -36,78 +29,9 @@ public class CacheConfig {
public RedissonSpringCacheManager redissonCacheManager() {
Map<String, org.redisson.spring.cache.CacheConfig> cacheConfigs = new HashMap<>();
return new RedissonSpringCacheManager(redissonClient, cacheConfigs) {
@Override
protected RMap<Object, Object> getMap(
String name, org.redisson.spring.cache.CacheConfig config) {
return redissonClient.getLocalCachedMap(
name,
LocalCachedMapOptions.defaults()
.storeMode(LocalCachedMapOptions.StoreMode.LOCALCACHE_REDIS)
.cacheProvider(LocalCachedMapOptions.CacheProvider.CAFFEINE)
.syncStrategy(LocalCachedMapOptions.SyncStrategy.INVALIDATE));
}
@Override
public Cache getCache(String name) {
Cache cache = super.getCache(name);
if (Proxy.isProxyClass(cache.getClass())) {
return cache;
}
// create dynamic proxy to intercept cache clear operation
long start = System.currentTimeMillis();
ProxyFactory proxyFactory = new ProxyFactory(cache);
proxyFactory.addAdvice(
new MethodBeforeAdvice() {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
if (method.getName().equals("clear")) {
if (cache instanceof RedissonCache) {
if (cache.getNativeCache()
instanceof RLocalCachedMap<?, ?> rLocalCachedMap) {
log.info("clear local cache {}", name);
rLocalCachedMap.clearLocalCache();
}
}
}
}
});
Cache proxy = (Cache) proxyFactory.getProxy();
Map instanceMap = (Map) ReflectUtils.getFieldValue(this, "instanceMap");
instanceMap.put(name, proxy);
long end = System.currentTimeMillis();
log.info("create proxy for cache {} cost {} ms", name, end - start);
return proxy;
}
};
return new RedissonSpringCacheManager(redissonClient, cacheConfigs);
}
// @Bean
// @Qualifier("caffeineCacheManager")
// public CaffeineCacheManager caffeineCacheManager() {
// CaffeineCacheManager manager = new CaffeineCacheManager();
// manager.setCaffeineSpec(CaffeineSpec.parse("expireAfterWrite=60m"));
// return manager;
// }
}

View File

@@ -2,10 +2,7 @@ package cn.lihongjie.coal.spring.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.*;
@@ -13,11 +10,11 @@ import java.util.*;
@Slf4j
public class HazelcastConfig {
@Bean
@Primary
public CacheManager hazelcastCacheManager(
com.hazelcast.core.HazelcastInstance hazelcastInstance) {
log.info("Creating HazelcastCacheManager");
return new com.hazelcast.spring.cache.HazelcastCacheManager(hazelcastInstance);
}
// @Bean
// @Primary
// public CacheManager hazelcastCacheManager(
// com.hazelcast.core.HazelcastInstance hazelcastInstance) {
// log.info("Creating HazelcastCacheManager");
// return new com.hazelcast.spring.cache.HazelcastCacheManager(hazelcastInstance);
// }
}

View File

@@ -2,7 +2,7 @@ geolite2:
url: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=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&suffix=tar.gz'
server:
port: 7456
port: 17456
servlet:
context-path: ${CONTEXT_PATH:/}