Cache database-redis data types and operations

Source: Internet
Author: User
Tags bit set timedelta

Redis supports five types of data: string (String), hash (hash), list, set (set), and Zset (sorted set: Ordered set)

I: String (String)

The string is the most basic type of redis, and you can understand it as a type that is identical to memcached, a key that corresponds to a value.

The string type is binary safe. This means that a Redis string can contain any data. For example, JPG images or serialized objects.

The string type is the most basic data type of Redis, and a key can store up to 512MB.

The string in Redis is stored in memory by a name corresponding to a value.

-------------------------------------------------------------------------------------

1) Set (Name,value,ex=none,px=none,nx=false,xx=false)

Set the value in Redis, create it without default, modify it if it exists

Ex: Expiration Time (seconds)

PX: Expiration Time (ms)

NX: If set to true, the current set operation is performed only if name does not exist

XX: If set to true, the pre-post set operation is executed only if name exists

2) setnx (Name,value)

Set the value to perform the set operation (add) only if name does not exist

= Set (name value,nx=true)

3) Setex (name,value,time)

# time, expiration (number of seconds or Timedelta object

=set (Name,value,ex=time)

4) Psetex (name, Time_ms, value)

# Time_ms, Expiration time (numeric milliseconds or Timedelta object

=set (Name,value,px=time)

5) Mset (*args, **kwargs)

Batch setting values

such as: Mset (k1= "v1", k2= "v2")

-----------------------------------------------------------------------------------

6) Get the value of Get (name)

7) Mget (Keys,*args) bulk acquisition

Mget (K1,K2)

8) Getset (Name,value) set the new value and get the original value

9) GetRange (key,start,end) Gets the subsequence (obtained by Byte, non-character)

# Name,redis's name

# Start, start position (bytes)

#end, End position (bytes)

For example: "China" 0-3 means "medium"

Setranage (name,offset,value) modifies the contents of the string, starting from the specified string index and replacing it (adding backward if the new value is too long)

One) Setbit (Name,offset,value) operates on bits of the binary representation of the name corresponding value

    

#operation on binary representation of name corresponding value #Parameters:    #Name,redis's name    #offset, the index of the bit (converting the value to binary before indexing)    #value, which can only be 1 or 0 #Note: If there is a corresponding in Redis: N1 = "Foo",Then the binary representation of String Foo is: 01100110 01101111 01101111So, if you execute Setbit ('N1', 7, 1), the 7th bit is set to 1, then the final binary becomes01100111 01101111 01101111, i.e.:"Goo" #to extend, convert binary representations:     #Source = "Wu Jianzi"Source ="Foo"      forIinchSource:num=Ord (i)PrintBin (num). replace ('b',"'in particular, if source is a kanji"Wu Jianzi"What to do? Answer: for UTF-8, each Kanji account is 3 bytes, then"Wu Jianzi"Then there are 9 bytes for the kanji, the For loop is iterated by byte, then each byte is converted to decimal number at iteration, then the decimal number is converted to binary11100110 10101101 10100110 11100110 10110010 10011011 11101001 10111101 10010000--------------------------------- ---------------------- -----------------------------Wu Jianzi* Use for example, in the most space-saving way, the number of online users and which users are online each user ID, user ID number is the number of the set number of 1, through the Bitcount () Statistics 1 number, represents how many online users
Advantages statistics Fast, less space: 100 million users only need to occupy 100M space 1m=1024*1024

Getbit (Name,offset) Gets the value of a bit in the binary representation of the name corresponding to the value (0 or 1)

Bitcount (Key, Start=none, End=none) Gets the value of name corresponding to the number of 1 in the binary representation

strlen (name) returns the byte length of the corresponding value of name (3 bytes for a kanji)

INCR (self, name, amount=1) increment the value corresponding to name, create Name=amount if name does not exist, otherwise, increment.

Incrbyfloat (self, name, amount=1.0) increases the value of name and, when name does not exist, creates a name=amount, otherwise, the increment.

DECR (self, name, amount=1) the value corresponding to the name, when name does not exist, creates the Name=amount, otherwise, the decrement.

Append (key, value) append content after the value corresponding to Redis name

Practice viewing

 

#-*-coding:utf-8-*-__author__='Shisanjun'[BEGIN]2017/7/29 15:51:31[[Email protected]~]#redis-cli127.0.0.1:6379> Set name1 Shi#set a Key-valueOK127.0.0.1:6379>Set name2 Sanok127.0.0.1:6379> set Name3 June ex 3#set the Name3 expiration time to 3 secondsOK127.0.0.1:6379>get Name3 (nil)127.0.0.1:6379> KEYS *#View all keys1)"name1"2)"name2"3)"Foo"127.0.0.1:6379> set name2 June NX#does not exist, is created, does not exist(nil)127.0.0.1:6379> set Name3 June NX#does not exist, is created, does not existOK127.0.0.1:6379> set name1 111 xx#exists is created, does not exist and does not createOK127.0.0.1:6379> set Name4 111 xx#exists is created, does not exist and does not create(nil)127.0.0.1:6379> setnx Name5 222#does not exist, is created, does not exist(integer) 1127.0.0.1:6379> setnx name5 222(integer) 0"Ten"127.0.0.1:6379> Setex Name6 3 333#set expiration of 3 seconds Setex key time valueOK127.0.0.1:6379>get Name6 (nil)127.0.0.1:6379> Psetex Name7 1 777#set expiration of 1 milliseconds Setex key time valueOK127.0.0.1:6379> MSET K1"v1"K2"v2" #Batch setupOK127.0.0.1:6379> keys *)"name1"2)"K1"3)"Name5"4)"K2"5)"name2"6)"Name3"7)"Foo"127.0.0.1:6379> mget K1 K2#Bulk View1)"v1"2)"v2"127.0.0.1:6379> Getset K1"v3" #set the new value back to the old value"v1"127.0.0.1:6379>Get K1"v3"127.0.0.1:6379> Set K3"China"OK127.0.0.1:6379> GETRANGE K3 0 3#get from that byte, a Chinese 3 byte"\xe4\xb8\xad\xe5"127.0.0.1:6379> SETRANGE K3 6 1111#set from that byte(integer) 10127.0.0.1:6379>Get K3"\xe4\xb8\xad\xe5\x9b\xbd1111"127.0.0.1:6379> set N1"Foo"OK127.0.0.1:6379> Setbit N1 7 1#left-to-right 7th bit set 1(integer) 0127.0.0.1:6379>get N1"Goo"127.0.0.1:6379> Getbit N1 7#get the 7th bit is 1 or 0(integer) 1127.0.0.1:6379> bitcount N1 0 3#gets the number of brokered binary 1 from 0-3 bytes(integer) 17127.0.0.1:6379> STRLEN K3#number of statistics bytes(integer) 10127.0.0.1:6379> set n2 1OK127.0.0.1:6379> INCR N2#Self-increment(integer) 2127.0.0.1:6379>INCR n2 (integer)3127.0.0.1:6379>INCR n2 (integer)4127.0.0.1:6379> DECR N2#Self-reduction(integer) 3127.0.0.1:6379>decr n2 (integer)2127.0.0.1:6379> incrbyfloat N2 1.0#self-increment by floating point number"3"127.0.0.1:6379> incrbyfloat N2 1.0"4"127.0.0.1:6379> set N3 4.0127.0.0.1:6379> incrbyfloat N3 2.1"6.1"127.0.0.1:6379> APPEND N3 2222#Append(integer) 7127.0.0.1:6379>get N3"6.12222"[END]2017/7/29 16:05:41
View Code

Cache database-redis data types and operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.