Redis 切片池与非切片池,连接指定库以及基本配置

Redis 是一个开源、支持网络、基于内存、键值对存储数据库。  
  
关于切片池和非切片池的区别,  一般项目基本都使用非切片池;切片池主要用于分布式项目,可以设置主从Redis库。  
  

如果需要指明Redis连接第几个库,需要在使用Redis进行数据操作之前使用如下语句:  


Jedis resource = jedisPool.getResource();  

resource.select(1);  


注意:select(1)表示指定Redis库的第二个库(第一个用0)。  
  
使用最新Redis jar包可以发现,释放redis连接的方法  jedisPool.returnResource(jedis);  在Redis3.0之后不再推荐使用,改为 jedis.close(); 来代替:  

starting from Jedis 3.0 this method will not be exposed. Resource cleanup should be done using @see Jedis.close()  


查看Redis数据库,我们可以通过 Redis桌面管理工具 在windows下查看。


软件下载地址:

Redis jar包

Redis桌面管理工具



以下提供Redis的基本操作类:  


/**
 * 主要用来给用户提供一个设计完备的,通过jedis的jar包来管理redis内存数据库的各种方法
 */
public class RedisManager {
	
	private static Logger log_error = Logger.getLogger("error");
	
	// ip、port、AUTH属性都定义在了config.properties文件中
	private static String host = CfgReader.getParam("REDIS_HOST");
	private static int port = Integer.parseInt(CfgReader.getParam("REDIS_PORT")); 

	//访问密码
    private static String AUTH = CfgReader.getParam("REDIS_AUTH");
    
    //可用连接实例的最大数目,默认值为8;
    //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
    private static int MAX_ACTIVE = 1024;
    
    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
    private static int MAX_IDLE = 200;
    
    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
    private static int MAX_WAIT = 60000;
    
    private static int TIMEOUT = 60000;
    
    //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    private static boolean TEST_ON_BORROW = true;
    
	// 设置为0的话就是永远都不会过期
	private static int expire = 0;

	// 定义一个管理池,所有的redisManager共同使用。
	private static JedisPool jedisPool = null;

	public RedisManager() {
	}

	/**
	 * 
	 * 初始化方法,在这个方法中通过host和port来初始化jedispool。
	 * 
	 */
	public static void init() {
		try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            if(RegexUtil.isEmpty(AUTH)) {
            	jedisPool = new JedisPool(config, host, port, TIMEOUT, AUTH);
            } else {
            	jedisPool = new JedisPool(config, host, port, TIMEOUT);
            }
        } catch (Exception e) {
            e.printStackTrace();
			log_error.error("-- Redis 连接出错--host="+host+",port="+port+",错误原因" + e);
        }
	}
	
    /**
     * 获取Jedis实例
     * @return
     */
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                resource.select(1);				//设置为第二个库(db1)
                return resource;
            } else {
    			MSGSendUtils.getInstance().sendMSG(CfgReader.getParam("error_name"), CfgReader.getParam("error_phone"), 
    					"获取的Redis链接为null");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
			MSGSendUtils.getInstance().sendMSG(CfgReader.getParam("error_name"), CfgReader.getParam("error_phone"), 
					"获取Redis链接出错:" + e.getMessage());
            return null;
        }
    }
    
	/**
	 * get value from redis
	 * 
	 * @param key
	 * @return
	 */
	public static byte[] get(byte[] key) {
		byte[] value = null;
		Jedis jedis = getJedis();
		try {
			value = jedis.get(key);
		} finally {
			jedis.close();
		}
		return value;
	}

	/**
	 * get value from redis
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		String value = null;
		Jedis jedis = getJedis();
		try {
			value = jedis.get(key);
		} finally {
			jedis.close();
		}
		return value;
	}

	/**
	 * set
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static void set(byte[] key, byte[] value) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key, value);
			if (expire != 0) {
				jedis.expire(key, expire);
			}
		} finally {
			jedis.close();
		}
	}

	/**
	 * set
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static void set(String key, String value) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key, value);
			if (expire != 0) {
				jedis.expire(key, expire);
			}
		} finally {
			jedis.close();
		}
	}

	/**
	 * set
	 * 
	 * @param key
	 * @param value
	 * @param expire
	 * @return
	 */
	public static void set(byte[] key, byte[] value, int expire) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key, value);
			if (expire != 0) {
				jedis.expire(key, expire);
			}
		} finally {
			jedis.close();
		}
	}

	/**
	 * set
	 * 
	 * @param key
	 * @param value
	 * @param expire
	 * @return
	 */
	public static void set(String key, String value, int expire) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key, value);
			if (expire != 0) {
				jedis.expire(key, expire);
			}
		} finally {
			jedis.close();
		}
	}

	/**
	 * del
	 * 
	 * @param key
	 */
	public static void del(byte[] key) {
		Jedis jedis = getJedis();
		try {
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}

	/**
	 * del
	 * 
	 * @param key
	 */
	public static void del(String key) {
		Jedis jedis = getJedis();
		try {
			jedis.del(key);
		} finally {
			jedis.close();
		}
	}

	/**
	 * 判断指定键是否存在
	 * @param key
	 * @return
	 */
	public static boolean exists(String key) {
		Jedis jedis = getJedis();
		boolean flag = jedis.exists(key);
		jedis.close();
		return flag ;
	}
	
	/**
	 * 获取key对应的值剩余存活时间
	 * @param key
	 * @return
	 * 		正数:剩余的时间(秒)
	 * 		负数:已过期
	 */
	public static Long ttlKey(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.ttl(key);
		} catch(Exception e) {
			log_error.error(" -- Redis 获取key对应的值剩余存活时间出错,出错原因:" + e);
			e.printStackTrace();
			return 0L;
		} finally {
			jedis.close();
		}
	}
	
	/**
	 * 获取key对应的值剩余存活时间
	 * @param key
	 * @return
	 * 		正数:剩余的时间(秒)
	 * 		负数:已过期
	 */
	public static Long ttlKey(byte[] key) {
		Jedis jedis = getJedis();
		try {
			return jedis.ttl(key);
		} catch(Exception e) {
			log_error.error(" -- Redis 获取key对应的值剩余存活时间出错,出错原因:" + e);
			e.printStackTrace();
			return 0L;
		} finally {
			jedis.close();
		}
	}
	
	/**
	 * 存储对象
	 * @param key
	 * @param obj
	 * @param expire
	 */
	public static void setObject(String key,Object obj,int expire) {
		byte[] data = ObjTOSerialize(obj);
		Jedis jedis = getJedis();
		jedis.set(key.getBytes(),data);
		if(expire != 0) {
			jedis.expire(key, expire);
		}
		jedis.close();
	}
	
	/**
	 * 获取对象
	 * @param key
	 * @return
	 */
	public static Object getObject(String key) {
		Jedis jedis = getJedis();
		byte[] data = jedis.get(key.getBytes());
		Object obj = null;
		if(data != null) {
			obj = unSerialize(data);
		}
		jedis.close();
		return obj;
	}
	
	/**
	 * 
	 * 序列化一个对象
	 * @param obj
	 * @return
	 */
	public static byte[] ObjTOSerialize(Object obj) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream byteOut = null;
		try {
			byteOut = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(byteOut);
			oos.writeObject(obj);
			byte[] bytes = byteOut.toByteArray();
			return bytes;
		} catch (Exception e) {
			log_error.error("-- Redis序列化对象出错:"+e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 反序列化一个对象
	 * @param bytes
	 * @return
	 */
	public static Object unSerialize(byte[] bytes) {
		ByteArrayInputStream in = null;
		try {
			in = new ByteArrayInputStream(bytes);
			ObjectInputStream objIn = new ObjectInputStream(in);
			return objIn.readObject();
		} catch (Exception e) {
			log_error.error("-- Redis反序列化对象出错:"+e);
			e.printStackTrace();
		}
		return null;
	}

}



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值