Redis服务器简单操作类及GEO坐标附近位置查询

package com.util;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


public class JedisUtil {
	private Logger log = Logger.getLogger(JedisUtil.class);

	private static JedisPool jedisPool = null;

	/** 缓存生存时间 */
	private final static int expire = 60000;

	static {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal((JRedisPoolConfig.MAX_ACTIVE));
		config.setMaxIdle(JRedisPoolConfig.MAX_IDLE);
		config.setMaxWaitMillis(JRedisPoolConfig.MAX_WAIT);
		// 在获取连接的时候检查有效性, 默认false
		config.setTestOnBorrow(JRedisPoolConfig.TEST_ON_BORROW);
		config.setTestOnReturn(JRedisPoolConfig.TEST_ON_RETURN);
		// redis如果设置了密码:
		jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP,
		 JRedisPoolConfig.REDIS_PORT, 10000, JRedisPoolConfig.REDIS_PASSWORD);

		//jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP, JRedisPoolConfig.REDIS_PORT, 10000);
	}

	public static JedisPool getPool() {
		return jedisPool;
	}

	/**
	 * 从jedis连接池中获取获取jedis对象
	 */
	public static Jedis getJedis() {
		return jedisPool.getResource();
	}

	/**
	 * 回收jedis
	 */
	public static void returnJedis(Jedis jedis) {
		if (jedis != null)
			jedisPool.returnResource(jedis);
	}

	public static void close(Jedis jedis){
		jedis.close();
	}

	/**
	 * 设置过期时间
	 */
	public static void expire(String key, int seconds) {
		if (seconds <= 0) {
			return;
		}
		Jedis jedis = getJedis();
		jedis.expire(key, seconds);
		returnJedis(jedis);
	}

	/**
	 * 设置默认过期时间
	 */
	public static void expire(String key) {
		expire(key, expire);
	}

	public static void set(String key, String value) {
		if (isBlank(key))
			return;
		Jedis jedis = getJedis();
		jedis.set(key, value);
		returnJedis(jedis);
	}

	public static long ttl(String key){
		Jedis jedis = getJedis();
		return jedis.ttl(key);
	}
	
	public static void set(String key, String value,int seconds) {
		if (seconds <= 0||isBlank(key)) {
			return;
		}
		Jedis jedis = getJedis();
		jedis.set(key, value);
		jedis.expire(key, seconds);
		returnJedis(jedis);
	}

	public static void set(String key, Object value) {
		if (isBlank(key))
			return;
		Jedis jedis = getJedis();
		jedis.set(key.getBytes(), SerializeUtil.serialize(value));
		returnJedis(jedis);
	}

	public static void set(String key, int value) {
		if (isBlank(key))
			return;
		set(key, String.valueOf(value));
	}

	public static void set(String key, long value) {
		if (isBlank(key))
			return;
		set(key, String.valueOf(value));
	}

	public static void set(String key, float value) {
		if (isBlank(key))
			return;
		set(key, String.valueOf(value));
	}

	public static void set(String key, double value) {
		if (isBlank(key))
			return;
		set(key, String.valueOf(value));
	}

	public static Float getFloat(String key) {
		if (isBlank(key))
			return null;
		return Float.valueOf(getStr(key));
	}

	public static Double getDouble(String key) {
		if (isBlank(key))
			return null;
		return Double.valueOf(getStr(key));
	}

	public static Long getLong(String key) {
		if (isBlank(key))
			return null;
		return Long.valueOf(getStr(key));
	}

	public static Integer getInt(String key) {
		if (isBlank(key))
			return null;
		return Integer.valueOf(getStr(key));
	}

	public static String getStr(String key) {
		if (isBlank(key))
			return null;
		Jedis jedis = getJedis();
		String value =null;
		if(jedis.exists(key)){
			value = jedis.get(key);
		}
		returnJedis(jedis);
		return value;
	}
	public static void del(String key) {
		if (isBlank(key))
			return;
		Jedis jedis = getJedis();
		if(jedis.exists(key)){
			jedis.del(key);
		}
		returnJedis(jedis);
	}
	/**
	 * Map数据删除
	 * @title
	 * @date 2017年8月14日
	 * @author niuchuang
	 * @param key
	 * @param fields
	 */
	public static void zrem(String key,String fields[]) {
		if (isBlank(key) || fields==null || fields.length<1){
			return;
		}
		Jedis jedis = getJedis();
		for (String field : fields) {
			jedis.zrem(key,field);
		}
		returnJedis(jedis);
	}
	

	public static Object getObj(String key) {
		if (isBlank(key))
			return null;
		Jedis jedis = getJedis();
		byte[] bits = jedis.get(key.getBytes());
		Object obj = SerializeUtil.unserialize(bits);
		returnJedis(jedis);
		return obj;
	}
	
	public static void geodist(String key){
		if (StringUtils.isBlank(key))
			return;
		Jedis jedis = JedisUtil.getJedis();
		try {
			jedis.geodist(key, "23.132361,113.377826", "23.132361,113.377826");
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//即时释放jedis
			JedisUtil.close(jedis);
		}
	}

	public static boolean isBlank(String str) {
		return str == null || "".equals(str.trim());
	}
}


 
  

package com.geo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.util.JedisUtil;

import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.geo.GeoRadiusParam;

/**
 * 
 * @data 2017年5月24日
 * com.hnisi.geo.service.GeoRedisService
 */
public class GeoRedisService {
	 private final static Logger logger = LoggerFactory.getLogger(GeoCodingServiceTest.class);
	 
	public void addGeoAll(String executeType,Map geoCoordinateMap){
		Jedis jedis = JedisUtil.getJedis();
		try {
			jedis.geoadd(executeType, geoCoordinateMap);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//即时释放jedis
			JedisUtil.close(jedis);
		}
	}
	
	public void addGeo(String executeType,String ywId,double dlzbjd,double dlzbwd){
		Map geoCoordinateMap = new HashMap();
		GeoCoordinate coordinate = new GeoCoordinate(dlzbjd, dlzbwd);
		geoCoordinateMap.put(ywId, coordinate);
		addGeoAll(executeType, geoCoordinateMap);
	}
	/**
	 * redis坐标删除接口实现
	 * @title
	 * @date 2017年8月14日
	 * @author niuchuang
	 * @param key
	 */
	public void delGeo(String key){
		if (StringUtils.isBlank(key)){
			return;
		}
		logger.info("进入企业坐标删除:"+key);
		Jedis jedis = JedisUtil.getJedis();
		try {
			List responseList = jedis.georadius(key, 113.377826, 23.132361, 500000000d, GeoUnit.M, GeoRadiusParam.geoRadiusParam().withCoord().withDist().sortAscending());
			if (responseList!=null && responseList.size()>0) {
				for (GeoRadiusResponse geoRadiusResponse : responseList) {
					String member = geoRadiusResponse.getMemberByString();
					jedis.zrem(key,member);
					//logger.info(key+"坐标删除成功:"+JSON.toJSONString(member));
				}
			}else{
				logger.info("没有数据,无需删除");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//即时释放jedis
			JedisUtil.returnJedis(jedis);
		}
		logger.info("离开企业坐标删除:"+key);
	}
}


 
  

你可能感兴趣的:(Redis服务器简单操作类及GEO坐标附近位置查询)