package com.commons.redis.cache;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class RedisManager {
/** 切片链接池 */
private static ShardedJedisPool shardedJedisPool;
private ShardedJedis shardedJedis;
// 设置与缓存服务器的连接池
static {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.parseInt(Config
.getProperty("redis.pool.maxActive")));
config.setMaxIdle(Integer.parseInt(Config
.getProperty("redis.pool.maxIdle")));
config.setMaxWait(Integer.parseInt(Config
.getProperty("redis.pool.maxWait")));
config.setTestOnBorrow(true);
List
// 服务器列表
String[] addressArr = (Config.getProperty("redis.address")).split(",");
for (String str : addressArr) {
String []addressConfig = str.split(":");
shards.add(new JedisShardInfo(addressConfig[0], Integer
.parseInt(addressConfig[1]), addressConfig[2]));
}
// 构造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}
/**
* redis的List集合 ,向key这个list添加元素
*
* @param key
* List别名
* @param string
* 元素
* @return
*/
public long rpush(String key, String string) {
try {
shardedJedis = shardedJedisPool.getResource();
long ret = shardedJedis.rpush(key, string);
shardedJedisPool.returnResource(shardedJedis);
return ret;
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
return 0L;
}
}
/**
* 获取key这个List,从第几个元素到第几个元素 LRANGE key start
* stop返回列表key中指定区间内的元素,区间以偏移量start和stop指定。
* 下标(index)参数start和stop都以0为底,也就是说,以0表示列表的第一个元素,以1表示列表的第二个元素,以此类推。
* 也可以使用负数下标,以-1表示列表的最后一个元素,-2表示列表的倒数第二个元素,以此类推。
*
* @param key
* List别名
* @param start
* 开始下标
* @param end
* 结束下标
* @return
*/
public List
try {
shardedJedis = shardedJedisPool.getResource();
List
shardedJedisPool.returnResource(shardedJedis);
return ret;
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
return null;
}
}
/**
* 将哈希表key中的域field的值设为value。
*
* @param key
* 哈希表别名
* @param field键
* @param value
* 值
*/
public void hset(String key, String field, String value) {
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.hset(key, field, value);
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
}
}
/**
* 向key赋值
*
* @param key
* @param value
*/
public void set(String key, String value) {
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.set(key, value);
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
}
}
/**
* 获取key的值
*
* @param key
* @return
*/
public String get(String key) {
try {
shardedJedis = shardedJedisPool.getResource();
String value = shardedJedis.get(key);
shardedJedisPool.returnResource(shardedJedis);
return value;
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
return null;
}
}
/**
* 将多个field - value(域-值)对设置到哈希表key中。
*
* @param key
* @param map
*/
public void hmset(String key, Map
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.hmset(key, map);
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
}
}
/**
* 给key赋值,并生命周期设置为seconds
*
* @param key
* @param seconds
* 生命周期 秒为单位
* @param value
*/
public void setex(String key, int seconds, String value) {
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.setex(key, seconds, value);
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
}
}
/**
* 为给定key设置生命周期
*
* @param key
* @param seconds
* 生命周期 秒为单位
*/
public void expire(String key, int seconds) {
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.expire(key, seconds);
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
if (shardedJedis != null)
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
}
}
/**
* 检查key是否存在
*
* @param key
* @return
*/
public boolean exists(String key) {
try {
shardedJedis = shardedJedisPool.getResource();
boolean bool = shardedJedis.exists(key);
shardedJedisPool.returnResource(shardedJedis);
return bool;
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
}
e.printStackTrace();
return false;
}
}
/**
* 返回key值的类型 none(key不存在),string(字符串),list(列表),set(集合),zset(有序集),hash(哈希表)
*
* @param key
* @return
*/
public String type(String key) {
try {
shardedJedis = shardedJedisPool.getResource();
String type = shardedJedis.type(key);
shardedJedisPool.returnResource(shardedJedis);
return type;
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
}
e.printStackTrace();
return null;
}
}
/**
* 从哈希表key中获取field的value
*
* @param key
* @param field
*/
public String hget(String key, String field) {
try {
shardedJedis = shardedJedisPool.getResource();
String value = shardedJedis.hget(key, field);
shardedJedisPool.returnResource(shardedJedis);
return value;
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
}
e.printStackTrace();
return null;
}
}
/**
* 返回哈希表key中,所有的域和值
*
* @param key
* @return
*/
public Map
try {
shardedJedis = shardedJedisPool.getResource();
Map
shardedJedisPool.returnResource(shardedJedis);
return map;
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
}
e.printStackTrace();
return null;
}
}
/**
* 返回哈希表key中,所有的域和值
*
* @param key
* @return
*/
public Set> smembers(String key) {
try {
shardedJedis = shardedJedisPool.getResource();
Set> set = shardedJedis.smembers(key);
shardedJedisPool.returnResource(shardedJedis);
return set;
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
}
e.printStackTrace();
return null;
}
}
/**
* 移除集合中的member元素
*
* @param key
* List别名
* @param field
* 键
*/
public void delSetObj(String key, String field) {
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.srem(key, field);
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
e.printStackTrace();
}
}
}
/**
* 判断member元素是否是集合key的成员。是(true),否则(false)
*
* @param key
* @param field
* @return
*/
public boolean isNotField(String key, String field) {
try {
shardedJedis = shardedJedisPool.getResource();
boolean bool = shardedJedis.sismember(key, field);
shardedJedisPool.returnResource(shardedJedis);
return bool;
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
}
e.printStackTrace();
return false;
}
}
/**
* 如果key已经存在并且是一个字符串,将value追加到key原来的值之后
*
* @param key
* @param value
*/
public void append(String key, String value) {
try {
shardedJedis = shardedJedisPool.getResource();
shardedJedis.append(key, value);
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
if (shardedJedis != null) {
shardedJedisPool.returnBrokenResource(shardedJedis);
}
e.printStackTrace();
}
}
/**
* 获取客户端连接
*
* @return
*/
public static ShardedJedis getShardedJedis() {
try {
return shardedJedisPool.getResource();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放连接
*
* @param jedis
*/
public static void returnResourceObject(ShardedJedisPool shardedJedisPool) {
try {
if (shardedJedisPool != null) {
shardedJedisPool.returnResourceObject(shardedJedisPool);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭 Redis
*/
public void destory() {
try {
shardedJedisPool.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.commons.redis.cache;
import java.io.File;
import java.io.InputStream;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Config {
private static Log log = LogFactory.getLog(Config.class);
private static Properties props = new Properties();
private static String contextAbsoultePath = "";
private static String contextPath = "";
private static ServletContext context;
private static InitialContext ic;
static {
InputStream in = Config.class.getResourceAsStream("/commons-pool.properties");
try {
if (in != null) {
try {
props.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getProperty(String key) {
return getProperty(key, "");
}
public static String getProperty(String key,String defaultValue) {
return props.getProperty(key, defaultValue);
}
public static String getPathProperty(String key) {
String s = props.getProperty(key, "");
s = s.replace('/', File.separator.charAt(0));
return s;
}
public static String getContextAbsoultePath() {
return contextAbsoultePath;
}
public static void setContextAbsoultePath(String contextAbsoultePath) {
Config.contextAbsoultePath = contextAbsoultePath;
}
public static String getContextPath() {
return contextPath;
}
public static void setContextPath(String contextPath) {
Config.contextPath = contextPath;
}
public static void setContext(ServletContext ctx) {
context = ctx;
}
public static ServletContext getContext() {
return context;
}
public static InitialContext getInitialContext() {
try {
if (ic == null) {
Properties p = new Properties();
p.put(
Context.INITIAL_CONTEXT_FACTORY,
getProperty("jndi.InitialContextFactory"));
p.put(Context.PROVIDER_URL, getProperty("jndi.ProviderURL"));
ic = new InitialContext(p);
}
} catch (Exception ne) {
log.fatal(ne);
}
return ic;
}
}
commons-pool.properties:
#最大分配的对象数
redis.pool.maxActive=5000
#最大能够保持idel状态的对象数
redis.pool.maxIdle=5000
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=10000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#address[ip地址:端口号:密码]多个以英文逗号分开
redis.address=127.0.0.1:6379:rayping88
package com.commons.redis.cache;
public class RedisTest {
public static void main(String[] args) {
RedisManager redisManager =new RedisManager();
redisManager.set("ray", "redis 测试");
System.out.println(redisManager.get("ray"));
}
}