Redis中三种特殊的数据类型

geospatial

这个功能可以将用户给定的地理位置信息储存起来, 并对这些信息进行操作

GEO 的数据结构总共有六个命令

  • geoadd
  • geopos
  • geodist
  • georadius
  • georadiusbymember
  • geohash

增加一个定位:

geoadd china:city 116.40 39.90 beijing
geoadd china:city 106.50 29.53 chongqing

获取当前定位:

deopos china:city beijing

两个定位之间的距离:

单位:

  • m标识单位为米
  • km标识单位为千米
  • mi标识单位为英里
  • ft标识单位为英尺
geodist china:city beijing chongqing km

查询指定半径中的数据:

georadius china:city 100 30 1000 km   #以经度100,纬度30 半径1000km的内的城市
georadius china:city 100 30 1000 km withdist  #显示到中心位置的距离
georadius china:city 100 30 1000 km withcoord  #显示符合条件的定位
georadius china:city 100 30 1000 km withcoord count 1  #输出指定个数符合条件的


Hyperloglog

Hyperloglog基数统计的算法。不过会有些许误差,如果允许容错,使用Hyperloglog,不允许容错的话使用set

基数=不重复元素的个数

网页的UV(一个人访问了一个网站多次,但是还是算作一个人

127.0.0.1:6379> pfadd key1 a b c d e f g  #创建一组元素
(integer) 1
127.0.0.1:6379> pfcount key1  #统计key1中基数的数量
(integer) 7
127.0.0.1:6379> pfadd key2 f g h i z k b c  
(integer) 1
127.0.0.1:6379> pfcount key1
(integer) 7
127.0.0.1:6379> pfmerge key3 key1 key2  #合并两组key1和key2
OK
127.0.0.1:6379> pfcount key3
(integer) 11

Bitmap

Bitmap就是通过一个bit位来表示某个元素对应的值或者状态。Bitmaps位图,只有0和1两个状态。

位存储。可以用来统计用户信息,登陆,未登录;打卡。

用bitmap来记录周一到周日的打卡:

127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 1
(integer) 0
127.0.0.1:6379> setbit sign 2 1
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 0
(integer) 0

查看某一天是否打卡:

127.0.0.1:6379> getbit sign 3
(integer) 1

查看一周打卡的天数:

127.0.0.1:6379> bitcount sign
(integer) 4


 

发布了322 篇原创文章 · 获赞 61 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/wan_ide/article/details/105458618