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.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.commons.lang.StringUtils; import org.springframework.dao.DataAccessException; 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.cache.annotation.CacheControlResult; import com.greenline.guahao.biz.manager.cache.annotation.CacheControlResult.InvoteControl; import com.greenline.guahao.biz.manager.order.dataobject.ShiftCaseDO; import com.greenline.guahao.biz.manager.order.query.ShiftQuery; /** * ? * * @Type ShiftCaseCacheManager * @Desc hrs???? ??(???) * ?jsonhashcodekey ?????manager?? * <ol> * <li>shiftq:{query.json.hashcode}:-----?value?180</li> * </ol> * @author weirui.shenwr * @date 2012-5-23 * @Version V1.0 */ public class ShiftCaseCacheManager extends CacheBase { /** * ? */ private int detailExpireTime = 15; /** * 60 */ private static long DEFAULT_EXPIRE_TIME = 60; /** * ? */ public static final String SHIFT_QUERY = "shiftq:"; /** * ? */ public static final String SHIFT = "shift:"; /** * ? */ public int getDetailExpireTime() { return detailExpireTime; } /** * ? */ public void setDetailExpireTime(int detailExpireTime) { this.detailExpireTime = detailExpireTime; } /** * @param template */ public ShiftCaseCacheManager(StringRedisTemplate template) { super(template); if (this.getExpireTime() == null) { this.setExpireTime(DEFAULT_EXPIRE_TIME); } } /** * ?key * * @param departmentId * @return */ public String queryKey(ShiftQuery query) { int hashcode = jackjson.writeString(query).hashCode(); return SHIFT_QUERY + String.valueOf(hashcode); } /** * ?key * * @param departmentId * @return */ public String shiftKey(String shiftCaseId) { return SHIFT + shiftCaseId; } public String getIsCachedKey(String key) { return key + "cheched"; } /** * ? * * @return */ private RedisList<String> shiftQueryList(ShiftQuery query) { return new DefaultRedisList<String>(queryKey(query), template); } /** * ? * * @param list */ public void setShiftCases(ShiftQuery query, final List<ShiftCaseDO> list) { if (query == null) { return; } if (list == null || list.isEmpty()) { String key = getIsCachedKey(queryKey(query)); // String isCached = template.opsForValue().get(key); if (StringUtils.isEmpty(isCached)) { setExpire(key, "true"); } return; } // add to list final RedisList<String> redislist = shiftQueryList(query); redislist.clear(); redislist.getOperations().execute(new SessionCallback<Object>() { @SuppressWarnings({ "unchecked", "rawtypes" }) public Object execute(RedisOperations operations) throws DataAccessException { operations.watch(redislist.getKey()); operations.multi(); for (ShiftCaseDO obj : list) { redislist.add(jackjson.writeString(obj)); } operations.exec(); return null; } }); // boolean expire = redislist.expire(this.getExpireTime(), TimeUnit.SECONDS); if (!expire) { // ??key template.delete(queryKey(query)); } } /** * ?? * * @return */ public CacheControlResult<List<ShiftCaseDO>> listShiftCases(ShiftQuery query) { CacheControlResult<List<ShiftCaseDO>> ret = new CacheControlResult<List<ShiftCaseDO>>(); ret.setInvoteControl(InvoteControl.DO_BIZ_FETCH); if (query == null) { ret.setInvoteControl(InvoteControl.STOP_BIZ_FETCH_PUT); return ret; } // key String key = queryKey(query); SortQuery<String> sortQuery = SortQueryBuilder.sort(key).noSort().get("#").build(); BulkMapper<ShiftCaseDO, String> hm = new BulkMapper<ShiftCaseDO, String>() { @Override public ShiftCaseDO mapBulk(List<String> bulk) { Iterator<String> iterator = bulk.iterator(); return jackjson.read(iterator.next(), ShiftCaseDO.class); } }; List<ShiftCaseDO> cacheResult = cleanExpireData(key, template.sort(sortQuery, hm)); if (cacheResult == null || cacheResult.isEmpty()) { String isCached = template.opsForValue().get(getIsCachedKey(key)); if (StringUtils.isNotEmpty(isCached)) { // ????? ret.setInvoteControl(InvoteControl.STOP_BIZ_FETCH_PUT); } } else { ret.setResult(cacheResult); ret.setInvoteControl(InvoteControl.STOP_BIZ_FETCH); } return ret; } public void setShiftCase(String hosptialId, String shiftCaseId, ShiftCaseDO shiftDO) { if (StringUtils.isEmpty(shiftCaseId)) { return; } if (shiftDO == null) { String key = getIsCachedKey(shiftKey(shiftCaseId)); // String isCached = template.opsForValue().get(key); if (StringUtils.isEmpty(isCached)) { template.opsForValue().set(key, "true", detailExpireTime, TimeUnit.SECONDS); } return; } String json = jackjson.writeString(shiftDO); template.opsForValue().set(shiftKey(shiftCaseId), json, detailExpireTime, TimeUnit.SECONDS); } public CacheControlResult<ShiftCaseDO> getShiftCase(String hosptialId, String shiftCaseId) { CacheControlResult<ShiftCaseDO> ret = new CacheControlResult<ShiftCaseDO>(); ret.setInvoteControl(InvoteControl.DO_BIZ_FETCH); if (StringUtils.isEmpty(shiftCaseId)) { ret.setInvoteControl(InvoteControl.STOP_BIZ_FETCH_PUT); return ret; } // key String key = shiftKey(shiftCaseId); ShiftCaseDO cacheResult = jackjson.read(template.opsForValue().get(key), ShiftCaseDO.class); if (cacheResult == null) { String isCached = template.opsForValue().get(getIsCachedKey(key)); if (StringUtils.isNotEmpty(isCached)) { // ????? ret.setInvoteControl(InvoteControl.STOP_BIZ_FETCH_PUT); } } else { ret.setResult(cacheResult); ret.setInvoteControl(InvoteControl.STOP_BIZ_FETCH); } return ret; } }