com.github.fartherp.framework.cache.redis.RedisCacheManager Maven / Gradle / Ivy
/*
* Copyright (c) 2017. CK. All rights reserved.
*/
package com.github.fartherp.framework.cache.redis;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* a implement of CacheManager in Redis
*
* a example:
*
*
* {@code
*
*
*
*
*
*
*
*
* 127.0.0.1
*
*
* 6379
*
*
* 123456
*
*
*
*
* }
*
* Author: CK
* Date: 2015/12/1
*/
public class RedisCacheManager extends AbstractRedisCacheManager {
/**
* 日志类
*/
private final Log LOGGER = LogFactory.getLog(getClass());
public String toString() {
int size = 0;
if (CollectionUtils.isNotEmpty(getClientList())) {
size = getClientList().size();
}
return "RedisCacheManager clientSize[" + size + "]";
}
protected List getClients(Object key) {
RedisClient client = getClient(key);
if (client == null) {
LOGGER.error("Redis Cache Manager get redis client is null.");
return Collections.emptyList();
}
return Arrays.asList(client);
}
/**
* get cache client. use hash code to select client
*
* @param key
* cache key
* @return {@link RedisClient} client
*/
private RedisClient getClient(Object key) {
if (CollectionUtils.isEmpty(getClientList())) {
return null;
}
List copied = new ArrayList(getClientList());
int index = Math.abs(key.toString().hashCode() % copied.size());
return copied.get(index);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy