1,Redis的安装参考:https://blog.csdn.net/zhu_tianwei/article/details/44890579
2,Jedis使用2.90版本,maven配置:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
3,Java代码实现:
package com.lairen.admin.controller.platform;
import redis.clients.jedis.*;
import redis.clients.jedis.params.geo.GeoRadiusParam;
import java.util.List;
import java.util.Map;
/**
* Created by lairen on 17/6/20.
*/
public class RedisUtil {
private static JedisPool jedisPool = null;
// Redis服务器IP
private static String ADDR = "127.0.0.1";
// Redis的端口号
private static int PORT = 6379;
// 访问密码
private static String AUTH = null;
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
config.setBlockWhenExhausted(true);
// 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
// 是否启用pool的jmx管理功能, 默认true
config.setJmxEnabled(true);
// 最大空闲连接数, 默认8个 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(8);
// 最大连接数, 默认8个
config.setMaxTotal(200);
// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(1000 * 100);
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
jedisPool = new JedisPool(config, ADDR, PORT, 3000, AUTH);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
*
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
*
* @param jedis
*/
public static void close(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
/**
* 增加地理位置的坐标
* @param key
* @param coordinate
* @param memberName
* @return
*/
public static Long geoadd(String key, GeoCoordinate coordinate, String memberName) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.geoadd(key,coordinate.getLongitude(),coordinate.getLatitude(),memberName);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
/**
* 批量添加地理位置
* @param key
* @param memberCoordinateMap
* @return
*/
public static Long geoadd(String key, Map<String, GeoCoordinate> memberCoordinateMap){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.geoadd(key, memberCoordinateMap);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
/**
* 根据给定地理位置坐标获取指定范围内的地理位置集合(返回匹配位置的经纬度 + 匹配位置与给定地理位置的距离 + 从近到远排序,)
* @param key
* @param coordinate
* @param radius
* @return List<GeoRadiusResponse>
*/
public static List<GeoRadiusResponse> geoRadius(String key, GeoCoordinate coordinate, double radius) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.georadius(key, coordinate.getLongitude(), coordinate.getLatitude(), radius, GeoUnit.KM, GeoRadiusParam.geoRadiusParam().withDist().withCoord().sortAscending());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
/**
* 根据给定地理位置获取指定范围内的地理位置集合(返回匹配位置的经纬度 + 匹配位置与给定地理位置的距离 + 从近到远排序,)
* @param key
* @param member
* @param radius
* @return List<GeoRadiusResponse>
*/
List<GeoRadiusResponse> georadiusByMember(String key, String member, double radius){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.georadiusByMember(key, member, radius, GeoUnit.KM, GeoRadiusParam.geoRadiusParam().withDist().withCoord().sortAscending());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
/**
* 查询两位置距离
* @param key
* @param member1
* @param member2
* @param unit
* @return
*/
public static Double geoDist(String key, String member1, String member2, GeoUnit unit){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.geodist(key, member1, member2, unit);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
/**
* 可以获取某个地理位置的geohash值
* @param key
* @param members
* @return
*/
public static List<String> geohash(String key, String... members){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.geohash(key, members);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
/**
* 获取地理位置的坐标
* @param key
* @param members
* @return
*/
public static List<GeoCoordinate> geopos(String key, String... members){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.geopos(key, members);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (null != jedis)
jedis.close();
}
return null;
}
}
---------------------
作者:csdn_guang
来源:CSDN
原文:https://blog.csdn.net/u010185805/article/details/73607519
版权声明:本文为博主原创文章,转载请附上博文链接!