天天看點

封裝redis(jedis)

1、添加pom檔案

<!-- Jedis -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.2.1</version>

</dependency>

2、添加redis.properties檔案

book.redis.pool.maxWait=1000

book.redis.pool.maxActive=1024

book.redis.pool.maxIdle=200

book.redis.pool.ip=localhost

book.redis.pool.port=6379

3、添加RedisPool.java類

public class RedisPool<T> {

private String poolName;

private String ip;

private String pore;

private int maxWait;

private int maxActive;

private int maxIdle;

private static JedisPool jedisPool;

public RedisPool() {

}

public RedisPool(String poolName, String ip, String pore, int maxWait, int maxActive, int maxIdle) {

this.poolName = poolName;

this.ip = ip;

this.pore = pore;

this.maxWait = maxWait;

this.maxActive = maxActive;

this.maxIdle = maxIdle;

createPool();

}

//建立池

private void createPool(){

JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();

jedisPoolConfig.setMaxActive(maxActive);

jedisPoolConfig.setMaxIdle(maxIdle);

jedisPoolConfig.setMaxWait(maxWait);

jedisPoolConfig.setTestOnBorrow(false);

jedisPool =new JedisPool(jedisPoolConfig,ip, SsmUtil.convert(pore,6379),maxWait);

}

public String getPoolName() {

return poolName;

}

public void setPoolName(String poolName) {

this.poolName = poolName;

}

public String getIp() {

return ip;

}

public void setIp(String ip) {

this.ip = ip;

}

public String getPore() {

return pore;

}

public void setPore(String pore) {

this.pore = pore;

}

public int getMaxWait() {

return maxWait;

}

public void setMaxWait(int maxWait) {

this.maxWait = maxWait;

}

public int getMaxActive() {

return maxActive;

}

public void setMaxActive(int maxActive) {

this.maxActive = maxActive;

}

public int getMaxIdle() {

return maxIdle;

}

public void setMaxIdle(int maxIdle) {

this.maxIdle = maxIdle;

}

//普通類set方法

public void set(String key, String value) {

boolean flag = true;

Jedis jedis = jedisPool.getResource();

try{

if(jedis!=null){

jedis.set(key, value);

}

}catch(Exception e){

flag = false;

e.printStackTrace();

}finally{

if (flag){

jedisPool.returnResource(jedis);

}else{

jedisPool.returnBrokenResource(jedis);

}

}

}

//String 類get方法

public String get(String key) {

boolean flag = true;

Jedis jedis =jedisPool.getResource();

try{

if(jedis!=null){

return jedis.get(key);

}

}catch(Exception e){

flag = false;

e.printStackTrace();

}finally{

if (flag){

//把資料還回資料池

jedisPool.returnResource(jedis);

}else{

//銷毀資料

jedisPool.returnBrokenResource(jedis);

}

}

return null;

}

}

4、添加 RedisUtil類

public class RedisUtil {

private static HashMap<String, RedisPool> redisPoolMap = new HashMap<>();

private final static String REDIS_PROFILE = "properties/redis.properties";

public static synchronized RedisPool getRedisPool(String poolName) {

RedisPool rp = null;

if (redisPoolMap.containsKey(poolName)) {

rp = redisPoolMap.get(poolName);

} else {

//讀取poolname對應的配置檔案的屬性來建構poolName

try {

Properties pro = PropertiesUtil.getProperties(REDIS_PROFILE,"utf-8");

rp = createRedisPool(poolName, pro);

redisPoolMap.put(poolName, rp);

} catch (Exception e) {

e.printStackTrace();

}

}

return rp;

}

//建立redis資料源

private static RedisPool createRedisPool(String poolName, Properties pro) {

String ip = pro.getProperty(poolName + ".redis.pool.ip");

String port = pro.getProperty(poolName + ".redis.pool.port");

System.out.println(poolName + ".redis.pool.maxWait");

int maxWait = SsmUtil.convert(pro.getProperty(poolName + ".redis.pool.maxWait"), 0);

int maxActive = SsmUtil.convert(pro.getProperty(poolName + ".redis.pool.maxActive"), 0);

int maxIdle = SsmUtil.convert(pro.getProperty(poolName + ".redis.pool.maxIdle"), 0);

System.out.println((poolName + ".redis.pool.ip") + ":[ip:" + ip + " 端口:" + port + " 等待:" + maxWait + " maxActive:" + maxActive + " maxIdle:" + maxIdle + "]");

return new RedisPool(poolName, ip, port, maxWait, maxActive, maxIdle);

}

}

5、添加PropertiesUtil類

public class PropertiesUtil {

//用來測試的方法

public static void main(String [] args)

{

Properties p = getProperties("properties/redis.properties","utf-8");

System.out.println(p);

}

//解析properties檔案添加到properties類

public static Properties getProperties(String filepath,String charset) {

Properties pro = new Properties();

Reader reader = null;

InputStream is = null;

try{

is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filepath);

reader = new InputStreamReader(is, Charset.forName(charset));

pro.load(reader);

}

catch (Exception e)

{

e.printStackTrace();

}

finally{

if(is!=null){

try {

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return pro;

}

}

6、添加 SsmUtil類

public class SsmUtil {

//轉換資料類型方法

public static int convert(String property, int i) {

return Integer.parseInt(property);

}

}

轉載于:https://www.cnblogs.com/Xuesk/p/7465133.html