RedisTemplate使用
1.Geospatial 地理位置
1.添加
//1.key 2.value(经度,纬度) 3.name redisTemplate.opsForGeo().add("city",new Point(116.35,23.535),"jieyang");
2.获取城市(1个或者多个)的经纬度
System.out.println(redisTemplate.opsForGeo().position("city", "jieyang", "dongguan"));
3.计算两个城市的直线距离
redisTemplate.opsForGeo().distance("city","guangzhou","dongguan",KILOMETERS)
4.附近的城市
//距离300km Distance distance = new Distance(300, Metrics.KILOMETERS); //以揭阳这个地点为中心点,附件300km的城市 GeoResults<RedisGeoCommands.GeoLocation<Object>> radius = redisTemplate.opsForGeo().radius("city", "jieyang", distance); for( GeoResult<RedisGeoCommands.GeoLocation<Object>> radiu: radius.getContent()){ System.out.println(radiu.getContent().getName()); } //以133.11 23.037这个地点为中心点,附件300km的城市 System.out.println(redisTemplate.opsForGeo().radius("city", new Circle(new Point(113.11, 23.037), distance)));
2.Hyperloglog基数
1.统计不相同的数的数据,可用于网页浏览量的实现
//添加的7个数, redisTemplate.opsForHyperLogLog().add("myHkey","a","a","b","c","d","e","e"); //输出不同数的数量 System.out.println(redisTemplate.opsForHyperLogLog().size("myHkey"));
3.ZSet 有序集合
// 添加数据 1.key 2.value 3.index redisTemplate.opsForZSet().add("myzset","one",8); redisTemplate.opsForZSet().add("myzset","two",12); redisTemplate.opsForZSet().add("myzset","three",33); redisTemplate.opsForZSet().add("myzset","four",54); //按照index从小到大排序 1.key 2.从0开始 3.到-1 相对于输出全部 System.out.println(redisTemplate.opsForZSet().range("myzset", 0, -1)); //集合个数 System.out.println(redisTemplate.opsForZSet().zCard("myzset")); //index 0到20 之间的数量 System.out.println(redisTemplate.opsForZSet().count("myzset", 0, 20)); //index 0到20之间的value System.out.println(redisTemplate.opsForZSet().rangeByScore("myzset", 0, 20));
4.Hash
//添加数据 redisTemplate.opsForHash().put("myhash","name","cwd"); redisTemplate.opsForHash().put("myhash","age","18"); //取出数据 System.out.println(redisTemplate.opsForHash().entries("myhash")); //数量 System.out.println(redisTemplate.opsForHash().size("myhash"));