This article will take you through the three special data types in redis. Interested friends can learn about it~
Geospatial Geographic location
Hyperloglog Cardinality statistics
Redis video tutorial]
127.0.0.1:6379> GEOADD city 113.087559 28.251818 changsha (integer) 1 127.0.0.1:6379> GEOADD city 114.064552 22.548457 shenzhen (integer) 1 127.0.0.1:6379> GEOADD city 104.087045 30.666416 chengdu (integer) 1 127.0.0.1:6379> GEOADD city 118.802422 32.064653 nanjing (integer) 1 127.0.0.1:6379> GEOADD city 106.558434 29.568996 chongqing (integer) 1 127.0.0.1:6379> GEOADD city 121.463615 31.195908 shanghai (integer) 1 127.0.0.1:6379> GEOADD city 117.208093 39.091103 tianjin (integer) 1GEOPOS
127.0.0.1:6379> GEOPOS city changsha 1) 1) "113.08755666017532349" 2) "28.25181827470789386" 127.0.0.1:6379> GEOPOS city tianjin 1) 1) "117.20809489488601685" 2) "39.0911021322545551"GEODIST
m : meter
km: kilometers
ft: feet
mi: miles
Get the distance between two cities
127.0.0.1:6379> GEODIST city changsha tianjin "1264101.6876" 127.0.0.1:6379> GEODIST city changsha tianjin km "1264.1017" 127.0.0.1:6379> GEODIST city changsha shenzhen km "641.9034"
GEOHASH
127.0.0.1:6379> GEOHASH city changsha 1) "wt02tr5fg00" 127.0.0.1:6379> GEOHASH city changsha beijing 1) "wt02tr5fg00" 2) (nil) 127.0.0.1:6379> GEOHASH city changsha beijing tianjin chongqing 1) "wt02tr5fg00" 2) (nil) 3) "wwgq7hk64t0" 4) "wm7b0yc7zk0"
GEORADIUS
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC ] [STORE key] [STOR127.0.0.1:6379> GEORADIUS city 110 30 500 m (empty array) 127.0.0.1:6379> GEORADIUS city 110 30 500 km 1) "chongqing" 2) "changsha" 127.0.0.1:6379> GEORADIUS city 110 30 1000 km 1) "chongqing" 2) "chengdu" 3) "shenzhen" 4) "changsha" 5) "nanjing" 127.0.0.1:6379> GEORADIUS city 110 30 700 km withcoord 1) 1) "chongqing" 2) 1) "106.55843228101730347" 2) "29.56899626404301529" 2) 1) "chengdu" 2) 1) "104.08704489469528198" 2) "30.6664164635846177" 3) 1) "changsha" 2) 1) "113.08755666017532349" 2) "28.25181827470789386" 127.0.0.1:6379> GEORADIUS city 110 30 700 km withdist 1) 1) "chongqing" 2) "335.6530" 2) 1) "chengdu" 2) "572.3911" 3) 1) "changsha" 2) "357.4711" 127.0.0.1:6379> GEORADIUS city 110 30 700 km withhash 1) 1) "chongqing" 2) (integer) 4026059435699931 2) 1) "chengdu" 2) (integer) 4026137831798506 3) 1) "changsha" 2) (integer) 4050903792284309
GEORADIUSBYMEMBER
GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]127.0.0.1:6379> GEORADIUSBYMEMBER city tianjin 1000 km 1) "nanjing" 2) "tianjin" 3) "shanghai" 127.0.0.1:6379> GEORADIUSBYMEMBER city tianjin 500 km 1) "tianjin" 127.0.0.1:6379> GEORADIUSBYMEMBER city tianjin 5000 km 1) "chongqing" 2) "chengdu" 3) "shenzhen" 4) "changsha" 5) "shanghai" 6) "nanjing" 7) "tianjin"The underlying principle of Geospatial is implemented using Zset ordered collection
, we can use Zset ordered collection To operate Geo, we use the Zset command to experience a wave 127.0.0.1:6379> ZRANGE city 0 -1
1) "chongqing"
2) "chengdu"
3) "shenzhen"
4) "changsha"
5) "shanghai"
6) "nanjing"
7) "tianjin"
127.0.0.1:6379> ZCARD city
(integer) 7
Hyperloglog Cardinality Statistics
The base number is a non-repeating number, for example:
A = {1,2,3,4,5}
B = {2,3 ,4,5,6}
Then the base of the union of A and B is 6
IntroductionHyperloglog is redis 2.8.9 This kind of data structure is available at the beginning of version
redis hyperloglog cardinality statistics is also an algorithm
For example, there is such an application scenario:
The traditional way is to use a set to save the ID, and count the number of IDs in the set to calculate the number of people
There is no problem with this method, but if the amount of data is large, it will be very troublesome, because it is useless for us to get the id, we only need to count
## Hyperloglog occupies a fixed memory space, 2^16 power, and only requires 12 KB of memory. From the perspective of memory, Hyperloglog is the first choice for technology selection.
PFADD key element [element ...]
将多个 Hyperloglog 取并集,得到一个结果 Hyperloglog ,里面的数据是不会重复的
127.0.0.1:6379> PFADD myhash 1 2 3 4 5 6 7 8 (integer) 1 127.0.0.1:6379> pfadd myhash2 3 4 5 6 7 8 9 0 88 99 (integer) 1 127.0.0.1:6379> PFMERGE res myhash myhash2 OK 127.0.0.1:6379> PFCOUNT res (integer) 12 127.0.0.1:6379> PFCOUNT myhash (integer) 8 127.0.0.1:6379> PFCOUNT myhash2 (integer) 10
Bitmaps 位图,位存储
一般用于,统计用户信息,活跃,不活跃,
登录,不登录
打卡,不打卡 等等,两种状态
Bitmaps 位图,也是一种数据结构,是操作二进制的方式来进行记录的,只有 0 和 1 两种状态
例如我们举个例子,来记录着一周每天的心情,1 是开心,0 是沮丧
设置 bit 位的值
获取 bit 位的值
127.0.0.1:6379> SETBIT week 0 1 (integer) 0 127.0.0.1:6379> SETBIT week 1 1 (integer) 0 127.0.0.1:6379> SETBIT week 2 1 (integer) 0 127.0.0.1:6379> SETBIT week 3 0 (integer) 0 127.0.0.1:6379> SETBIT week 4 0 (integer) 0 127.0.0.1:6379> SETBIT week 5 1 (integer) 0 127.0.0.1:6379> SETBIT week 6 1 (integer) 0 127.0.0.1:6379> GETBIT week 6 (integer) 1 127.0.0.1:6379> GETBIT week 5 (integer) 1
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of An in-depth analysis of the three special data types in redis. For more information, please follow other related articles on the PHP Chinese website!