使用hazelcast缓存

This commit is contained in:
2024-09-17 23:10:45 +08:00
parent 9b7369650d
commit 8b7771fe67
10 changed files with 75 additions and 13 deletions

10
pom.xml
View File

@@ -430,6 +430,16 @@
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -14,6 +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 lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@@ -146,7 +147,20 @@ public class CacheCtlService extends BaseService<CacheCtlEntity, CacheCtlReposit
.collect(Collectors.toList());
}else {
}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

@@ -20,7 +20,6 @@ import jakarta.persistence.PersistenceContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.MessagePostProcessor;
@@ -83,9 +82,9 @@ public class DataCollectorService
@Async
public void init() {
findAll().stream()
.filter(x -> StringUtils.isNotEmpty(x.getAppKey()))
.forEach(this::createQueue);
// findAll().stream()
// .filter(x -> StringUtils.isNotEmpty(x.getAppKey()))
// .forEach(this::createQueue);
}
public Object getLocalStatus(GetLocalStatusRequest getLocalStatusRequest) {

View File

@@ -79,7 +79,7 @@ public class ResourceService extends BaseService<ResourceEntity, ResourceReposit
@Autowired ApplicationContext applicationContext;
@Cacheable(cacheNames = CACHE_RESOURCE_ALL)
@Cacheable(cacheNames = CACHE_RESOURCE_ALL, key = "'all'")
public List<ResourceDto> allResources(){
return this.repository.findAll().stream().map(mapper::toDto).collect(Collectors.toList());
@@ -248,14 +248,14 @@ public class ResourceService extends BaseService<ResourceEntity, ResourceReposit
this.clearCache();
}
@Cacheable(cacheNames = CACHE_RESOURCE_MENU_TREE)
@Cacheable(cacheNames = CACHE_RESOURCE_MENU_TREE, key = "'all'")
public List<ResourceTreeDto> menuTree() {
return this.mapper.toTreeDto(
this.repository.findAllByTypeAndParentIsNullOrderBySortKey("0"));
}
@Cacheable(cacheNames = CACHE_RESOURCE_API_TREE)
@Cacheable(cacheNames = CACHE_RESOURCE_API_TREE, key = "'all'")
public List<ResourceTreeDto> apiTree() {
return this.mapper.toTreeDto(
this.repository.findAllByTypeAndParentIsNullOrderBySortKey("3"));

View File

@@ -15,16 +15,16 @@ import cn.lihongjie.coal.user.entity.UserEntity;
import cn.lihongjie.coal.user.mapper.UserMapper;
import cn.lihongjie.coal.user.service.UserService;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;
@Component
@@ -57,7 +57,7 @@ public class InitDataRunner implements CommandLineRunner {
@Override
@Transactional
@Async
// @Async
public void run(String... args) throws Exception {
StopWatch stopWatch = new StopWatch("InitDataRunner");

View File

@@ -0,0 +1,23 @@
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.*;
@Configuration
@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);
}
}

View File

@@ -11,4 +11,9 @@ logging:
file: info
console: info
server:
port: 7457
port: 7457
spring:
hazelcast:
config: classpath:hazelcast-test.yaml

View File

@@ -57,7 +57,8 @@ spring:
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.class: org.springframework.scheduling.quartz.LocalDataSourceJobStore
startup-delay: 10s
hazelcast:
config: classpath:hazelcast.yaml
devtools:

View File

@@ -0,0 +1,8 @@
hazelcast:
cluster-name: coal
network:
join:
tcp-ip:
enabled: true
member-list:
- "10.50.15.0-10.50.15.254"

View File

@@ -0,0 +1,2 @@
hazelcast:
cluster-name: coal