Commit c0218fd65f8755e6cf4e80db293c4c4b7efc3792
1 parent
fffaeb8617
Exists in
master
and in
3 other branches
增加
Showing 4 changed files with 68 additions and 16 deletions
- platform-biz-patient-service/src/main/java/com/lyms/platform/biz/service/BasicConfigService.java
- platform-biz-service/src/main/java/com/lyms/platform/permission/model/Organization.java
- platform-biz-service/src/main/java/com/lyms/platform/permission/model/Roles.java
- platform-common/src/main/java/com/lyms/platform/common/utils/CacheHelper.java
platform-biz-patient-service/src/main/java/com/lyms/platform/biz/service/BasicConfigService.java
View file @
c0218fd
| 1 | 1 | package com.lyms.platform.biz.service; |
| 2 | 2 | |
| 3 | -import java.util.Collection; | |
| 4 | -import java.util.List; | |
| 5 | - | |
| 6 | -import com.lyms.platform.common.enums.YnEnums; | |
| 7 | -import org.apache.commons.lang.StringUtils; | |
| 8 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | -import org.springframework.data.domain.Sort; | |
| 10 | -import org.springframework.data.domain.Sort.Direction; | |
| 11 | -import org.springframework.stereotype.Service; | |
| 12 | - | |
| 3 | +import com.google.common.cache.*; | |
| 13 | 4 | import com.lyms.platform.biz.dal.IBasicConfigDao; |
| 14 | 5 | import com.lyms.platform.common.dao.operator.MongoCondition; |
| 15 | 6 | import com.lyms.platform.common.dao.operator.MongoOper; |
| 16 | 7 | import com.lyms.platform.common.dao.operator.MongoQuery; |
| 17 | 8 | import com.lyms.platform.common.dao.operator.Page; |
| 9 | +import com.lyms.platform.common.enums.YnEnums; | |
| 10 | +import com.lyms.platform.common.utils.CacheHelper; | |
| 18 | 11 | import com.lyms.platform.pojo.BasicConfig; |
| 19 | 12 | import com.lyms.platform.query.BasicConfigQuery; |
| 13 | +import org.apache.commons.lang.StringUtils; | |
| 14 | +import org.springframework.beans.factory.InitializingBean; | |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 16 | +import org.springframework.data.domain.Sort; | |
| 17 | +import org.springframework.data.domain.Sort.Direction; | |
| 18 | +import org.springframework.stereotype.Service; | |
| 20 | 19 | |
| 20 | +import java.util.Collection; | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.concurrent.TimeUnit; | |
| 23 | + | |
| 21 | 24 | @Service("basicConfigService") |
| 22 | -public class BasicConfigService { | |
| 23 | - | |
| 25 | +public class BasicConfigService implements InitializingBean { | |
| 26 | + | |
| 24 | 27 | @Autowired |
| 25 | 28 | private IBasicConfigDao basicConfigDao; |
| 26 | 29 | |
| 30 | + private LoadingCache<String, BasicConfig> cached=null; | |
| 31 | + | |
| 27 | 32 | public void addBasicConfig(BasicConfig obj) { |
| 28 | 33 | obj.setModifiedDate(System.currentTimeMillis()); |
| 29 | 34 | basicConfigDao.addBasicConfig(obj); |
| 30 | 35 | |
| 31 | 36 | |
| 32 | 37 | |
| 33 | 38 | |
| ... | ... | @@ -52,15 +57,35 @@ |
| 52 | 57 | return basicConfigDao.queryBasicConfig(MongoCondition.newInstance("parentId", "0", MongoOper.IS).and("yn", YnEnums.YES.getId(), MongoOper.IS).toMongoQuery() |
| 53 | 58 | .addOrder(Direction.ASC, "id")); |
| 54 | 59 | } |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 55 | 64 | public BasicConfig getOneBasicConfigById(String id) { |
| 56 | - return basicConfigDao.getOneBasicConfigById(id); | |
| 65 | + try{ | |
| 66 | + return cached.get(id); | |
| 67 | + }catch (Exception e){ | |
| 68 | + } | |
| 69 | + return null; | |
| 57 | 70 | } |
| 71 | + | |
| 58 | 72 | public List<BasicConfig> queryByParentId(String parentId) { |
| 59 | - return basicConfigDao.queryBasicConfig(MongoCondition.newInstance("parentId", parentId, MongoOper.IS).and("yn", YnEnums.YES.getId(),MongoOper.IS).toMongoQuery()); | |
| 73 | + return basicConfigDao.queryBasicConfig(MongoCondition.newInstance("parentId", parentId, MongoOper.IS).and("yn", YnEnums.YES.getId(), MongoOper.IS).toMongoQuery()); | |
| 60 | 74 | } |
| 61 | 75 | |
| 62 | 76 | public Page<BasicConfig> queryGuidesAndPage(int start, int end) { |
| 63 | 77 | return basicConfigDao.findPage(MongoCondition.newInstance().toMongoQuery().start(start).end(end).addOrder(Direction.ASC, "id")); |
| 78 | + } | |
| 79 | + | |
| 80 | + @Override | |
| 81 | + public void afterPropertiesSet() throws Exception { | |
| 82 | + //cache size 为400 缓存3分钟 | |
| 83 | + cached = CacheHelper.cached(new CacheLoader<String, BasicConfig>() { | |
| 84 | + @Override | |
| 85 | + public BasicConfig load(String key) throws Exception { | |
| 86 | + return basicConfigDao.getOneBasicConfigById(key); | |
| 87 | + } | |
| 88 | + },400,3); | |
| 64 | 89 | } |
| 65 | 90 | } |
platform-biz-service/src/main/java/com/lyms/platform/permission/model/Organization.java
View file @
c0218fd
platform-biz-service/src/main/java/com/lyms/platform/permission/model/Roles.java
View file @
c0218fd
platform-common/src/main/java/com/lyms/platform/common/utils/CacheHelper.java
View file @
c0218fd
| 1 | +package com.lyms.platform.common.utils; | |
| 2 | + | |
| 3 | +import com.google.common.cache.*; | |
| 4 | + | |
| 5 | +import java.util.concurrent.TimeUnit; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * 创建本地缓存 | |
| 9 | + * | |
| 10 | + * Created by Administrator on 2016/5/27 0027. | |
| 11 | + */ | |
| 12 | +public class CacheHelper { | |
| 13 | + public static <K, V> LoadingCache<K, V> cached(CacheLoader<K, V> cacheLoader,int size,int minutes) { | |
| 14 | + LoadingCache<K, V> cache = CacheBuilder | |
| 15 | + .newBuilder() | |
| 16 | + .maximumSize(size) | |
| 17 | + .expireAfterAccess(minutes, TimeUnit.MINUTES) | |
| 18 | + .removalListener(new RemovalListener<K, V>() { | |
| 19 | + @Override | |
| 20 | + public void onRemoval(RemovalNotification<K, V> rn) { | |
| 21 | + System.out.println(rn.getKey() + "被移除"); | |
| 22 | + } | |
| 23 | + }) | |
| 24 | + .build(cacheLoader); | |
| 25 | + return cache; | |
| 26 | + } | |
| 27 | +} |