CacheManager,spring缓存

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    <diskStore path="java.io.tmpdir" />
    <defaultCache maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />

    <cache name="ORGANAZATION" maxElementsInMemory="10000" eternal="true"
        timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" />

    <!-- <cache name="ORGANAZATION" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" 
        eternal="true" diskPersistent = "true" diskSpoolBufferSizeMB="20" memoryStoreEvictionPolicy="LFU" 
        transactionalMode="off"> <bootstrapCacheLoaderFactory class="com.tianjian.property.service.MyBootstrapCacheLoaderFactory" 
        properties="ASYNCHRONOUS=false"/> </cache> -->


</ehcache>

CacheManager,spring缓存

2.applicationContext-ehcache.xml

<?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:ehcache="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.1.xsd">

    <ehcache:annotation-driven />

    <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:cache/ehcache.xml" />
    </bean>

</beans>

CacheManager,spring缓存

 

 

3.CacheService implements InitializingBean

package com.tianjian.property.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.json.JSONArray;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianjian.property.entity.system.Admin;
import com.tianjian.property.entity.system.Department;
import com.tianjian.property.entity.system.Position;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

@Service
public class CacheService implements InitializingBean {

    @Autowired
    private DepartmentService departmentService;
    
    @Autowired
    private AdminService  adminService;
    
    @Autowired
    private PositionService positionService;
    
    
    //设置机构缓存   格式为<deptCode,Department>
    public void setDepartment(){
            CacheManager manager = CacheManager.create();  
            Cache cache=manager.getCache("ORGANAZATION"); 
            List<Department> rs=departmentService.getAllDepartment();
            Map<String,Department> map=new HashMap<String,Department>();
            for(int i=0;i<rs.size();i++){
                map.put(rs.get(i).getDeptCode(), rs.get(i));
            }
            Element element=new Element("org",map);
            cache.put(element);

            if(cache!=null)
            cache.removeAll();

    }
    
    //岗位缓存
    public void setPosition(){
        CacheManager manager = CacheManager.create();  
        Cache cache=manager.getCache("ORGANAZATION"); 
        List<Position> rs=positionService.getAllPosition();
        Map<String,Position> map=new HashMap<String,Position>();
        for(int i=0;i<rs.size();i++){
            map.put(rs.get(i).getDeptCode()+"-"+rs.get(i).getPositionCode(), rs.get(i));
        }
        Element element=new Element("position",map);
        cache.put(element);
        if(cache!=null)
        cache.removeAll();
    }
    

    
    public void cacheInit(){
        CacheManager manager = CacheManager.create();  
        Cache cache=manager.getCache("ORGANAZATION"); 
        if(cache.get("org")==null){
            setDepartment();
        }
        if(cache.get("position")==null){
            setPosition();
        }
    }
    
    //根据机构编号获取机构
    public Department getDepartmentByDeptCode(String deptCode){
        CacheManager manager = CacheManager.create();  
        Cache cache=manager.getCache("ORGANAZATION"); 
        Map<String,Department> org=(Map<String, Department>) cache.get("org");
        Department dept=org.get(deptCode);
        return dept;
    }
    
    //获取所有部门
    public List<Department> getAllDepartment(){
        return departmentService.getAllDepartment();
    }
    
    
    public List<Department> getChildDepartment(String deptCode,boolean child){
         List<Department> departments=new ArrayList<Department>();
        if(!child){
            return departmentService.getChildDepartment(deptCode);
        }else{
            departments.addAll(departmentService.getChildDepartment(deptCode));
            List<Department>  list=departmentService.getChildDepartment(deptCode);
            for(Department dept:list){
                departments.addAll(getChildDepartment(dept.getDeptCode(),true));
            }
            return departments;
        }
    }
    
    
    //根据部门code获取该部门下所有人员  child是否获取下级人员
    public List<Admin> getAdminByDeptCode(String deptCode,boolean child){
         List<Admin> adminList=new ArrayList<Admin>();
        if(!child){
            adminList.addAll(adminService.getAdminByDeptCode(deptCode));
        }
        if(child){
            adminList.addAll(adminService.getAdminByDeptCode(deptCode));
            List<Department>  list=getChildDepartment(deptCode,true);
            for(Department dept:list){
                adminList.addAll(getAdminByDeptCode(dept.getDeptCode(),true));
            }
        }
        return adminList;
    }
    
    //获取该岗位下所有成员
    public List<Admin> getAdminByPosition(String deptCode,String positionCode){
        CacheManager manager = CacheManager.create();  
        Cache cache=manager.getCache("ORGANAZATION"); 
        Map<String,Position> positionCache=(Map<String, Position>) cache.get("position");
        Position position=positionCache.get(deptCode+"-"+positionCode);
        List<Admin> admin=new ArrayList<Admin>();
        if(position!=null){
            JSONArray json=new JSONArray(position.getPersons());
            List<String> guids=new ArrayList<String>();
            for(int i=0;i<json.length();i++){
                guids.add(json.get(i).toString());
            }
            admin.addAll(adminService.getAdminByGuid(guids));
        }
        return admin;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        cacheInit();        
    }
}
 

CacheManager,spring缓存

 

 

4.DepartmentService

package com.tianjian.property.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tianjian.property.dao.system.DepartmentDao;
import com.tianjian.property.entity.system.Department;

@Service
@Transactional
public class DepartmentService {

    @Autowired
    private DepartmentDao deptDao;
    
    public List<Department> getDepartmentByDeptCode(String deptCode){
        return deptDao.getDepartmentByCode(deptCode);
    }
    
    public List<Department> findAll(String deptCode) {
        List<Department> list = deptDao.searchDept(deptCode);
        return list;
    }

    @Cacheable(value="ORGANAZATION", key="deptAll")  
    public List<Department> getAllDepartment() {
        return deptDao.getAllDepartment();
    }

    @Cacheable(value="ORGANAZATION", key="#deptCode+'Child'")  
    public List<Department> getChildDepartment(String deptCode) {
        return deptDao.getChildDepartment(deptCode);
    }
    
    /*public List<Department> findCompanyCode(String companyCode) {
        List<Department> list = deptDao.searchCompanyCode(companyCode);
        return list;
    }*/
    
    public List<Department> findDeptCode(String deptCode) {
        List<Department> list = deptDao.searchDeptCode(deptCode);
        return list;
    }
}
 

CacheManager,spring缓存

 

maven依赖:

<dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>