jedis记录用户常用模块生成实时排名

需求:

  • 拦截用户对不同类型模块的进入,新增,更新,删除 的操作,每次操作频率+1 , 实时显示用户当面所有模块的排名。要求设计时满足对历史数据的排行,比如某月,某年等时间段的排行。

环境:

  • 由于项目没有针对redis 做持久化操作,所以每次项目启动需要重新加载数据库中的数据到缓存中。
  • 由于此项目redis版本较老,使用的是2.6版本,并且使用的是 shardJedis + sentinel 的高可用模式 。
  • 采用jedis访问redis

最终形成页面如下:

数据库设计:

采用2张表:

  • 模块表:主要记录模块的相关信息,访问路径等。

  • 用户排名表:由于需要考虑对历史数据的统计,所以目前一条数据的最小维度为,用户id-模块id-时间具体到天-点击数量。后期可以考虑对点击日期进行分区

自定义注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RedisRank {
	//模块类型 系统台账/系统流程/自定义台账/自定义流程
	public CommonRankType type();

	public CommonRankCustomCode CustomCode() default CommonRankCustomCode.none;
}

统计大类(目前业务中只涉及到台账和流程,所以用1,2区分),如果需要新类型排行追加即可:

public enum CommonRankType {
	//1-台账  2-流程 (用于分组,目前只需要统计台账和流程的排行,不需要区分太细,所以只用了1,2进行类型区分)
	EQU("1"), BILL("2"), CUSTOM_EQU("1"), CUSTOM_BILL("2");
	
	
	
	
	private CommonRankType(String type) {
		this.type = type;
	}

	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}

自定义模块的枚举类,此处只有个枚举属性,如果有更多的模块追加即可:

public enum CommonRankCustomCode {

	none("", "", ""), 
	itomOnlineExamine(
			"模块唯一标识",
			"模块名称",
			"url访问路径");

	private String code;
	private String name;
	private String src;

	private CommonRankCustomCode(String code, String name, String src) {
		this.code = code;
		this.name = name;
		this.src = src;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

}

系统模块配置:

自定义模块配置:

将上述注解放到需要拦截的方法即可,如删除,更新,保存方法上等

自定义注解拦截,需要扩展根据枚举类型追加即可:

@Aspect
@Component
public class RedisCommonRank {
	// private static final Logger logger = LoggerFactory
	// .getLogger(RedisCommonRank.class);
	@Autowired
	ItomComUDynTemplateService itomComUDynTemplateService;
	@Autowired
	ItomTemplateDao itomTemplateDao;

	@After("@annotation(redisRank)")
	public void afterCommonRank(JoinPoint jp, RedisRank redisRank) {
		CommonRankCustomCode customCode = redisRank.CustomCode();
		ItomCommonUseDto itomCommonUseDto = null;
		ItomCommonCache itomCommonCache = null;
		String templateId = null;
		ItomTemplateDTO templateDto = null;
		CommonRankType type = redisRank.type();
		switch (type.name()) {
		case "EQU":// 系统台账
			templateId = DynaTagUtil.templateId.get();
			templateDto = itomTemplateDao.findItomTemplateById(templateId);
			
			itomCommonUseDto = new ItomCommonUseDto();
			itomCommonUseDto.setCode(templateId);
			itomCommonUseDto.setName(templateDto.getTemplateName());
			itomCommonUseDto.setPrefix(CommonRannkPrefix.itomUseRankEquPrefix);
			itomCommonUseDto
					.setSrc("platform/itom/itomequ/itomequ/ItomEquController/ItomEquInfo?templateId="
							+ templateId + "&templateBaseId=AAAAAAAAAA");
			itomCommonUseDto.setType(type.getType());
			itomCommonCache = new ItomCommonCache(itomCommonUseDto);
			itomCommonCache.createCache();

			break;
		case "CUSTOM_EQU":// 自定义开发台账
			itomCommonUseDto = new ItomCommonUseDto();
			itomCommonUseDto.setCode(customCode.getCode());
			itomCommonUseDto.setName(customCode.getName());
			itomCommonUseDto.setPrefix(CommonRannkPrefix.itomUseRankEquPrefix);
			itomCommonUseDto.setSrc(customCode.getSrc());
			itomCommonUseDto.setType(type.getType());
			itomCommonCache = new ItomCommonCache(itomCommonUseDto);
			itomCommonCache.createCache();
			break;

		case "BILL":// 系统流程
			templateId = DynaTagUtil.templateId.get();
			templateDto = itomTemplateDao.findItomTemplateById(templateId);

			itomCommonUseDto = new ItomCommonUseDto();
			itomCommonUseDto.setCode(templateId);
			itomCommonUseDto.setName(templateDto.getTemplateName());
			itomCommonUseDto.setPrefix(CommonRannkPrefix.itomUseRankBillPrefix);
			itomCommonUseDto
					.setSrc("platform/itom/itombill/itombill/ItomBillController/ItomBillInfo?templateId="
							+ templateId + "&templateBaseId=CCCCCCCCCC");
			itomCommonUseDto.setType(type.getType());
			itomCommonCache = new ItomCommonCache(itomCommonUseDto);
			itomCommonCache.createCache();
			break;

		case "CUSTOM_BILL":// 自定义开发流程
			itomCommonUseDto = new ItomCommonUseDto();
			itomCommonUseDto.setCode(customCode.getCode());
			itomCommonUseDto.setName(customCode.getName());
			itomCommonUseDto.setPrefix(CommonRannkPrefix.itomUseRankBillPrefix);
			itomCommonUseDto.setSrc(customCode.getSrc());
			itomCommonUseDto.setType(type.getType());
			itomCommonCache = new ItomCommonCache(itomCommonUseDto);
			itomCommonCache.createCache();
			break;

		default:
			break;
		}
	}

}

redis用到的所有前缀:

public class CommonRannkPrefix {
	public static final String itomUseDelayPrefix = "ITOM_COM_USE_DELAY"; // String<userid,templateId/templateCode>
																			// 延迟key(为了避免用户连续点击,做延迟处理)
	public static final String itomUseRankEquPrefix = "ITOM_COM_USE_EQU_RANK"; // zset_userId<hit,templateId>
																				// 个人排名key(包含所有台账)
	public static final String itomUseRankBillPrefix = "ITOM_COM_USE_BILL_RANK"; // zset_userId<hit,templateId>
																					// 个人排名key(包含所有流程)
	public static final String itomUseTemplatePrefix = "ITOM_COM_USE_TEMPLATE"; // hset<templateId,templateInfo>
																				// 模板资源key
	public static final String itomUseCodeToTemplatePrefix = "ITOM_COM_USE_CODE_TO_TEMPLATEID"; // hset<templateCode,templateId>
																								// 自定义模块和模板的标识key
	public static final String itomUseUserPrefix = "ITOM_COM_USE_USER"; // hset<userId,templateHitInfo>
																		// 定时任务需要更新的用户,以及相关模块的点击量,每次同步后删除此key
	public static final String delaySuffix = "DELAY"; //延迟后缀
	
	public static final HashMap<String,String> rankMap = new HashMap<String,String>(); //排名类型
	static{ //由于只需要统计台站和流程,如果还有其他大类,在此处初始化进来即可
		rankMap.put(CommonRankType.EQU.getType(), itomUseRankEquPrefix);
		rankMap.put(CommonRankType.BILL.getType(), itomUseRankBillPrefix);
	}
	

}

真正的入redis:

@Component
public class ItomCommonCache {
	private static final Logger logger = LoggerFactory
			.getLogger(ItomCommonCache.class);
	ItomCommonUseDto itomCommonUseDto;

	public ItomCommonCache() {
		super();
	}

	public ItomCommonCache(ItomCommonUseDto itomCommonUseDto) {
		this.itomCommonUseDto = itomCommonUseDto;
	}

	public void createCache() {

		ItomComUDynTemplateService itomComUDynTemplateService = SpringFactory
				.getBean(ItomComUDynTemplateService.class);
		BaseCacheManager baseCacheManager = SpringFactory
				.getBean(BaseCacheManager.class);

		JedisSentinelPool pool = null;
		ShardedJedis jedis = null;

		pool = (JedisSentinelPool) SpringFactory.getBean("jedisSentinelPool");
		jedis = (ShardedJedis) pool.getResource();
		try {
			SysUser sysUser = (SysUser) ContextCommonHolder
					.getAttribute(SessionHelper.SESSION_CURRENT_USER);

			// 1.记录操作用户,便于定时任务只更新已操作的用户
			jedis.sadd(CommonRannkPrefix.itomUseUserPrefix, sysUser.getId());

			// 2.是否能获取到缓存中的模板,获取不到,则新增数据,并插入缓存
			String templateId = jedis.hget(
					CommonRannkPrefix.itomUseCodeToTemplatePrefix,
					itomCommonUseDto.getCode());
			if (templateId == null) {// 未获取到模板
				templateId = ComUtil.getId();
				// 3.先生成模板ID与CODE的对应关系插入缓存
				jedis.hset(CommonRannkPrefix.itomUseCodeToTemplatePrefix,
						itomCommonUseDto.getCode(), templateId);
				ItomComUDynTemplateDTO itomComUDynTemplateDTO = new ItomComUDynTemplateDTO();

				itomComUDynTemplateDTO.setId(templateId);
				itomComUDynTemplateDTO.setTemplateCode(itomCommonUseDto
						.getCode());
				itomComUDynTemplateDTO.setTemplateName(itomCommonUseDto
						.getName());
				itomComUDynTemplateDTO
						.setTemplateSrc(itomCommonUseDto.getSrc());
				itomComUDynTemplateDTO.setTemplateType(itomCommonUseDto
						.getType());
				itomComUDynTemplateDTO.setValidFlag("1");

				try {
					// 新增常用模板数据
					itomComUDynTemplateService
							.insertItomComUDynTemplate(itomComUDynTemplateDTO);
				} catch (Exception e) {
					logger.error("常用任务插入未存在的模板时报错:" + e.getMessage());
					jedis.hdel(CommonRannkPrefix.itomUseCodeToTemplatePrefix,
							itomCommonUseDto.getCode());
					baseCacheManager
							.delByField(
									CommonRannkPrefix.itomUseTemplatePrefix,
									templateId);
					return;
				}

				// 定义延迟key ,用户在一定时间内连续点击,只记录1次
				jedis.setex(CommonRannkPrefix.itomUseDelayPrefix + ":"
						+ templateId, 10, "1");

				// 自增一次对应模块的点击数
				jedis.zincrby(
						itomCommonUseDto.getPrefix() + ":" + sysUser.getId(),
						1, templateId);
				// 定时任务需要取出的key
				jedis.zincrby(
						itomCommonUseDto.getPrefix() + ":"
								+ CommonRannkPrefix.delaySuffix + ":"
								+ sysUser.getId(), 1, templateId);
			} else {
				Long setnx = jedis.setnx(CommonRannkPrefix.itomUseDelayPrefix
						+ ":" + sysUser.getId() + ":" + templateId, "1");
				if (setnx == 1) {
					// 延迟记录,避免连续多次点击
					jedis.setex(CommonRannkPrefix.itomUseDelayPrefix + ":"
							+ sysUser.getId() + ":" + templateId, 10, "1");
					// 自增一次对应模块的点击数
					jedis.zincrby(
							itomCommonUseDto.getPrefix() + ":"
									+ sysUser.getId(), 1, templateId);
					// 自增定时任务需要取出的key
					jedis.zincrby(
							itomCommonUseDto.getPrefix() + ":"
									+ CommonRannkPrefix.delaySuffix + ":"
									+ sysUser.getId(), 1, templateId);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (pool != null) {
				pool.returnResource(jedis);
			}

		}

	}
}

采用定时任务,每隔一段时间从redis写入oracle中:

@Component
public class ItomCommonUseTbThread {
	private static final Logger logger = LoggerFactory
			.getLogger(ItomCommonUseTbThread.class);
	private ScheduledExecutorService scheduler = Executors
			.newSingleThreadScheduledExecutor();

	@SuppressWarnings("unused")
	@PostConstruct
	private void tb() {
		scheduler.scheduleWithFixedDelay(runnable, 5, 5, TimeUnit.MINUTES);
	}

	Runnable runnable = new Runnable() {
	

		public void run() {
			long start = System.currentTimeMillis();
			JedisSentinelPool pool = null;
			ShardedJedis jedis = null;
			int updateCount = 0;
			int insertCount = 0;
			try {
				ItomComUDynRankService itomComUDynRankService = SpringFactory
						.getBean(ItomComUDynRankService.class);
				Calendar instance = Calendar.getInstance();
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String hitDate = dateFormat.format(instance.getTime());

				pool = (JedisSentinelPool) SpringFactory
						.getBean("jedisSentinelPool");
				jedis = (ShardedJedis) pool.getResource();

				// 获取定时任务时间段内需要更改的人员
				Set<String> smembers = jedis
						.smembers(CommonRannkPrefix.itomUseUserPrefix);
				if (smembers.size() == 0) {
					return;
				}
				logger.info("==========同步常用业务定时任务begin");
				logger.info("==========本次同步用户数: " + smembers.size());
				// 删除本次获取到的需要更新的人员id Set
				jedis.del(CommonRannkPrefix.itomUseUserPrefix);
				HashMap<String, String> param = new HashMap<String, String>();
				for (String userId : smembers) {
					// 循环排名类型
					for (Entry<String, String> rankMap : CommonRannkPrefix.rankMap
							.entrySet()) {
						// 获取用户名下有变动的模板分值
						Set<String> equRange = jedis.zrange(rankMap.getValue()
								+ ":" + CommonRannkPrefix.delaySuffix + ":"
								+ userId, 0, -1);

						jedis.del(rankMap.getValue() + ":"
								+ CommonRannkPrefix.delaySuffix + ":" + userId);
						// 循环模板
						for (String templateId : equRange) {
							Double zscore = jedis.zscore(rankMap.getValue()
									+ ":" + userId, templateId);
							param.put("templateId", templateId);
							param.put("userId", userId);
							param.put("hitDate", hitDate);
							String id = itomComUDynRankService
									.getIsHasItomComUDynRank(param);
							if (StringUtils.isNotEmpty(id)) {// 累加分数
								param.put("hits", zscore.intValue() + "");
								ItomCommonUpdateUtil.instance.update(param);
								updateCount++;
							} else {// 不存在则新增分数
								HashMap<String, Object> insertParam = new HashMap<String, Object>();
								insertParam.put("templateId", templateId);
								insertParam.put("userId", userId);
								insertParam.put("hitDate", instance.getTime());
								insertParam.put("zscore", zscore.intValue());
								ItomCommonUpdateUtil.instance
										.insert(insertParam);
								insertCount++;
							}
						}
					}
				}

				logger.info("==========同步常用业务定时任务更新排名数据:" + updateCount
						+ " 条, 新增排名数据:" + insertCount + "条");
				logger.info("==========同步常用业务定时任务正常结束");
				logger.info("==========同步常用业务定时任务用时: "
						+ ((System.currentTimeMillis() - start) / 1000.0) + " seconds");
			} catch (Exception e) {
				logger.info("==========同步常用业务定时任务异常结束");
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
//				PlatformConstant.OpType optype,
//				PlatformConstant.OpResult opresult, LogBase logBase
				if (pool != null) {
					pool.returnResource(jedis);
				}
			}
		}
	};
}

写入oracle时,采用线程池,避免上述执行耗时不放行:

@Component
public class ItomCommonUpdateUtil {
	ThreadPoolExecutor threadPool;
	public static volatile ItomCommonUpdateUtil instance;
	Logger logger = LoggerFactory.getLogger(ItomCommonUpdateUtil.class);

	private ItomCommonUpdateUtil() {
		this.threadPool = new ThreadPoolExecutor(Runtime.getRuntime()
				.availableProcessors() * 2, 10, 3L, TimeUnit.MINUTES,
				new ArrayBlockingQueue<Runnable>(10000), new ThreadFactory() {
					public Thread newThread(Runnable r) {
						Thread t = new Thread(r);
						t.setDaemon(true);
						// t.setPriority(1);
						return t;
					}
				}, new DiscardOldestPolicy());
		instance = this;
	}

	// private static class ItomCommonUpdateUtilInstance {
	// private static final ItomCommonUpdateUtil INSTANCE = new
	// ItomCommonUpdateUtil();
	// }
	//
	// public static ItomCommonUpdateUtil getInstance() {
	// return ItomCommonUpdateUtilInstance.INSTANCE;
	// }

	public void insert(HashMap<String, Object> insertParam) {
		this.threadPool.execute(new InsertTask(insertParam));
	}

	public void update(HashMap<String, String> param) {
		this.threadPool.execute(new UpdateTask(param));
	}

	class UpdateTask implements Runnable {
		HashMap<String, String> param;
		ItomComUDynRankService itomComUDynRankService = SpringFactory
				.getBean(ItomComUDynRankService.class);

		public UpdateTask(HashMap<String, String> param) {
			this.param = param;
		}

		public void run() {
			if (param != null) {
				itomComUDynRankService.updateItomComUDynRankByCache(param);
			}
		}

	}

	class InsertTask implements Runnable {
		ItomComUDynRankDTO dto;
		Logger logger = LoggerFactory.getLogger(InsertTask.class);
		SysUserAPI sysUserAPI = SpringFactory.getBean(SysUserAPImpl.class);
		BaseCacheManager baseCacheManager = SpringFactory
				.getBean(BaseCacheManager.class);

		public InsertTask(HashMap<String, Object> insertParam) {

			ItomComUDynRankDTO dto = new ItomComUDynRankDTO();
			dto.setUserId(insertParam.get("userId").toString());
			dto.setUserName(sysUserAPI.getSysUserNameById(insertParam.get(
					"userId").toString()));

			dto.setTemplateId(insertParam.get("templateId").toString());

			ItomComUDynTemplateDTO itomComUDynTemplateDTO = (ItomComUDynTemplateDTO) baseCacheManager
					.getObjectFromCache(
							CommonRannkPrefix.itomUseTemplatePrefix,
							insertParam.get("templateId").toString(),
							ItomComUDynTemplateDTO.class);

			dto.setTemplateName(itomComUDynTemplateDTO.getTemplateName());
			dto.setHitDate((Date) insertParam.get("hitDate"));
			dto.setHits((int) insertParam.get("zscore"));
			this.dto = dto;
		}

		public void run() {
			ItomComUDynRankService itomComUDynRankService = SpringFactory
					.getBean(ItomComUDynRankService.class);
			if (dto != null) {
				try {
					itomComUDynRankService.insertItomComUDynRankNoLog(dto);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
}

最终页面显示时调用的代码:

@RequestMapping(value = "ItomComUResourceShowInfo")
	public ModelAndView toItomComUResourceShow(HttpServletRequest request,
			HttpServletResponse reponse) {
		ModelAndView mav = new ModelAndView();
		String userId = SessionHelper.getLoginSysUserId(request);
		SysLookupAPI sysLookupAPI = SpringFactory.getBean(SysLookupAPI.class);
		String num = sysLookupAPI.getNameByLooupTypeCodeAndLooupCodeByAppId(
				"ITOM_COMMON_D_SHOW_NUM", "NUM",
				SessionHelper.getApplicationId());
		int showNum = 0;
		if (StringUtils.isNotEmpty(num)) {
			showNum = Integer.parseInt(num);
			if (showNum > 0) {
				showNum = showNum - 1;// redis num默认从0开始
			}
		}

		ArrayList<ItomComUDynTemplateDTO> equList = new ArrayList<ItomComUDynTemplateDTO>();
		ArrayList<ItomComUDynTemplateDTO> billList = new ArrayList<ItomComUDynTemplateDTO>();

		JedisSentinelPool pool = null;
		ShardedJedis jedis = null;

		pool = (JedisSentinelPool) SpringFactory.getBean("jedisSentinelPool");
		jedis = (ShardedJedis) pool.getResource();
		Set<String> rankEquSet = null;
		Set<String> rankBillSet = null;
		try {
			rankEquSet = jedis.zrevrange(CommonRannkPrefix.itomUseRankEquPrefix
					+ ":" + userId, 0, showNum);
			rankBillSet = jedis.zrevrange(
					CommonRannkPrefix.itomUseRankBillPrefix + ":" + userId, 0,
					showNum);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (pool != null) {
				pool.returnResource(jedis);
			}
		}
		Iterator<String> equIter = rankEquSet.iterator();
		equList = new ArrayList<ItomComUDynTemplateDTO>();
		while (equIter.hasNext()) {
			String templateId = equIter.next();
			ItomComUDynTemplateDTO cacheDto = (ItomComUDynTemplateDTO) baseCacheManager
					.getObjectFromCache(
							CommonRannkPrefix.itomUseTemplatePrefix,
							templateId, ItomComUDynTemplateDTO.class);
			if (cacheDto != null) {
				equList.add(cacheDto);
			}
		}

		Iterator<String> billIter = rankBillSet.iterator();
		billList = new ArrayList<ItomComUDynTemplateDTO>();
		while (billIter.hasNext()) {
			String templateId = billIter.next();
			ItomComUDynTemplateDTO cacheDto = (ItomComUDynTemplateDTO) baseCacheManager
					.getObjectFromCache(
							CommonRannkPrefix.itomUseTemplatePrefix,
							templateId, ItomComUDynTemplateDTO.class);
			if (cacheDto != null) {
				billList.add(cacheDto);
			}
		}
		mav.addObject("equList", equList);
		mav.addObject("billList", billList);
		mav.setViewName("avicit/itom/itomcomonbussiness/itomcomuresource/ItomComUResourceShowManager");
		request.setAttribute(
				"url",
				"platform/itom/itomcomonbussiness/itomcomuresource/ItomComUResourceController/operation/");
		return mav;
	}

前端展示页面html:

<body>
	  <div class="enter">
	  	 <!--  <div class="theme">常用入口</div> -->
	  	  <div class="main">
	  	  	   <ul>
	  	  	   	   <li>
	  	  	   	   	   <h3 class="main-title">流程</h3>
	  	  	   	   	   <div class="main-list">
	  	  	   	   	   		<c:forEach items="${billList}" var="item">
								 <a onclick="openInChart(this);" href="javascript:void(0);" src="${item.templateSrc}"><i></i><span>${item.templateName }</span></a>
							</c:forEach>
	  	  	   	   	   </div>
	  	  	   	   </li>
	  	  	   	     <li>
	  	  	   	   	   <h3 class="main-title">台账</h3>
	  	  	   	   	   <div class="main-list">
	  	  	   	   	   		<c:forEach items="${equList}" var="item">
								   <a onclick="openInChart(this);" href="javascript:void(0);" src="${item.templateSrc}"><i></i><span>${item.templateName }</span></a>
							</c:forEach>
	  	  	   	   	   </div>
	  	  	   	   </li>
	  	  	   </ul>
	  	  </div>
	  </div>
</body>

项目启动时从oracle读取数据到redis:采用pipeline设置每5000条进行一次批量提交,避免超时

@Component
public class ItomComUDynInit {
	private static final Logger logger = LoggerFactory
			.getLogger(ItomComUDynInit.class);
	@Autowired
	private ItomComUDynTemplateDao itomComUDynTemplateDao;

	@Autowired
	private ItomComUDynRankService itomComUDynRankService;

	@Autowired
	BaseCacheManager baseCacheManager;

	@SuppressWarnings("unused")
	@PostConstruct
	private void init() throws Exception {
		logger.info("开始初始化常用业务缓存");
		JedisSentinelPool pool = null;
		ShardedJedis jedis = null;
		pool = (JedisSentinelPool) SpringFactory.getBean("jedisSentinelPool");
		jedis = (ShardedJedis) pool.getResource();

		try {
			// 1.由于ShardedJedis 无法调用模糊查询功能,所以需要遍历每个分片后,依次进行模糊删除
			long start1 = System.currentTimeMillis();
			Collection<Jedis> allShards = jedis.getAllShards();
			Iterator<Jedis> allShardsIter = allShards.iterator();
			while (allShardsIter.hasNext()) {
				Jedis nextJedis;
				Pipeline nextPipeline;
				nextJedis = allShardsIter.next();
				nextPipeline = nextJedis.pipelined();
				try {
					Set<String> comUseKeySet = nextJedis.keys("ITOM_COM_USE_*");
					Iterator<String> comUseKeyIter = comUseKeySet.iterator();

					while (comUseKeyIter.hasNext()) {
						String comUseKey = comUseKeyIter.next();
						nextPipeline.del(comUseKey);
					}
					nextPipeline.sync();
				} catch (Exception e) {
					// TODO: handle exception
					nextPipeline.syncAndReturnAll();
				} finally {
					nextJedis.close();
				}
			}
			logger.info("删除常用业务缓存用时: "
					+ ((System.currentTimeMillis() - start1) / 1000.0)
					+ " seconds");
			// jedis.getShard(“ somekey”)。keys(“ some pattern”)
			// pipelined.del(CommonRannkPrefix.itomUseRankEquPrefix+"*");//删除设备排行
			// pipelined.del(CommonRannkPrefix.itomUseRankBillPrefix+"*");//删除流程排行
			// pipelined.del(CommonRannkPrefix.itomUseTemplatePrefix);//删除模板
			// pipelined.del(CommonRannkPrefix.itomUseCodeToTemplatePrefix);//删除codeTo模板

			// 2.初始化模板缓存
			long start2 = System.currentTimeMillis();
			ShardedJedisPipeline pipelineTemplate = jedis.pipelined();
			ItomComUDynTemplateDTO searchTemDto = new ItomComUDynTemplateDTO();
			List<ItomComUDynTemplateDTO> searchItomComUDynTemplate = itomComUDynTemplateDao
					.searchItomComUDynTemplate(searchTemDto);
			for (ItomComUDynTemplateDTO temDto : searchItomComUDynTemplate) {
				// // 构建 模板id > 模板明细
				baseCacheManager.insertCache(temDto);
				// 模板Code > 模板Id
				pipelineTemplate.hset(CommonRannkPrefix.itomUseCodeToTemplatePrefix,
						temDto.getTemplateCode(), temDto.getId());
			}
			pipelineTemplate.sync();
			logger.info("初始化常用业务模板缓存用时: "
					+ ((System.currentTimeMillis() - start2) / 1000.0)
					+ " seconds");

			// 3.初始化人员模块排名缓存
			long start3 = System.currentTimeMillis();
			HashMap<String, String> param = new HashMap<String, String>();
			List<HashMap<String, String>> ranList = itomComUDynRankService
					.getItomComDynRankByCache(param);
			
			int pageSize = 5000;//批量提交切分
			List<List<HashMap<String, String>>> listArray = new ArrayList<List<HashMap<String, String>>>();
			for (int i = 0; i < ranList.size(); i+=pageSize) {
				int toIndex = i + pageSize>ranList.size()?ranList.size():i+pageSize;
				listArray.add(ranList.subList(i, toIndex));
			}
			
			ShardedJedisPipeline pipelineRank;
			for (List<HashMap<String, String>> pipelLinelist : listArray) {
				pipelineRank = jedis.pipelined();
				for (int i = 0; i < pipelLinelist.size(); i++) {
					Object hits = pipelLinelist.get(i).get("HITS");
					pipelineRank.zadd(
							CommonRannkPrefix.rankMap.get(pipelLinelist
									.get(i).get("TEMPLATE_TYPE"))
									+ ":"
									+ pipelLinelist.get(i).get(
											"USER_ID"), Double.parseDouble(hits
									.toString()), pipelLinelist.get(i)
									.get("TEMPLATE_ID"));
				}
				pipelineRank.sync();
			}
			logger.info("初始化常用业务人员排名缓存用时: "
					+ ((System.currentTimeMillis() - start3) / 1000.0)
					+ " seconds");
			
		} catch (Exception e) {
			logger.info("初始化常用业务缓存异常");
			// TODO: handle exception
			throw e;
		} finally {
			logger.info("结束初始化常用业务缓存");
			if (pool != null) {
				pool.returnResource(jedis);
			}
		}

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值