/** * 缓存list * * @param k key * @param v value * @param time time * @return boolean */ @Override public boolean cacheList(String k, List<String> v, long time) { String key = KEY_PREFIX_LIST + k; try { ListOperations<String, String> listOps = redisTemplate.opsForList(); listOps.rightPushAll(key, v); if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Throwable t) { LOGGER.error("缓存[" + key + "]失败, value[" + v + "]", t); } return false; }
@Override public void pushList(final Position position, final String key, final String... values) { final ListOperations<String, String> operation = redisTemplate.opsForList(); if (position == Position.LEFT) { operation.leftPushAll(getKey(key), values); } else { operation.rightPushAll(getKey(key), values); } }
/** * list缓存 * * @param k key * @param v value * @param time time * @return boolean */ @Override public boolean cacheList(String k, String v, long time) { String key = KEY_PREFIX_LIST + k; try { ListOperations<String, String> listOps = redisTemplate.opsForList(); listOps.rightPush(key, v); if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Throwable t) { LOGGER.error("缓存[" + key + "]失败, value[" + v + "]", t); } return false; }
/** * 获取list缓存 * * @param k key * @param start start * @param end end * @return list */ @Override public List<String> getList(String k, long start, long end) { try { ListOperations<String, String> listOps = redisTemplate.opsForList(); return listOps.range(KEY_PREFIX_LIST + k, start, end); } catch (Throwable t) { LOGGER.error("获取list缓存失败key[" + KEY_PREFIX_LIST + k + ", Codeor[" + t + "]"); } return null; }
/** * 获取总条数, 可用于分页 * * @param k key * @return long */ @Override public long getListSize(String k) { try { ListOperations<String, String> listOps = redisTemplate.opsForList(); return listOps.size(KEY_PREFIX_LIST + k); } catch (Throwable t) { LOGGER.error("获取list长度失败key[" + KEY_PREFIX_LIST + k + "], Codeor[" + t + "]"); } return 0; }
/** * 获取总条数, 可用于分页 * * @param listOps listOps * @param k k * @return long */ @Override public long getListSize(ListOperations<String, String> listOps, String k) { try { return listOps.size(KEY_PREFIX_LIST + k); } catch (Throwable t) { LOGGER.error("获取list长度失败key[" + KEY_PREFIX_LIST + k + "], Codeor[" + t + "]"); } return 0; }
/** * 移除list缓存 * * @param k k * @return boolean */ @Override public boolean removeOneOfList(String k) { String key = KEY_PREFIX_LIST + k; try { ListOperations<String, String> listOps = redisTemplate.opsForList(); listOps.rightPop(key); return true; } catch (Throwable t) { LOGGER.error("移除list缓存失败key[" + KEY_PREFIX_LIST + k + ", Codeor[" + t + "]"); } return false; }
@Override public ListOperations<K, V> opsForList() { try { return redisTemplate.opsForList(); } catch (Exception ex) { throw new RedisBaoException(ex); } }
@Override public String popList(final String key) { final ListOperations<String, String> operation = redisTemplate.opsForList(); final String value = operation.leftPop(getKey(key)); return value; }
@Override public List<String> rangeList(final String key) { final ListOperations<String, String> operation = redisTemplate.opsForList(); final List<String> value = operation.range(getKey(key), 0, operation.size(getKey(key))); return value; }
@RequestMapping(value = "/api/v1/alerts",method = RequestMethod.POST) @ResponseBody public Echo getAlerts(@RequestBody ArrayList<Alert> alerts) { logger.info(alerts.size() + " alert(s) is coming···"); // 该接口每接受一次客户端请求,api.v1.alerts.invoked.count 就增加 1 counterService.increment("api.v1.alerts.invoked.count"); ListOperations<String,String> listOps = stringRedisTemplate.opsForList(); ValueOperations<String, String> valueOps = stringRedisTemplate.opsForValue(); // the key of list in Redis String cached_alerts = "cached_alerts"; String queued_metrics = "queued_metrics"; for(int i = 0; i < alerts.size(); i++) { Map<String,String> labels = alerts.get(i).getLabels(); wwwAlert = populate(WWWAlert.class, labels); String metric = wwwAlert.getLocateInfo(); /* * Socket连接断开,此时AlertHadnler接收到的原生告警信息需要缓存到Redis队列中, * 待Socket连接建立后,再由Client从Redis队列中取出数据一一发送。 */ if(null == client.getSocket()) { logger.warn("socket connection is unnormal, cache alerts to Redis"); listOps.leftPush(cached_alerts, wwwAlert.toString()); /* * Socket连接保持中,此时AlertHandler需要将封装后的告警信息添加到缓存和队列中。 * Redis缓存利用String数据结构,key=指标名,value=封装后的告警信息; * Redis队列利用List数据结构,这也是Redis常见的应用场景之一; */ }else { if("".equals(valueOps.get(metric)) || null == valueOps.get(metric)) { logger.info("socket connection is normal,but there is no metric in Redis"); wwwAlertService.insert(wwwAlert); // 持久化到mysql数据库中 valueOps.set(metric,wwwAlert.toString()); listOps.leftPush(queued_metrics, metric); }else { logger.info("socket connection is normal,and the metric has been cached in Redis"); wwwAlertService.update(wwwAlert); // 更新mysql数据库中数据 valueOps.set(metric,wwwAlert.toString()); listOps.leftPush(queued_metrics, metric); } } } logger.info("one request has been handled completely"); return new Echo("succes",LocalDateTime.now().toString()); }
@Override public ListOperations<K, V> opsForList() { int dbIndex = RedisApplication.redisConnectionDbIndex.get(); return new DefaultListOperations<K, V>(this, dbIndex); }
public ListOperations<String, String> listOps() { return redisTemplate.opsForList(); }
@Before public void setUp() throws Exception { redisTemplate = mock(RedisTemplate.class); listOperations = mock(ListOperations.class); super.setUp(); }
/** * 列表添加 * @param k * @param v */ public void lPush(String k, String v) { ListOperations<String, String> list = stringRedisTemplate.opsForList(); list.rightPush(k, v); }
/** * 列表获取 * @param k * @param l * @param l1 * @return */ public List<String> lRange(String k, long l, long l1) { ListOperations<String, String> list = stringRedisTemplate.opsForList(); return list.range(k, l, l1); }
/** * redis List 引擎 * * @return the list operations */ public ListOperations<String, String> opsForList() { return redisTemplate.opsForList(); }
/** * 获取页码 * * @param listOps * @param k * @return */ long getListSize(ListOperations<String, String> listOps, String k);
public ListOperations<String, Object> opsForList(){ return redisTemplate.opsForList(); }
ListOperations<K, V> opsForList();