package com.ssi.model; import com.google.common.base.Strings; import com.ssi.utils.JsonUtil; import java.util.Collection; import java.util.HashMap; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.GeoRadiusResponse; import redis.clients.jedis.GeoUnit; import redis.clients.jedis.Jedis; import redis.clients.jedis.params.geo.GeoRadiusParam; import redis.clients.util.Pool; import java.util.List; import java.util.Map; import java.util.Set; /** * Description: * * @author LiXiaoCong * @version 2019/7/12 15:37 */ @Slf4j public class RedisDataModel { private Pool pool; public RedisDataModel(Pool pool) { this.pool = pool; } public String get(String key) { String json = null; if (!Strings.isNullOrEmpty(key)) { Jedis jedis = null; try { jedis = getJedis(); json = jedis.get(key); } catch (Exception e) { log.error(String.format("读取%s失败", key), e); } finally { close(jedis); } } return json; } public Map getJson2Map(String key) { Map map = null; if (!Strings.isNullOrEmpty(key)) { Jedis jedis = null; try { jedis = getJedis(); String json = jedis.get(key); if (!Strings.isNullOrEmpty(json)) { map = JsonUtil.jsonToMap(json); } } catch (Exception e) { log.error(String.format("读取%s失败", key), e); } finally { close(jedis); } } return map; } public Set keys(String key) { Set keys = null; if (!Strings.isNullOrEmpty(key)) { Jedis jedis = null; try { jedis = getJedis(); keys = jedis.keys(key); } catch (Exception e) { log.error(String.format("获取所有前缀为%s的key失败。", key), e); } finally { close(jedis); } } return keys; } public List mget(String... key) { List keys = null; if (key != null) { Jedis jedis = null; try { jedis = getJedis(); keys = jedis.mget(key); } catch (Exception e) { log.error(String.format("获取所有前缀为%s的key失败。", key), e); } finally { close(jedis); } } return keys; } public Map mgetForMap(Collection key) { val value = mget(key.toArray(new String[0])); assert value != null; assert value.size() == key.size(); Map map = new HashMap<>(); int i = 0; for (String k : key) { map.put(k, value.get(i)); i++; } return map; } public Map getJson2Map(String key, String field) { Map map = null; if (!Strings.isNullOrEmpty(key)) { Jedis jedis = null; try { jedis = getJedis(); //String json = jedis.hget(key, field); String redisKey = String.format("%s,%s", key, field); String json = jedis.get(redisKey); if (!Strings.isNullOrEmpty(json)) { map = JsonUtil.jsonToMap(json); } } catch (Exception e) { log.error(String.format("读取%s$%s失败", key, field), e); } finally { close(jedis); } } return map; } public Map hgetJson2Map(String key, String field) { Map map = null; if (!Strings.isNullOrEmpty(key)) { Jedis jedis = null; try { jedis = getJedis(); String json = jedis.hget(key, field); //String redisKey = String.format("%s,%s",key,field); // String json = jedis.get(redisKey); if (!Strings.isNullOrEmpty(json)) { map = JsonUtil.jsonToMap(json); } } catch (Exception e) { log.error(String.format("读取%s$%s失败", key, field), e); } finally { close(jedis); } } return map; } public void hset(String key, String field, String value) { if (!Strings.isNullOrEmpty(key)) { Jedis jedis = null; try { jedis = getJedis(); jedis.hset(key, field, value); } catch (Exception e) { log.error(String.format("存储%s,%s,%s失败", key, field, value), e); } finally { close(jedis); } } } public boolean set(String key, String value) { if (!Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(value)) { Jedis jedis = null; try { jedis = getJedis(); jedis.set(key, value); return true; } catch (Exception e) { log.error(String.format("读取%s失败", key), e); } finally { close(jedis); } } return false; } public boolean del(String key) { if (!Strings.isNullOrEmpty(key)) { Jedis jedis = null; try { jedis = getJedis(); Long l = jedis.del(key); return true; } catch (Exception e) { log.error(String.format("读取%s失败", key), e); } finally { close(jedis); } } return false; } public void close(Jedis jedis) { if (jedis != null) { try { jedis.close(); } catch (Exception e) { } } } public Jedis getJedis() { Jedis jedis = null; try { jedis = (Jedis) pool.getResource(); } catch (Exception e) { log.error("获取jedis实体失败。"+e.getMessage(),e); } return jedis; } public String getMember(String key, double longitude, double latitude, double radius) { String memberByString = null; Jedis jedis = null; try { long l = System.currentTimeMillis(); jedis = getJedis(); GeoRadiusParam geoRadiusParam = GeoRadiusParam.geoRadiusParam(); geoRadiusParam.sortAscending().count(1); List georadius = jedis.georadius(key, longitude, latitude, radius, GeoUnit.M, geoRadiusParam); if (georadius != null && !georadius.isEmpty()) { memberByString = georadius.get(0).getMemberByString(); } log.debug(String.format("从redis中获取高程数据耗时:%s", (System.currentTimeMillis() - l))); } catch (Exception e) { log.error(String.format("读取%s失败", key), e); } finally { close(jedis); } return memberByString; } public boolean set(String key, String value, Long expireTime) { if (!Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(value)) { Jedis jedis = null; try { jedis = getJedis(); jedis.set(key, value); jedis.expire(key, expireTime.intValue()); return true; } catch (Exception e) { log.error(String.format("读取%s失败", key), e); } finally { close(jedis); } } return false; } }