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 javax.annotation.Resource; 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.hrs.DictManager; import com.greenline.guahao.biz.manager.hrs.dataobject.DictDO; /** * @Type DictCacheManager * @Desc ? * <ul> * <li>dicts:{id}---?json)</li> * </ul> * @author weirui.shenwr * @date 2012-5-21 * @Version V1.0 */ public class DictCacheManager extends CacheBase { @Resource DictManager dictManager; /** * ?KEY? */ public static final String DICTS = "dicts:"; /** * @param template */ public DictCacheManager(StringRedisTemplate template) { super(template); } /** * ?KEY * * @param dictId * @return String */ public String dictListKey(String dictId) { return DICTS + dictId; } /** * ? * * @return RedisList<String> */ private RedisList<String> dictList(String dictId) { return new DefaultRedisList<String>(dictListKey(dictId), template); } /** * * * @param list */ public void setDicts(String dictId, final List<DictDO> list) { if (StringUtils.isEmpty(dictId) || list == null || list.isEmpty()) { return; } final RedisList<String> redislist = dictList(dictId); 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 (DictDO obj : list) { redislist.add(jackjson.writeString(obj)); } operations.exec(); return null; } }); setExpire(redislist); } /** * ? * * @return List<DictDO> */ public List<DictDO> listDicts(String dictId) { if (StringUtils.isEmpty(dictId)) { return null; } String key = dictListKey(dictId); SortQuery<String> query = SortQueryBuilder.sort(key).noSort().get("#").build(); BulkMapper<DictDO, String> hm = new BulkMapper<DictDO, String>() { @Override public DictDO mapBulk(List<String> bulk) { Iterator<String> iterator = bulk.iterator(); return jackjson.read(iterator.next(), DictDO.class); } }; return template.sort(query, hm); } /** * ? * * @param dictId * @param key * @return String */ public String getDictName(String dictId, String key) { if (StringUtils.isEmpty(dictId) || StringUtils.isEmpty(key)) { return ""; } List<DictDO> list = listDicts(dictId); if (list == null || list.isEmpty()) { // listDictsgetDictValue(String dictId,String key) // ??istDicts? list = dictManager.listDicts(dictId); } if (list == null || list.isEmpty()) { return ""; } for (DictDO dictDO : list) { if (key.equals(dictDO.getValue())) { return dictDO.getName(); } } return ""; } }