mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
redis 二级缓存
This commit is contained in:
20
pom.xml
20
pom.xml
@@ -196,6 +196,26 @@
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jcache -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-jcache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<!-- for Hibernate v4.x -->
|
||||
|
||||
<artifactId>redisson-hibernate-6</artifactId>
|
||||
<version>3.23.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||
<version>3.23.4</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
|
||||
@@ -2,10 +2,12 @@ package cn.lihongjie.coal.permission.entity;
|
||||
|
||||
import cn.lihongjie.coal.resource.entity.ResourceEntity;
|
||||
import cn.lihongjie.coal.base.entity.CommonEntity;
|
||||
import jakarta.persistence.Cacheable;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinTable;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
import java.util.List;
|
||||
@@ -13,12 +15,14 @@ import java.util.List;
|
||||
@Data
|
||||
@Entity
|
||||
@Comment("权限")
|
||||
@Cacheable
|
||||
public class PermissionEntity extends CommonEntity {
|
||||
|
||||
|
||||
@ManyToMany()
|
||||
@JoinTable(foreignKey = @jakarta.persistence.ForeignKey(name = "none" , value = jakarta.persistence.ConstraintMode.NO_CONSTRAINT),
|
||||
inverseForeignKey = @jakarta.persistence.ForeignKey(name = "none" , value = jakarta.persistence.ConstraintMode.NO_CONSTRAINT))
|
||||
@Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
|
||||
private List<ResourceEntity> resources;
|
||||
|
||||
|
||||
|
||||
@@ -18,13 +18,16 @@ import java.util.List;
|
||||
@Comment("资源")
|
||||
@Slf4j
|
||||
@Table(indexes = {@Index(name = "idx_resource_type_code", columnList = "type,code", unique = true)})
|
||||
@Cacheable
|
||||
public class ResourceEntity extends CommonEntity {
|
||||
|
||||
@ManyToMany(mappedBy = "resources")
|
||||
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
|
||||
private List<PermissionEntity> permissions;
|
||||
|
||||
|
||||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
|
||||
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
|
||||
private List<ResourceEntity> children;
|
||||
|
||||
@ManyToOne
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
package cn.lihongjie.coal.role.entity;
|
||||
|
||||
import cn.lihongjie.coal.permission.entity.PermissionEntity;
|
||||
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
|
||||
import cn.lihongjie.coal.permission.entity.PermissionEntity;
|
||||
import cn.lihongjie.coal.user.entity.UserEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
import jakarta.persistence.Cacheable;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.Cache;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
|
||||
@Cacheable
|
||||
public class RoleEntity extends OrgCommonEntity {
|
||||
|
||||
|
||||
|
||||
|
||||
@ManyToMany(mappedBy = "roles")
|
||||
@JsonBackReference
|
||||
@Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
|
||||
private List<UserEntity> users;
|
||||
|
||||
@ManyToMany
|
||||
@Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
|
||||
private List<PermissionEntity> permissions;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -15,13 +16,32 @@ import java.util.Map;
|
||||
public class HibernateConfig {
|
||||
|
||||
|
||||
@Autowired
|
||||
MyRedissonRegionFactory myRedissonRegionFactory;
|
||||
|
||||
|
||||
@Bean
|
||||
HibernatePropertiesCustomizer hibernatePropertiesCustomizer() {
|
||||
return new HibernatePropertiesCustomizer() {
|
||||
@Override
|
||||
public void customize(Map<String, Object> hibernateProperties) {
|
||||
hibernateProperties.put(AvailableSettings.DIALECT, MyPostgreSQLDialect.class.getCanonicalName());
|
||||
// <property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonRegionFactory" />
|
||||
hibernateProperties.put("hibernate.cache.region.factory_class", myRedissonRegionFactory);
|
||||
// hibernateProperties.put(AvailableSettings.SCHEMA_MANAGEMENT_TOOL, MultithreadHibernateSchemaManagementTool.class.getCanonicalName());
|
||||
|
||||
/**
|
||||
* <!-- 2nd level cache activation -->
|
||||
* <property name="hibernate.cache.use_second_level_cache" value="true" />
|
||||
* <property name="hibernate.cache.use_query_cache" value="true" />
|
||||
*
|
||||
* <!-- Redisson can fallback on database if Redis cache is unavailable -->
|
||||
* <property name="hibernate.cache.redisson.fallback" value="true" />
|
||||
*/
|
||||
hibernateProperties.put("hibernate.cache.use_second_level_cache", "true");
|
||||
hibernateProperties.put("hibernate.cache.use_query_cache", "false");
|
||||
hibernateProperties.put("hibernate.cache.redisson.fallback", "true");
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.lihongjie.coal.spring.config;
|
||||
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.hibernate.RedissonRegionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class MyRedissonRegionFactory extends RedissonRegionFactory {
|
||||
|
||||
@Autowired
|
||||
RedissonClient redissonClient;
|
||||
|
||||
@Override
|
||||
protected RedissonClient createRedissonClient(StandardServiceRegistry registry, Map properties) {
|
||||
return redissonClient;
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,19 @@ package cn.lihongjie.coal.user.entity;
|
||||
|
||||
import cn.lihongjie.coal.role.entity.RoleEntity;
|
||||
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
|
||||
import jakarta.persistence.Cacheable;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinTable;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Cacheable
|
||||
public class UserEntity extends OrgCommonEntity {
|
||||
|
||||
|
||||
@@ -31,9 +34,11 @@ public class UserEntity extends OrgCommonEntity {
|
||||
|
||||
@ManyToMany()
|
||||
@JoinTable(indexes = {@jakarta.persistence.Index(name = "idx_user_roles_users_id", columnList = "users_id"), @jakarta.persistence.Index(name = "idx_user_roles_roles_id", columnList = "roles_id")})
|
||||
@Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
|
||||
private List<RoleEntity> roles;
|
||||
|
||||
|
||||
|
||||
@Comment("机构管理员标识")
|
||||
private Boolean orgAdmin;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user