二.redis的配置
目录结构推荐
image在conf.properties里配置redis信息
imageredis-server=127.0.0.1
redis-port=根据自己配置
redis-passwd=gg思密达
三.redis的工具类RedisClient.java(自己建包)
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class RedisClient {
private static Log log=LogFactory.getLog(RedisClient.class);
private Jedis jedis;//非切片额客户端连接
private JedisPool jedisPool;//非切片连接池
private ShardedJedis shardedJedis;//切片额客户端连接
private ShardedJedisPool shardedJedisPool;//切片连接池
private String redisServer = getConfigAttr("conf.properties","redis-server");
private String redisPort = getConfigAttr("conf.properties","redis-port");
private String passwd = getConfigAttr("conf.properties","redis-passwd");
private static final String defaultServerIp = "127.0.0.1";//如果没有配置地址,默认为本机地址
public RedisClient()
{
initServerPort();
initialPool();
initialShardedPool();
shardedJedis = shardedJedisPool.getResource();
jedis = jedisPool.getResource();
jedis.auth(passwd);
}
private void initServerPort(){
if("".equals(redisServer) || redisServer==null){
redisServer = defaultServerIp;
}
if("".equals(redisPort) || redisPort==null){
redisPort = "6379";
}
}
/**
* 初始化非切片池
*/
private void initialPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(1000l);
config.setTestOnBorrow(false);
jedisPool = new JedisPool(config,redisServer,Integer.parseInt(redisPort),100000);
}
/**
* 初始化切片池
*/
private void initialShardedPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(1000l);
config.setTestOnBorrow(false);
// slave链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo jedisShardInfo= new JedisShardInfo(redisServer,Integer.parseInt(redisPort),100000,"master");
jedisShardInfo.setPassword(passwd);
shards.add(jedisShardInfo);
// 构造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}
public void cacheRecord(Map<String, String> map){
Set<Map.Entry<String, String>> entrySet = map.entrySet();
//将关系集合entrySet进行迭代,存放到迭代器中
Iterator<Map.Entry<String, String>> it2 = entrySet.iterator();
while(it2.hasNext()){
Map.Entry<String, String> me = it2.next();//获取Map.Entry关系对象me
String key = me.getKey();//通过关系对象获取key
String value = me.getValue();//通过关系对象获取value
shardedJedis.set(key, value);
}
}
public void delCacheList(List<String> list){
for(String s:list){
jedis.del(s);
}
}
public boolean isExist(String s){
return jedis.exists(s);
}
public String getValue(String s){
return jedis.get(s);
}
public void destroy(){
jedisPool.returnResource(jedis);
shardedJedisPool.returnResource(shardedJedis);
}
private String getConfigAttr(String propfile,String attr){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propfile);
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e) {
if(log.isErrorEnabled()){
log.error(e);
}
} catch (Exception e) {
if(log.isErrorEnabled()){
log.error(e);
}
}finally{
if(p.isEmpty()){
try {
inputStream.close();
} catch (IOException e) {
if(log.isErrorEnabled()){
log.error(e);
}
} catch (Exception e) {
if(log.isErrorEnabled()){
log.error(e);
}
}
}
}
String attrValue = p.getProperty(attr);
return attrValue;
}
/**
* 获取jedis
* @return the jedis
*/
public Jedis getJedis() {
return jedis;
}
/**
* 设置jedis
* @param jedis the jedis to set
*/
public void setJedis(Jedis jedis) {
this.jedis = jedis;
}
}
四.取用redis的参数
RedisClient redisClientPc = new RedisClient();String aaa = redisClientPc.getValue("aaa") == null ? "" : redisClientPc.getValue("aaa");
网友评论