redis 5.0五大数据类型的操作命令

目前:还没有redis 5.0的windows版本

Set Key value
Get Key
DEL key1 key2 key3 (To delete a key).
EXISTS key1 key2 (To check a key exist or not).
TTL key  (To check time to live).
EXPIRE key 10(in seconds).
PTTL mykey (to check time in millisecond).
PEXPIRE mykey 1500 (Time in Milliseconds).
PERSIST mykey (Remove EXPIRATION from the key)
KEYS a?? (Returns all keys matching pattern)
支持的正则表达式:
h?llo matches hello,hallo and hxllo 
h*llo matches hllo and heeeello
h[ae]llo matches hello and hallo ,but not hillo 
h[^e]llo matches hallo ,hbllo,... but not hello 
h[a-b]llo matches hallo and hbllo

RANDOMKEY (Return a random key from the currently selected database)
· RENAME mykey myotherkey
· RENAMENX mykey myotherkey (Renames key to newkey if newkey does not yet exist)
· TOUCH key1 key2 (Alters the last access time of a key(s).
· UNLINK key1 key2 key3 (The actual removal will happen later asynchronously.)
· TYPE key1 (Return Type of Value)
· DUMP mykey (Serialize the value stored at key in a Redis-specific format)
· RESTORE mykey 0 "\n\x17\x17\x00\x00\x00\x12\x00\x00\x00\x03\x00\”


· APPEND mykey "Hello" (appends the value at the end of the string).
· INCR mykey (Increments the number stored at key by one).
· INCRBY mykey 5
· DECR mykey (Decrements the number stored at key by one.)
· DECRBY mykey 3 (Decrements the number stored at key )
· INCRBYFLOAT mykey 0.1(Increment the string representing a floating point number)
· GETSET mycounter "0" (Atomically sets key to value and returns the old value stored at key)
· MSET key1 "Hello" key2 "World" (Sets the given keys to their respective values.)
· MGET key1 key2 (Returns the values of all specified keys).
· SETNX mykey "Hello" (Set key to hold string value if key does not exist.)
· MSETNX key1 "Hello" key2 "there" (Sets the given keys to their respective values.)
· GETRANGE mykey 0 3 (Returns the substring of the string value stored at key).
· GETRANGE mykey -3 -1. (-1 means the last character and so on).
· SETEX mykey 10 "Hello" (Set key to hold the string value and set key to timeout after a given number of seconds.)
· PSETEX mykey 1000 "Hello" ( expire time is specified in milliseconds instead of seconds.)
· SETRANGE key1 6 "Redis" (Overwrites part of the string stored at key) .
· STRLEN mykey (Returns the length of the string).


RPUSH mylist "hello" (Insert all the specified values at the tail of the list stored at key.)
LRANGE mylist 0 -1 (Returns the specified elements of the list stored at key.)
LPUSH mylist "world" (Insert all the specified values at the head of the list stored at key.)
RPUSHX mylist "World" (Inserts value at the tail of the list stored at key, only if key already exists and holds a list.)
LPUSH mylist "World" (Inserts value at the head of the list stored at key, only if key already exists and holds a list.)
RPOP mylist (Removes and returns the last element of the list stored at key.)
LPOP mylist (Removes and returns the first element of the list stored at key.)
LTRIM mylist 1 -1 (Trim an existing list so that it will contain only the specified range of elements specified)
LSET mylist 0 "four" (Sets the list element at index to value.)
LINDEX mylist 0 (Returns the element at index in the list stored at key.)
LINSERT mylist BEFORE "World" "There" (Inserts value in the list stored at key either before or after the reference value pivot.)
LLEN mylist (Returns the length of the list stored at key. )
LREM mylist 2 "hello" (Removes the first count occurrences of elements equal to value from the list stored at key.)


--------
HSET myhash field1 "Hello".  (Sets field in the hash stored at key to value. If field already exists in the hash, it is overwritten.)
HGET myhash field1 (Returns the value associated with field in the hash stored at key.)

HMSET myhash field1 "Hello" field2 "World" ( Set Multiple values.)
HGETALL myhash  (Returns all fields and values of the hash stored at key.)
HMGET myhash field1 field2 (Returns the values associated with the specified fields in the hash stored at key.)
HEXISTS myhash field1 (Returns if field is an existing field in the hash stored at key.)
HKEYS myhash (Returns all field names in the hash stored at key.)
HLEN myhash (Returns the number of fields contained in the hash stored at key.)
HSETNX myhash field "Hello" (Sets field in the hash stored at key to value, only if field does not yet exist.)
HDEL myhash field1 (Removes the specified fields from the hash stored at key.)
HINCRBY myhash field 1 (Increments the number stored at field in the hash stored at key by increment.)
HINCRBYFLOAT mykey field 0.1 (Increment the specified field of a hash stored at key, and representing a floating-point number, by the specified increment.)
HSTRLEN myhash f1 (Returns the string length of the value associated with field in the hash stored at key.)
HVALS myhash (Returns all values in the hash stored at key.)

-----------------------------------------------------------------------------------------
SADD myset "Hello" Add the specified members to the set stored at key.
SMEMBERS myset (Returns all the members of the set value stored at key.)
SISMEMBER myset "one” (Returns if member is a member of the set stored at key.)

SCARD myset(Returns the set cardinality (number of elements) of the set stored at key.)

SMOVE myset myotherset "two" (Move member from the set at source to the set at destination.)
SPOP myset (Removes and returns one or more random elements from the set value store at key.)
SREM myset "one" (Remove the specified members from the set stored at key.)

SDIFF key1 key (Returns the members of the set resulting from the difference between the first set and all the successive sets.)
SDIFFSTORE key key1 key2(This command is equal to SDIFF, but instead of returning the resulting set, it is stored in destination.)

SINTER key1 key2 (Returns the members of the set resulting from the intersection of all the given sets.)
SINTERSTORE key key1 key2(This command is equal to SINTER, but instead of returning the resulting set, it is stored in destination.)
SUNION key1 key2 (Returns the members of the set resulting from the union of all the given sets.)
SUNIONSTORE key key1 key2(This command is equal to SUNION, but instead of returning the resulting set, it is stored in destination.)
SRANDMEMBER myset 2 ( When called with just the key argument, return a random element from the set value stored at key.)

-----------------------------------------------------------------------------------
ZADD myzset 2 "two" 3 "three" (https://redis.io/commands/zadd  Refer this link to know more.)
ZRANGE myzset 0 -1 WITHSCORES (Returns the specified range of elements in the sorted set stored at key.)
ZCARD myzset (Returns the sorted set cardinality (number of elements) of the sorted set stored at key.)

ZREM myzset "two" (Removes the specified members from the sorted set stored at key. Non existing members are ignored.)
ZSCORE myzset "one" (Returns the score of member in the sorted set at key.)
ZREVRANGE myzset 0 -1 (Returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the highest to the lowest score.)
ZRANK myzset "three" (Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.)

ZREVRANK myzset "one" (Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low. The rank (or index) is 0-based, which means that the member with the highest score has rank 0.)
ZINCRBY myzset 2 "one" (Increments the score of member in the sorted set stored at key by increment.)
ZCOUNT myzset -inf +inf (Returns the number of elements in the sorted set at key with a score between min and max.)
ZPOPMAX myzset (Removes and returns up to count members with the highest scores in the sorted set stored at key.)
ZPOPMIN myzset (Removes and returns up to count members with the lowest scores in the sorted set stored at key.
ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 (Computes the intersection of numkeys sorted sets given by the specified keys, and stores the result in destination. )
ZUNIONSTORE out 2 zset1 zset2 WEIGHTS 2 3 (Computes the union of numkeys sorted sets given by the specified keys, and stores the result in destination.)

ZRANGEBYSCORE myzset (1 2 (Returns all the elements in the sorted set at key with a score between min and max)
ZRANGEBYLEX myzset - [c (https://redis.io/commands/zrangebylex Refer to this link to know more)
ZLEXCOUNT myzset [b [f (When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command returns the number of elements in the sorted set at key with a value between min and max.)
ZREVRANGEBYLEX myzset [c – (Apart from the reversed ordering, ZREVRANGEBYLEX is similar to ZRANGEBYLEX.)


ZREMRANGEBYLEX myzset [alpha [omega  (When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command removes all elements in the sorted set stored at key between the lexicographical range specified by min and max.)
ZREMRANGEBYRANK myzset 0 1 (Removes all elements in the sorted set stored at key with rank between start and stop.)
ZREMRANGEBYSCORE myzset -inf (2  (Removes all elements in the sorted set stored at key with a score between min and max (inclusive).)
ZREVRANGEBYSCORE myzset 2 (1  (Apart from the reversed ordering, ZREVRANGEBYSCORE is similar to ZRANGEBYSCORE.)

----------------------------------------------------------------------------------------------------
Data types
Reference:- Whole Article taken from Redis Docs (https://redis.io/topics/data-types)
Strings
Strings are the most basic kind of Redis value. Redis Strings are binary safe, this means that a Redis string can contain any kind of data, for instance a JPEG image or a serialized Ruby object.
A String value can be at max 512 Megabytes in length.
You can do a number of interesting things using strings in Redis, for instance you can:
•	Use Strings as atomic counters using commands in the INCR family: INCR, DECR, INCRBY.
•	Append to strings with the APPEND command.
•	Use Strings as a random access vectors with GETRANGE and SETRANGE.
Check all the available string commands for more information.

Lists
Redis Lists are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list.
The LPUSH command inserts a new element on the head, while RPUSH inserts a new element on the tail. A new list is created when one of this operations is performed against an empty key. Similarly the key is removed from the key space if a list operation will empty the list. These are very handy semantics since all the list commands will behave exactly like they were called with an empty list if called with a non-existing key as argument.
Some example of list operations and resulting lists:
1.	LPUSH mylist a   # now the list is "a"
2.	LPUSH mylist b   # now the list is "b","a"
3.	RPUSH mylist c   # now the list is "b","a","c" (RPUSH was used this time)
The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).
The main features of Redis Lists from the point of view of time complexity are the support for constant time insertion and deletion of elements near the head and tail, even with many millions of inserted items. Accessing elements is very fast near the extremes of the list but is slow if you try accessing the middle of a very big list, as it is an O(N) operation.
You can do many interesting things with Redis Lists, for instance you can:
•	Model a timeline in a social network, using LPUSH in order to add new elements in the user time line, and using LRANGE in order to retrieve a few of recently inserted items.
•	You can use LPUSH together with LTRIM to create a list that never exceeds a given number of elements, but just remembers the latest N elements.
•	Lists can be used as a message passing primitive, See for instance the well known Resque Ruby library for creating background jobs.

Please check all the available commands operating on lists for more information.

Sets
Redis Sets are an unordered collection of Strings. It is possible to add, remove, and test for existence of members in O(1) (constant time regardless of the number of elements contained inside the Set).
Redis Sets have the desirable property of not allowing repeated members. Adding the same element multiple times will result in a set having a single copy of this element. Practically speaking this means that adding a member does not require a check if exists then add operation.
A very interesting thing about Redis Sets is that they support a number of server side commands to compute sets starting from existing sets, so you can do unions, intersections, differences of sets in very short time.
The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).
You can do many interesting things using Redis Sets, for instance you can:
•	You can track unique things using Redis Sets. Want to know all the unique IP addresses visiting a given blog post? Simply use SADD every time you process a page view. You are sure repeated IPs will not be inserted.
•	Redis Sets are good to represent relations. You can create a tagging system with Redis using a Set to represent every tag. Then you can add all the IDs of all the objects having a given tag into a Set representing this particular tag, using the SADD command. Do you want all the IDs of all the Objects having three different tags at the same time? Just use SINTER.
•	You can use Sets to extract elements at random using the SPOP or SRANDMEMBER commands.
As usual, check the full list of Set commands for more information.
Hashes
Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (e.g. A User with a number of fields like name, surname, age, and so forth):
1.	@cli
2.	HMSET user:1000 username antirez password P1pp0 age 34
3.	HGETALL user:1000
4.	HSET user:1000 password 12345
5.	HGETALL user:1000
A hash with a few fields (where few means up to one hundred or so) is stored in a way that takes very little space, so you can store millions of objects in a small Redis instance.
While Hashes are used mainly to represent objects, they are capable of storing many elements, so you can use Hashes for many other tasks as well.
Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).
Check the full list of Hash commands for more information.
Sorted sets
Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.
With sorted sets you can add, remove, or update elements in a very fast way (in a time proportional to the logarithm of the number of elements). Since elements are taken in order and not ordered afterwards, you can also get ranges by score or by rank (position) in a very fast way. Accessing the middle of a sorted set is also very fast, so you can use Sorted Sets as a smart list of non repeating elements where you can quickly access everything you need: elements in order, fast existence test, fast access to elements in the middle!
In short with sorted sets you can do a lot of tasks with great performance that are really hard to model in other kind of databases.
With Sorted Sets you can:
•	Take a leader board in a massive online game, where every time a new score is submitted you update it using ZADD. You can easily take the top users using ZRANGE, you can also, given an user name, return its rank in the listing using ZRANK. Using ZRANK and ZRANGE together you can show users with a score similar to a given user. All very quickly.
•	Sorted Sets are often used in order to index data that is stored inside Redis. For instance if you have many hashes representing users, you can use a sorted set with elements having the age of the user as the score and the ID of the user as the value. So using ZRANGEBYSCORE it will be trivial and fast to retrieve all the users with a given interval of ages.
Sorted Sets are probably the most advanced Redis data types, so take some time to check the full list of Sorted Set commands to discover what you can do with Redis!




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值