Bitmap
Bitmap
Is through a bit
to represent an element or a value corresponding to the status bits
Wherein the key is the corresponding element itself, but also by the fact underlying the operation of the character string to achieve
Redis
From the 2.2
new version after the setbit
, getbit
, bitcount
and several other bitmap related commands
Although the new command, but are themselves operating on a string
SETBIT key offset value
Which offset
must be a number, value can only be a 0
or1
This command return value is the value before the modification
For example, call
setbit byte0 0 1; setbit byte0 2 1; setbit byte0 5 1; setbit byte1 1 1; setbit byte1 4 1;
Corresponding to the value of the memory is such
byte | bit0 | bit1 | bit2 | bit3 | bit4 | bit5 | bit6 | bit7 |
---|---|---|---|---|---|---|---|---|
byte0 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
byte1 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
You can see bit
the default value is 0
then Bitmap
used in the actual development of it?
To give an example: store user online
Here only one key, and then as a user ID offset
, if it is set to online 1
, offline is set to0
// Set online $ redis-> Online setBit 0 1 ; // set offline $ redis-> setBit Online 0 0 ; // get the state $ redis-> GETBIT Online 0 ; // get the number of online $ redis -> bitCount online;
Geo
Redis
The GEO
properties in Redis 3.2
the launch version
This function can be given by the user location information is stored, and the operation information
GEO data structure There are six command
-
geoadd
-
geopos
-
geodist
-
georadius
-
georadiusbymember
-
geohash
GEOADD
GEOADD key longitude latitude member [longitude latitude member ...]
Given spatial elements (latitude, longitude, name) added to the specified key inside
These data will be zset
a structure which is stored in the key
Such as setting
key
Is Sicily
(islands in Assisi, Italy)
member1
As Palermo
(the Sicilian regional capital Palermo, Italy)
member2
As Catania
(the capital of Italy Sicily Province of Catania Catania)
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
GEOPOS
GEOPOS key member [member ...]
Returns all elements from a given position inside the key position (longitude and latitude)
For example, callGEOPOS Sicily Palermo
Returns the Palermo
latitude and longitude
redis> GEOPOS Sicily Palermo
1) 1) "13.361389338970184"
2) "38.115556395496299"
GEODIST
GEODIST key member1 member2 [unit]
Returns the given distance between the two positions
If a location between the two does not exist, then returnnull
Parameter specifies the units unit
must be in the following units wherein a default :( m)
-
m
Meter -
km
km -
mi
mile -
ft
foot
For example, callGEODIST Sicily Palermo Catania
Return Palermo
and Catania
distance166274.15米
redis> GEODIST Sicily Palermo Catania
"166274.15156960039"
GEORADIUS
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [ASC|DESC] [COUNT count]
A given latitude and longitude as the center, from the position of the element which contains the return key, the center does not exceed a given maximum distance elements in all positions
Consistent distance units and above, where the latter option:
-
WITHDIST
While the return position of the element, the distance between the center position of the elements is also returned together
User distance units and given range consistent units
-
WITHCOORD
The latitude and longitude position of the element is also returned together
-
WITHHASH
In
52
bit signed integer form of the element after the original return position geohash encoded ordered set valueThis option is mainly used for debugging or underlying application, the actual effect is not large in
For example, callGEORADIUS Sicily 15 37 200 km WITHDIST
It means that the return key
is Sicily
in in
To 经度15
, 纬度37
as the center, the radius of 200km
the elements within the circular range, and the distance to the center element
redis> GEORADIUS Sicily 15 37 200 km WITHDIST
1) 1) "Palermo"
2) "190.4424"
2) 1) "Catania"
2) "56.4413"
HyperLogLog
Redis
Cardinality statistics
This structure can be very provincial statistics to various counts of memory, such as 注册 IP 数
, 每日访问 IP 数
, 页面实时UV
, 在线用户数
etc.
But it also has its limitations, is only the number of statistics, and no way to know what specific content
PFADD
Redis> PFADD Databases "Redis" "MongoDB" "MySQL" (Integer) 1 Redis > PFADD Databases "Redis" # Redis already exists, do not have to update the estimate of the number (Integer) 0
PFCOUNT
redis> PFCOUNT databases
(integer) 3
PFMERGE
PFMERGE achievement sourcekey [sourcekey ...]
Multiple HyperLogLog
merge into oneHyperLogLog
The combined HyperLogLog
base close to all inputs HyperLogLog
of可见集合的并集
The combined results of HyperLogLog
will be stored in the destkey
key inside
If the key does not exist, then the command before execution, will create a space for the key HyperLogLog
redis> PFADD nosql "Redis" "MongoDB" "Memcached" (integer) 1 redis> PFADD RDBMS "MySQL" "MSSQL" "PostgreSQL" (integer) 1 redis> PFMERGE databases nosql RDBMS OK redis> PFCOUNT databases (integer) 6