Browse Source

用户企业信息缓存

wangmh 7 years ago
parent
commit
145cd1cf2f

+ 3 - 0
sso-server/build.gradle

@@ -59,6 +59,9 @@ dependencies {
     compile("org.codehaus.jackson:jackson-core-asl:1.9.13")
     compile("org.codehaus.jackson:jackson-mapper-asl:1.9.13")
     compile("net.sf.ehcache:ehcache:2.10.3")
+    compile("org.hibernate:hibernate-ehcache") {
+      exclude(group: 'net.sf.ehcache', module: 'ehcache-core')
+    }
 
     testCompile("org.springframework.boot:spring-boot-starter-test")
 }

+ 34 - 0
sso-server/src/main/java/com/uas/sso/CacheConfiguration.java

@@ -0,0 +1,34 @@
+package com.uas.sso;
+
+import org.springframework.beans.factory.annotation.Configurable;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.ehcache.EhCacheCacheManager;
+import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.core.io.ClassPathResource;
+
+/**
+ * @author wangmh
+ * @create 2018-07-05 20:04
+ * @desc
+ **/
+@Configurable
+@EnableCaching
+public class CacheConfiguration {
+
+    @Bean
+    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
+        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
+        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource(
+                "spring/ehcache.xml"));
+        ehCacheManagerFactoryBean.setShared(true);
+        return ehCacheManagerFactoryBean;
+    }
+
+    @Bean
+    public EhCacheCacheManager cacheManager() {
+        EhCacheCacheManager cacheManager = new EhCacheCacheManager();
+        cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
+        return cacheManager;
+    }
+}

+ 7 - 2
sso-server/src/main/java/com/uas/sso/entity/User.java

@@ -4,16 +4,19 @@ import com.alibaba.fastjson.annotation.JSONField;
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import java.io.Serializable;
-import java.sql.Time;
 import java.sql.Timestamp;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import javax.persistence.*;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.Table;
 
 import com.uas.sso.core.Const;
 import org.codehaus.jackson.annotate.JsonIgnore;
-import org.springframework.transaction.annotation.Transactional;
+import org.hibernate.annotations.*;
+import org.hibernate.annotations.Cache;
 
 /**
  * 用户信息
@@ -24,6 +27,8 @@ import org.springframework.transaction.annotation.Transactional;
 @Entity
 @Table(name = "sso$user")
 @JsonInclude(Include.NON_NULL)
+@Cacheable
+@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "UserCache")
 public class User implements Serializable {
 
     /**

+ 4 - 0
sso-server/src/main/java/com/uas/sso/entity/Userspace.java

@@ -8,6 +8,8 @@ import javax.persistence.*;
 
 import com.uas.sso.core.Const;
 import org.codehaus.jackson.annotate.JsonIgnore;
+import org.hibernate.annotations.Cache;
+import org.hibernate.annotations.CacheConcurrencyStrategy;
 
 /**
  * 企业实体
@@ -17,6 +19,8 @@ import org.codehaus.jackson.annotate.JsonIgnore;
  */
 @Entity
 @Table(name = "sso$userspace")
+@Cacheable
+@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "UserSpaceCache")
 public class Userspace implements Serializable {
 
     /**

+ 11 - 1
sso-server/src/main/resources/application.yml

@@ -7,15 +7,25 @@ spring:
   application:
     name: sso-server
   profiles:
-    active: prod
+    active: dev
   jpa:
     database: MYSQL
     show-sql: false
     properties:
+      javax:
+        persistence:
+          sharedCache:
+            mode: ENABLE_SELECTIVE
       hibernate:
         dialect: org.hibernate.dialect.MySQL5Dialect
         hbm2ddl:
           auto: update
+        generate_statistics: false
+        cache:
+          use_second_level_cache: true
+          use_query_cache: true
+          region:
+            factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
 
   http:
     encoding:

+ 0 - 23
sso-server/src/main/resources/spring/spring-ehcache.xml

@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xmlns:cache="http://www.springframework.org/schema/cache"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans
-        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-        http://www.springframework.org/schema/cache
-        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
-
-    <description>ehcache缓存配置管理文件</description>
-
-    <!-- 启用缓存注解开关 -->
-    <cache:annotation-driven cache-manager="cacheManager"/>
-
-    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
-        <property name="cacheManager" ref="ehcache"/>
-    </bean>
-
-    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
-        <property name="configLocation" value="classpath:spring/ehcache.xml"/>
-    </bean>
-
-</beans>