import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import com.thinkive.base.config.Configuration;
public class RedisUtils {
private static ShardedJedisPool jedisPool = null;
private static Logger logger = Logger.getLogger(RedisUtils.class);
public static String saveCodeRedis(String codeKey, String codeValue)
throws Exception {
// 获取系统默认过期时间
int expiredTime = Configuration.getInt("token.expiredTime");
return saveCodeRedis(codeKey, codeValue, expiredTime);
}
public static String saveCodeRedis(String codeKey, String codeValue,
int expiredTime) throws Exception {
ShardedJedis jedis = null;
String msg = "";
try {
jedis = getPool().getResource();
msg = jedis.setex(codeKey, expiredTime, codeValue);
logger.info("调用RedisUtils保存成功" + codeValue);
} catch (Exception e) {
logger.error("调用RedisUtils保存失败" + e.getMessage(), e);
msg = "error";
} finally {
if (jedis != null) {
jedisPool.returnResourceObject(jedis);
}
}
return msg;
}
public static String getCodeRedis(String codeKey) throws Exception {
ShardedJedis jedis = null;
String code = "";
try {
jedis = getPool().getResource();
code = jedis.get(codeKey);
logger.info("调用RedisUtils获取成功" + code);
} catch (Exception e) {
logger.error("调用RedisUtils获取失败" + e.getMessage(), e);
throw new Exception(e);
} finally {
if (jedis != null) {
getPool().returnResourceObject(jedis);
}
}
return code;
}
public static String delCode(String codeKey) {
ShardedJedis jedis = null;
String msg = "";
try {
jedis = getPool().getResource();
jedis.del(codeKey);
logger.info("调用RedisUtils删除成功" + codeKey);
} catch (Exception e) {
logger.error("调用RedisUtils删除失败" + e.getMessage(), e);
msg = "error";
} finally {
if (jedis != null) {
getPool().returnResourceObject(jedis);
}
}
return msg;
}
public static ShardedJedisPool getPool() {
if (jedisPool == null) {
String host = Configuration.getString("redis.host");
int port = Configuration.getInt("redis.port");
// 暂不用密码
// String password = Configuration.getString("redis.password");
// int timeout = Configuration.getInt("redis.timeout");
int maxTotal = Configuration.getInt("jedisPool.maxTotal");
int maxIdle = Configuration.getInt("jedisPool.maxIdle");
int minIdle = Configuration.getInt("jedisPool.minIdle");
int maxWaitMillis = Configuration.getInt("jedisPool.maxWaitMillis");
boolean testOnBorrow = Configuration
.getBoolean("jedisPool.testOnBorrow");
boolean testOnReturn = Configuration
.getBoolean("jedisPool.testOnReturn");
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(maxTotal);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
config.setMaxWait(maxWaitMillis);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
// Jedis链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(host, port, "master"));
// 构造池
jedisPool = new ShardedJedisPool(config, shards);
}
return jedisPool;
}
public static void main(String[] args) {
try {
String str = RedisUtils.getCodeRedis("225381435196968712");
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.List;
import org.apache.log4j.Logger;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import com.thinkive.base.config.Configuration;
public class RedisUtils {
private static ShardedJedisPool jedisPool = null;
private static Logger logger = Logger.getLogger(RedisUtils.class);
public static String saveCodeRedis(String codeKey, String codeValue)
throws Exception {
// 获取系统默认过期时间
int expiredTime = Configuration.getInt("token.expiredTime");
return saveCodeRedis(codeKey, codeValue, expiredTime);
}
public static String saveCodeRedis(String codeKey, String codeValue,
int expiredTime) throws Exception {
ShardedJedis jedis = null;
String msg = "";
try {
jedis = getPool().getResource();
msg = jedis.setex(codeKey, expiredTime, codeValue);
logger.info("调用RedisUtils保存成功" + codeValue);
} catch (Exception e) {
logger.error("调用RedisUtils保存失败" + e.getMessage(), e);
msg = "error";
} finally {
if (jedis != null) {
jedisPool.returnResourceObject(jedis);
}
}
return msg;
}
public static String getCodeRedis(String codeKey) throws Exception {
ShardedJedis jedis = null;
String code = "";
try {
jedis = getPool().getResource();
code = jedis.get(codeKey);
logger.info("调用RedisUtils获取成功" + code);
} catch (Exception e) {
logger.error("调用RedisUtils获取失败" + e.getMessage(), e);
throw new Exception(e);
} finally {
if (jedis != null) {
getPool().returnResourceObject(jedis);
}
}
return code;
}
public static String delCode(String codeKey) {
ShardedJedis jedis = null;
String msg = "";
try {
jedis = getPool().getResource();
jedis.del(codeKey);
logger.info("调用RedisUtils删除成功" + codeKey);
} catch (Exception e) {
logger.error("调用RedisUtils删除失败" + e.getMessage(), e);
msg = "error";
} finally {
if (jedis != null) {
getPool().returnResourceObject(jedis);
}
}
return msg;
}
public static ShardedJedisPool getPool() {
if (jedisPool == null) {
String host = Configuration.getString("redis.host");
int port = Configuration.getInt("redis.port");
// 暂不用密码
// String password = Configuration.getString("redis.password");
// int timeout = Configuration.getInt("redis.timeout");
int maxTotal = Configuration.getInt("jedisPool.maxTotal");
int maxIdle = Configuration.getInt("jedisPool.maxIdle");
int minIdle = Configuration.getInt("jedisPool.minIdle");
int maxWaitMillis = Configuration.getInt("jedisPool.maxWaitMillis");
boolean testOnBorrow = Configuration
.getBoolean("jedisPool.testOnBorrow");
boolean testOnReturn = Configuration
.getBoolean("jedisPool.testOnReturn");
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(maxTotal);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
config.setMaxWait(maxWaitMillis);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
// Jedis链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(host, port, "master"));
// 构造池
jedisPool = new ShardedJedisPool(config, shards);
}
return jedisPool;
}
public static void main(String[] args) {
try {
String str = RedisUtils.getCodeRedis("225381435196968712");
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}