@Override protected void doHealthCheck(Health.Builder builder) throws Exception { RedisConnection connection = RedisConnectionUtils .getConnection(this.redisConnectionFactory); try { if (connection instanceof RedisClusterConnection) { ClusterInfo clusterInfo = ((RedisClusterConnection) connection) .clusterGetClusterInfo(); builder.up().withDetail("cluster_size", clusterInfo.getClusterSize()) .withDetail("slots_up", clusterInfo.getSlotsOk()) .withDetail("slots_fail", clusterInfo.getSlotsFail()); } else { Properties info = connection.info(); builder.up().withDetail(VERSION, info.getProperty(REDIS_VERSION)); } } finally { RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory); } }
@Scheduled(fixedDelay = 5000) public void monitor() { DefaultCacheStatistics statistics = new DefaultCacheStatistics(); // @see http://redis.io/commands/INFO RedisConnection connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory); try { Properties props = connection.info(); Long hitCount = Long.parseLong(props.getProperty("keyspace_hits")); Long missCount = Long.parseLong(props.getProperty("keyspace_misses")); statistics.setGetCacheCounts(hitCount, missCount); // we do not currently have a way of calculating the cache size, so we have to filter List<Metric<?>> metrics = statistics .toMetrics("cache.") .stream() .filter(f -> !f.getName().contains(".size")) .collect(Collectors.toList()); metrics.forEach(m -> metricServices.submit(m.getName(), (Double) m.getValue())); } finally { RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory); } }
@Override public boolean setnx(String key, Serializable value) { RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection redisConnection = null; try { redisConnection = RedisConnectionUtils.getConnection(factory); if (redisConnection == null) { return redisTemplate.boundValueOps(key).setIfAbsent(value); } return redisConnection.setNX(keySerializer.serialize(key), valueSerializer.serialize(value)); } finally { RedisConnectionUtils.releaseConnection(redisConnection, factory); } }
@Override public boolean lock(String key) { RedisConnectionFactory factory = redisTemplate.getConnectionFactory(); RedisConnection redisConnection = null; try { redisConnection = RedisConnectionUtils.getConnection(factory); if (redisConnection == null) { return redisTemplate.boundValueOps(key).setIfAbsent("0"); } return redisConnection.setNX(keySerializer.serialize(key), valueSerializer.serialize("0")); } finally { RedisConnectionUtils.releaseConnection(redisConnection, factory); } }
private String getDataType(String serverName, int dbIndex, String key) { RedisTemplate redisTemplate = RedisApplication.redisTemplatesMap.get(serverName); RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory()); connection.select(dbIndex); DataType dataType = connection.type(key.getBytes()); connection.close(); return dataType.name().toUpperCase(); }
private Object getKV(String serverName, int dbIndex, String key) { RedisTemplate redisTemplate = RedisApplication.redisTemplatesMap.get(serverName); RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory()); connection.select(dbIndex); DataType dataType = connection.type(key.getBytes()); connection.close(); Object values = null; switch(dataType) { case STRING: values = redisDao.getSTRING(serverName, dbIndex, key); break; case LIST: values = redisDao.getLIST(serverName, dbIndex, key); break; case SET: values = redisDao.getSET(serverName, dbIndex, key); break; case ZSET: values = redisDao.getZSET(serverName, dbIndex, key); break; case HASH: values = redisDao.getHASH(serverName, dbIndex, key); break; case NONE: //never be here values = null; } return values; }
protected void initRedisKeysCache(RedisTemplate redisTemplate, String serverName , int dbIndex) { RedisConnection connection = RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory()); connection.select(dbIndex); Set<byte[]> keysSet = connection.keys("*".getBytes()); connection.close(); List<RKey> tempList = new ArrayList<RKey>(); ConvertUtil.convertByteToString(connection, keysSet, tempList); Collections.sort(tempList); CopyOnWriteArrayList<RKey> redisKeysList = new CopyOnWriteArrayList<RKey>(tempList); if(redisKeysList.size()>0) { redisKeysListMap.put(serverName+DEFAULT_SEPARATOR+dbIndex, redisKeysList); } }
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { RedisConnection connection = RedisConnectionUtils .getConnection(this.redisConnectionFactory); try { Properties info = connection.info(); builder.up().withDetail("version", info.getProperty("redis_version")); } finally { RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory); } }
@Override public CacheStatistics getCacheStatistics(CacheManager cacheManager, RedisCache cache) { DefaultCacheStatistics statistics = new DefaultCacheStatistics(); // @see http://redis.io/commands/INFO RedisConnection connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory); try { Properties props = connection.info(); Long hitCount = Long.parseLong(props.getProperty("keyspace_hits")); Long missCount = Long.parseLong(props.getProperty("keyspace_misses")); statistics.setGetCacheCounts(hitCount, missCount); } finally { RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory); } return statistics; }
@Override public RedisConnection getRedisConnection() { return RedisConnectionUtils.getConnection(this.redisConnectionFactory); }