Java tutorial
/* * Project: guahao-portal-biz-core * * File Created at 2012-5-17 * * Copyright 2012 Greenline.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Greenline Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Greenline.com. */ package com.greenline.guahao.biz.manager.cache.hrs; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang.StringUtils; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.BulkMapper; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.SessionCallback; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.query.SortQuery; import org.springframework.data.redis.core.query.SortQueryBuilder; import org.springframework.data.redis.support.collections.DefaultRedisList; import org.springframework.data.redis.support.collections.RedisList; import com.greenline.guahao.biz.manager.cache.CacheBase; import com.greenline.guahao.biz.manager.hrs.dataobject.HospitalDepartmentDO; /** * @Type HospitalCacheManager * @Desc ? * <ul> * <li>dept:{id} ---? -json</li> * <li>cate:{category}:depts ---ID</li> * <li>hdept:{id}---? hash</li> * <li>hdept:{id}:doctor:count---</li> * <li>?ID</li> * <li>?ID</li> * </ul> * @author weirui.shenwr * @date 2012-5-17 * @Version V1.0 */ public class DepartmentCacheManager extends CacheBase { /** * @param template */ public DepartmentCacheManager(StringRedisTemplate template) { super(template); } /** * KEY? */ public static final String HOSPITAL_DEPARTMENT = "hdept:"; /** * KEYhdept:{id} * * @param id * @return */ public String hDeptKey(String id) { return HOSPITAL_DEPARTMENT + id; } /** * key,hosp:{hospId}:hdepts * * @param hospitalId * @return */ public String hDeptsKey(String hospitalId) { return HospitalCacheManager.HOSPITAL + hospitalId + ":hdepts"; } /** * hdept:{id}:doctor:count * * @param hdeptId * @return */ public String hDeptDoctorCntKey(String hdeptId) { return HOSPITAL_DEPARTMENT + hdeptId + ":doctor:cnt"; } public String hDeptDoctorCountedKey(String hdeptId) { return HOSPITAL_DEPARTMENT + hdeptId + ":doctor:counted"; } /** * @param id * @return */ private String hDeptPlatKey(String id) { return HOSPITAL_DEPARTMENT + id + ":plat"; } /** * * @param id * @return */ public BoundHashOperations<String, String, String> deptPlat(String id) { BoundHashOperations<String, String, String> ops = template.boundHashOps(hDeptPlatKey(id)); return ops; } /** * key,cate:{cateid}:depts * * @param category * @return */ public String categoryDeptsKey(String category) { return "cate:" + category + ":depts"; } /** * key,pid:{pid}:depts * * @param parentId * @return */ public String childDeptsKey(String parentId) { return "pid:" + parentId + ":depts"; } /** * * * * @param parentId * @return */ public RedisList<String> hospitalDepts(String hospitalId) { return new DefaultRedisList<String>(hDeptsKey(hospitalId), template); } /** * * * * @param category * @return */ public RedisList<String> categoryDepts(String category) { return new DefaultRedisList<String>(categoryDeptsKey(category), template); } /** * * * * @param category * @return */ public RedisList<String> childDepts(String parentId) { return new DefaultRedisList<String>(childDeptsKey(parentId), template); } /** * Hash? * * @param id * @return */ public BoundHashOperations<String, String, String> hdept(String id) { BoundHashOperations<String, String, String> userOps = template.boundHashOps(hDeptKey(id)); return userOps; } /** * * * @param deptId * @param count */ public void setDeptDoctorCounts(String hospitalId, Map<String, Integer> counts) { if (StringUtils.isNotEmpty(hospitalId) && counts != null) { for (Entry<String, Integer> o : counts.entrySet()) { setDeptDoctorCount(o.getKey(), o.getValue()); } } // setExpire(hDeptDoctorCountedKey(hospitalId), "true"); } /** * ? ??. * * @param hospitalId ID * @return */ public Map<String, Integer> getDeptDoctorCounts(String hospitalId) { if (hospitalId == null) { return null; } // String cacheCounted = template.opsForValue().get(hDeptDoctorCountedKey(hospitalId)); if (StringUtils.isEmpty(cacheCounted)) { return null; } String key = hDeptsKey(hospitalId); SortQuery<String> query = SortQueryBuilder.sort(key).noSort().get("#").get(hDeptDoctorCntKey("*")).build(); BulkMapper<String[], String> hm = new BulkMapper<String[], String>() { @Override public String[] mapBulk(List<String> bulk) { Iterator<String> iterator = bulk.iterator(); String[] d = new String[2]; d[0] = iterator.next(); d[1] = iterator.next(); if (d[1] == null) { d[1] = "0"; } return d; } }; List<String[]> list = template.sort(query, hm); if (list == null) { return null; } Map<String, Integer> ret = new HashMap<String, Integer>(); for (String[] o : list) { ret.put(o[0], toInt(o[1])); } return ret; } /** * * * @param deptId * @param count */ public void setDeptDoctorCount(String deptId, Integer count) { if (StringUtils.isNotEmpty(deptId)) { if (count == null) { count = Integer.valueOf(0); } setExpire(hDeptDoctorCntKey(deptId), count.toString()); } } /** * * * @param deptId * @param count */ public String getDeptDoctorCount(String deptId) { return template.opsForValue().get(hDeptDoctorCntKey(deptId)); } /** * * * @param hospitalId * @param list */ public void setHospitalDepts(String hospitalId, final List<HospitalDepartmentDO> list) { if (hospitalId == null || list == null || list.isEmpty()) { return; } // final RedisList<String> hDepts = hospitalDepts(hospitalId); // hDepts.clear(); // hDepts.getOperations().execute(new SessionCallback<Object>() { @SuppressWarnings({ "unchecked", "rawtypes" }) public Object execute(RedisOperations operations) throws DataAccessException { operations.watch(hDepts.getKey()); operations.multi(); for (HospitalDepartmentDO obj : list) { hDepts.add(obj.getId()); } operations.exec(); return null; } }); setExpire(hDepts); // ? for (HospitalDepartmentDO obj : list) { setHospitalDept(obj.getId(), obj); } } /** * ? * * @param hospitalId ID * @return */ public List<HospitalDepartmentDO> listHospitalDepts(String hospitalId) { if (hospitalId == null) { return null; } String dept = HOSPITAL_DEPARTMENT + "*->"; String key = hDeptsKey(hospitalId); SortQuery<String> query = SortQueryBuilder.sort(key).noSort().get(dept + "id").get(dept + "name") .get(dept + "description").get(dept + "category").get(dept + "deptId").get(dept + "profitType") .build(); BulkMapper<HospitalDepartmentDO, String> hm = new BulkMapper<HospitalDepartmentDO, String>() { @Override public HospitalDepartmentDO mapBulk(List<String> bulk) { Iterator<String> iterator = bulk.iterator(); HospitalDepartmentDO d = new HospitalDepartmentDO(); d.setId(iterator.next()); if (d.getId() == null) { // ?null return null; } d.setName(iterator.next()); d.setDescription(iterator.next()); d.setCategory(iterator.next()); d.setDeptId(iterator.next()); String profitType = iterator.next(); d.setProfitType(profitType == null ? 1 : Integer.valueOf(profitType)); return d; } }; return cleanExpireData(key, template.sort(query, hm)); } /** * * * @param dept */ public void setHospitalDept(String id, HospitalDepartmentDO dept) { if (StringUtils.isEmpty(id) || dept == null) { return; } // save as hash BoundHashOperations<String, String, String> ops = hdept(id); Map<String, String> data = new HashMap<String, String>(); data.put("id", dept.getId()); data.put("name", null2Empty(dept.getName())); data.put("description", null2Empty(dept.getDescription())); data.put("category", null2Empty(dept.getCategory())); data.put("deptId", null2Empty(dept.getDeptId()));// ID data.put("rule", null2Empty(dept.getRule()));// data.put("hospitalId", null2Empty(dept.getHospitalId()));// ID Integer profitType = dept.getProfitType(); if (null == profitType) { profitType = 1; } data.put("profitType", profitType.toString()); ops.putAll(data); setExpire(ops); } /** * ? * * @param id * @return */ public HospitalDepartmentDO getHospitalDept(String id) { if (StringUtils.isEmpty(id)) { return null; } BoundHashOperations<String, String, String> rmap = hdept(id); Map<String, String> data = rmap.entries(); HospitalDepartmentDO hdept = new HospitalDepartmentDO(); hdept.setId(data.get("id")); if (StringUtils.isEmpty(hdept.getId())) { return null; } hdept.setName(data.get("name")); hdept.setDescription(data.get("description")); hdept.setCategory(data.get("category")); hdept.setDeptId(data.get("deptId")); hdept.setRule(data.get("rule")); hdept.setHospitalId(data.get("hospitalId")); String profitType = data.get("profitType"); hdept.setProfitType(profitType == null ? 1 : Integer.valueOf(profitType)); return hdept; } }