Spring-data-redis1.8简略封装(支持geo)

Spring-data-redis1.8简单封装(支持geo)
  • 其实spring-data-redis中的RedisTemplate更强大全面,应公司要求,稍微简化

package test;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

/**
 * 
 * @ClassName:RedisDao
 * @Description:redis操作Dao:依赖spring-data-redis1.8,spring4.3.2,jedis2.9.0
 * @Author:chenchuanliang
 * @Date:2017年5月18日下午1:03:03
 * @version:1.0.0
 */
@Repository
public class RedisDao {
    /**
     * 日志记录
     */
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected RedisTemplate<String, String> redisTemplate;
    /**
     * 前缀
     */
    public static final String KEY_PREFIX_VALUE = "yb:value:";
    public static final String KEY_PREFIX_SET = "yb:set:";
    public static final String KEY_PREFIX_LIST = "yb:list:";
    public static final String KEY_PREFIX_GEO = "yb:geo:";

  /**
   * 
   * @MethodName:cacheValue
   * @param k
   * @param v
   * @param time(单位秒)  <=0 不过期
   * @return
   * @ReturnType:boolean
   * @Description:缓存value操作
   * @Creator:chenchuanliang
   * @CreateTime:2017年5月18日上午11:24:56
   * @Modifier:
   * @ModifyTime:
   */
    protected boolean cacheValue(String k, String v, long time) {
        String key = KEY_PREFIX_VALUE + k;
        try {
            ValueOperations<String, String> valueOps =  redisTemplate.opsForValue();
            valueOps.set(key, v);
            if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        } catch (Throwable t) {
            logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
        }
        return false;
    }

  /**
   * 
   * @MethodName:cacheValue
   * @param k
   * @param v
   * @return
   * @ReturnType:boolean
   * @Description:缓存value操作
   * @Creator:chenchuanliang
   * @CreateTime:2017年5月18日上午11:24:43
   * @Modifier:
   * @ModifyTime:
   */
    protected boolean cacheValue(String k, String v) {
        return cacheValue(k, v, -1);
    }

    /**
     * 
     * @MethodName:getValue
     * @param k
     * @return
     * @ReturnType:String
     * @Description:获取缓存
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:21:37
     * @Modifier:
     * @ModifyTime:
     */
    protected String getValue(String k) {
        try {
            ValueOperations<String, String> valueOps =  redisTemplate.opsForValue();
            return valueOps.get(KEY_PREFIX_VALUE + k);
        } catch (Throwable t) {
            logger.error("获取缓存失败key[" + KEY_PREFIX_VALUE + k + ", error[" + t + "]");
        }
        return null;
    }
  
    /**
    * 
    * @MethodName:cacheSet
    * @param k
    * @param v
    * @param time
    * @return
    * @ReturnType:boolean
    * @Description:缓存set操作
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:20:00
    * @Modifier:
    * @ModifyTime:
    */
    protected boolean cacheSet(String k, String v, long time) {
        String key = KEY_PREFIX_SET + k;
        try {
            SetOperations<String, String> valueOps =  redisTemplate.opsForSet();
            valueOps.add(key, v);
            if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
        } catch (Throwable t) {
            logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
        }
        return true;
    }

   /**
    * 
    * @MethodName:cacheSet
    * @param k
    * @param v
    * @return
    * @ReturnType:boolean
    * @Description:缓存set
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:19:00
    * @Modifier:
    * @ModifyTime:
    */
    protected boolean cacheSet(String k, String v) {
        return cacheSet(k, v, -1);
    }

   /**
    * 
    * @MethodName:cacheSet
    * @param k
    * @param v
    * @param time
    * @return
    * @ReturnType:boolean
    * @Description:缓存set
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:18:48
    * @Modifier:
    * @ModifyTime:
    */
    protected boolean cacheSet(String k, Set<String> v, long time) {
        String key = KEY_PREFIX_SET + k;
        try {
            SetOperations<String, String> setOps =  redisTemplate.opsForSet();
            setOps.add(key, v.toArray(new String[v.size()]));
            if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        } catch (Throwable t) {
            logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
        }
        return false;
    }

    /**
     * 
     * @MethodName:cacheSet
     * @param k
     * @param v
     * @return
     * @ReturnType:boolean
     * @Description:缓存set
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:18:34
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean cacheSet(String k, Set<String> v) {
        return cacheSet(k, v, -1);
    }

  /**
   * 
   * @MethodName:getSet
   * @param k
   * @return
   * @ReturnType:Set<String>
   * @Description:获取缓存set数据
   * @Creator:chenchuanliang
   * @CreateTime:2017年5月18日上午11:17:45
   * @Modifier:
   * @ModifyTime:
   */
    protected Set<String> getSet(String k) {
        try {
            SetOperations<String, String> setOps = redisTemplate.opsForSet();
            return setOps.members(KEY_PREFIX_SET + k);
        } catch (Throwable t) {
            logger.error("获取set缓存失败key[" + KEY_PREFIX_SET + k + ", error[" + t + "]");
        }
        return null;
    }

   /**
    * 
    * @MethodName:cacheList
    * @param k
    * @param v
    * @param time
    * @return
    * @ReturnType:boolean
    * @Description:list缓存
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:17:23
    * @Modifier:
    * @ModifyTime:
    */
    protected boolean cacheList(String k, String v, long time) {
        String key = KEY_PREFIX_LIST + k;
        try {
            ListOperations<String, String> listOps =  redisTemplate.opsForList();
            listOps.rightPush(key, v);
            if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        } catch (Throwable t) {
            logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
        }
        return false;
    }

   /**
    * 
    * @MethodName:cacheList
    * @param k
    * @param v
    * @return
    * @ReturnType:boolean
    * @Description:缓存list
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:17:10
    * @Modifier:
    * @ModifyTime:
    */
    protected boolean cacheList(String k, String v) {
        return cacheList(k, v, -1);
    }

  /**
   * 
   * @MethodName:cacheList
   * @param k
   * @param v
   * @param time
   * @return
   * @ReturnType:boolean
   * @Description:缓存list
   * @Creator:chenchuanliang
   * @CreateTime:2017年5月18日上午11:15:47
   * @Modifier:
   * @ModifyTime:
   */
    protected boolean cacheList(String k, List<String> v, long time) {
        String key = KEY_PREFIX_LIST + k;
        try {
            ListOperations<String, String> listOps =  redisTemplate.opsForList();
            listOps.rightPushAll(key, v);
            if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        } catch (Throwable t) {
            logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
        }
        return false;
    }

    /**
     * 
     * @MethodName:cacheList
     * @param k
     * @param v
     * @return
     * @ReturnType:boolean
     * @Description:缓存list
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:15:05
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean cacheList(String k, List<String> v) {
       return cacheList(k, v, -1);
    }

   /**
    * 
    * @MethodName:getList
    * @param k
    * @param start
    * @param end
    * @return
    * @ReturnType:List<String>
    * @Description:获取list缓存
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:14:45
    * @Modifier:
    * @ModifyTime:
    */
    protected List<String> getList(String k, long start, long end) {
        try {
            ListOperations<String, String> listOps =  redisTemplate.opsForList();
            return listOps.range(KEY_PREFIX_LIST + k, start, end);
        } catch (Throwable t) {
            logger.error("获取list缓存失败key[" + KEY_PREFIX_LIST + k + "]" + ", error[" + t + "]");
        }
        return null;
    }

    /**
     * 
     * @MethodName:getListSize
     * @param k
     * @return
     * @ReturnType:long
     * @Description:获取总条数
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:13:39
     * @Modifier:
     * @ModifyTime:
     */
    protected long getListSize(String k) {
        try {
            ListOperations<String, String> listOps =  redisTemplate.opsForList();
            return listOps.size(KEY_PREFIX_LIST + k);
        } catch (Throwable t) {
            logger.error("获取list长度失败key[" + KEY_PREFIX_LIST + k + "], error[" + t + "]");
        }
        return 0;
    }

   /**
    * 
    * @MethodName:removeOneOfList
    * @param k
    * @return
    * @ReturnType:boolean
    * @Description:移除list缓存
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:11:16
    * @Modifier:
    * @ModifyTime:
    */
    protected boolean removeOneOfList(String k) {
        try {
            ListOperations<String, String> listOps =  redisTemplate.opsForList();
            listOps.rightPop(KEY_PREFIX_LIST + k);
            return true;
        } catch (Throwable t) {
            logger.error("移除list缓存失败key[" + KEY_PREFIX_LIST + k + ", error[" + t + "]");
        }
        return false;
    }
    
    /**
     * 
     * @MethodName:cacheGeo
     * @param key
     * @param x
     * @param y
     * @param member
     * @param time(单位秒)  <=0 不过期
     * @return
     * @ReturnType:boolean
     * @Description:缓存地理位置信息
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:30:23
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean cacheGeo(String key, double x, double y, String member, long time) {
    	String k = KEY_PREFIX_GEO + key;
    	try {
    		GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
    		geoOps.geoAdd(k, new Point(x, y) , member);
    		if (time > 0) redisTemplate.expire(k, time, TimeUnit.SECONDS);
		} catch (Throwable t) {
			logger.error("缓存[" + k +"]" + "失败, point["+ x + "," + 
					y + "], member[" + member + "]" +", error[" + t + "]");
		}
		return true;
    }
    
    /**
     * 
     * @MethodName:cacheGeo
     * @param key
     * @param locations
     * @param time(单位秒)  <=0 不过期
     * @return
     * @ReturnType:boolean
     * @Description:
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:31:33
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean cacheGeo(String key, Iterable<GeoLocation<String>> locations, long time) {
    	try {
    		for (GeoLocation<String> location : locations) {
    			cacheGeo(key, location.getPoint().getX(), location.getPoint().getY(), location.getName(), time);
    		}
		} catch (Throwable t) {
			logger.error("缓存[" + KEY_PREFIX_GEO + key +"]" + "失败" +", error[" + t + "]");
		}
		return true;
	}
    
    /**
     * 
     * @MethodName:removeGeo
     * @param key
     * @param members
     * @return
     * @ReturnType:boolean
     * @Description:移除地理位置信息
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午10:53:01
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean removeGeo(String key, String...members) {
    	try {
    		GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
    		geoOps.geoRemove(key, members);
		} catch (Throwable t) {
			logger.error("移除[" + KEY_PREFIX_GEO + key +"]" + "失败" +", error[" + t + "]");
		}
    	return true;
    }
    
    /**
     * 
     * @MethodName:distanceGeo
     * @param key
     * @param member1
     * @param member2
     * @return Distance
     * @ReturnType:Distance
     * @Description:根据两个成员计算两个成员之间距离
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午10:58:33
     * @Modifier:
     * @ModifyTime:
     */
    protected Distance distanceGeo(String key, String member1, String member2) {
    	try {
    		GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
    		return geoOps.geoDist(key, member1, member2);
		} catch (Throwable t) {
			logger.error("计算距离[" + KEY_PREFIX_GEO + key +"]" + "失败, member[" + member1 + "," + member2 +"], error[" + t + "]");
		}
		return null;
    }
    
    /**
     * 
     * @MethodName:getGeo
     * @param key
     * @param members
     * @return
     * @ReturnType:List<Point>
     * @Description:根据key和member获取这些member的坐标信息
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:02:01
     * @Modifier:
     * @ModifyTime:
     */
    protected List<Point> getGeo(String key, String...members) {
    	String k = KEY_PREFIX_GEO + key;
    	try {
    		GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
    		return geoOps.geoPos(k, members);
		} catch (Throwable t) {
			logger.error("获取坐标[" + KEY_PREFIX_GEO + k +"]" + "失败]" + ", error[" + t + "]");
		}
		return null;
    }
    
    /**
     * 
     * @MethodName:radiusGeo
     * @param key
     * @param x
     * @param y
     * @param distance
     * @return
     * @ReturnType:GeoResults<GeoLocation<String>>
     * @Description:通过给定的坐标和距离(km)获取范围类其它的坐标信息
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:09:10
     * @Modifier:
     * @ModifyTime:
     */
    protected GeoResults<GeoLocation<String>> radiusGeo(String key, double x, double y, double distance) {
    	try {
    		String k = KEY_PREFIX_GEO + key;
    		GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
    		return geoOps.geoRadius(k, new Circle(x, y, distance));
		} catch (Throwable t) {
			logger.error("通过坐标[" + x + "," + y +"]获取范围[" + distance + "km的其它坐标失败]" + ", error[" + t + "]");
		}
		return null;
    }
    
    /**
     * 
     * @MethodName:containsListKey
     * @param k
     * @return
     * @ReturnType:boolean
     * @Description:判断缓存是否存在
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:23:37
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean containsValueKey(String k) {
        return containsKey(KEY_PREFIX_VALUE + k);
    }

    /**
     * 
     * @MethodName:containsListKey
     * @param k
     * @return
     * @ReturnType:boolean
     * @Description:判断缓存是否存在
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:23:37
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean containsSetKey(String k) {
        return containsKey(KEY_PREFIX_SET + k);
    }

   /**
    * 
    * @MethodName:containsListKey
    * @param k
    * @return
    * @ReturnType:boolean
    * @Description:判断缓存是否存在
    * @Creator:chenchuanliang
    * @CreateTime:2017年5月18日上午11:23:37
    * @Modifier:
    * @ModifyTime:
    */
    protected boolean containsListKey(String k) {
        return containsKey(KEY_PREFIX_LIST + k);
    }
    
    /**
     * 
     * @MethodName:containsGeoKey
     * @param k
     * @return
     * @ReturnType:boolean
     * @Description:判断缓存是否存在
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:34:14
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean containsGeoKey(String k) {
        return containsKey(KEY_PREFIX_GEO + k);
    }

    private boolean containsKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Throwable t) {
            logger.error("判断缓存存在失败key[" + key + ", error[" + t + "]");
        }
        return false;
    }
    
    /**
     * 
     * @MethodName:remove
     * @param key
     * @return
     * @ReturnType:boolean
     * @Description:移除key中所有缓存
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:20:19
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean removeValue(String k) {
        return remove(KEY_PREFIX_VALUE + k);
    }
    
    /**
     * 
     * @MethodName:remove
     * @param key
     * @return
     * @ReturnType:boolean
     * @Description:移除key中所有缓存
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:20:19
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean removeSet(String k) {
        return remove(KEY_PREFIX_SET + k);
    }
    
    /**
     * 
     * @MethodName:remove
     * @param key
     * @return
     * @ReturnType:boolean
     * @Description:移除key中所有缓存
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:20:19
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean removeList(String k) {
        return remove(KEY_PREFIX_LIST + k);
    }
    
    /**
     * 
     * @MethodName:removeGeo
     * @param k
     * @return
     * @ReturnType:boolean
     * @Description:移除key中所有缓存
     * @Creator:chenchuanliang
     * @CreateTime:2017年5月18日上午11:36:23
     * @Modifier:
     * @ModifyTime:
     */
    protected boolean removeGeo(String k) {
        return remove(KEY_PREFIX_GEO + k);
    }
    
    private boolean remove(String key) {
        try {
            redisTemplate.delete(key);
            return true;
        } catch (Throwable t) {
            logger.error("移除缓存失败key[" + key + ", error[" + t + "]");
        }
        return false;
    }
}