No-sql's Redis common commands

Source: Internet
Author: User
Tags echo message redis labs

From: http://blog.csdn.net/nicewuranran/article/details/51793760 No-sql's Redis Introduction

Redis is a memory-based Key-value high-performance storage system, similar to memcached, but Redis supports a rich data structure type, and it also supports the persistence of persisted to disk.

Redis is a data structure server. It is open-source, networked, in-memory, and stores keys with optional durability. The development of Redis has been sponsored by Redis Labs since June2015. Before that, it was sponsored by Pivotal Software and by VMware. According to the monthly ranking by DB-Engines.com, Redis is the most popular key-value database. Redis has also been ranked the #1 NoSQL (and #4 database) in User Satisfactionand Market Presence based on user reviews, the most popular NoSQL database in containers, and the #1 NoSQL among Top 50 Developer Tools & Services. The name Redis means REmote DIctionary Server.

Note:

NO-SQL(特指key-value型)数据库其实是比较简单的,和传统关系型数据库相比,其学习成本是非常低的,但是学习其收获远大于学习成本,所以应该花点时间好好学些。NO-SQL数据库本身并不难,它们的难点在于清楚它们的应用场景,知道什么时候使用NO-SQL能得到最大的益处。
Redis Common Commands

I think the best command to use is

help command参考redis官网:http://redis.io
Connection command
select index(change the selected database for current connection,index从0开始的整数.)//重点掌握quit (close the connection)//重点掌握auth password(authenticate to the server)echo message(echo the given string)ping [message](ping the server,this command is often used to test if a connection is still alive,是一条测试连通性命令)//了解
Server command
info [section] (Get information and statistics about the server)//Key Master client list (get the list of client connections)//Master D Bsize (return the number of the keys in the selected database)//Master Flushall (remove all keys form all databases)//master with caution flushdb ( Remove all key from the current database)//Mastering Caution with role (return the role of the instance in the context of replication)//Mastering Understanding S Ave (synchronously Save the dataset to disk)//Master slaveof host port (make the server A slave of another instance,or promote it as Master)//Master Understanding bgrewriteaof (asynchronously rewrite the append-only file) Bgsave (asynchronously Save the database to Di  SK) Client SetName (set the connection name) client GetName (get the current connection name) client Pause timeout (stop Processing commands from clients for some time) client reply On|off|skip (instruct the server whether to reply to commands) Command (get array of redis command details) command count (get total number of REDIS commands) command Getkeys (Extract key s given a fullredis command) command info command-name[command-name ...] (Get array of specifi Redis command details) config get parameter (get the value of a configuration parameter) Config Rewrit E (rewrite the configuration file with the on memory configuration) config set parameter (set a configuration parameter to T He give Value) Config Resetstat (reset the stats returned by info), Debug object key (Get debugging information about a key) d Ebug Segfault (make the server crash) Lastsave (get the Unix timestamp of the last successful save to disk) monitor (listen Requests received by server in real time shutdown [Nosave|save] (synchronously save dataset to disk and then shut Down the server) slowlog subcommand [argument] (manages the redis slow queres log) sync (internal command used for Repliatio N) Time (return the Currect server time)
Keys command
Del key [Key ...] (Delete a key or some keys)//Key master exists key [key ...] (Determine if a key exists)//Key Master expire key seconds (set a key ' s time to live in seconds)//Key Master persist key (remove the EX Piration form a key)//Key Master Randomkey (return a random key from the keyspace)//Key Master Rename key Newkey (rename a key)//Key Master key s pattern (Find all keys matching the given pattern)//Key Master TTL key (get the time to live for a key)//Key Master type key (determin E The type stored at key)//Key Master Expireat key Timestatmp (set the expiration for a key as a UNIX timestatmp)//Key to master sort key [ By Pattern][limit offset Count]//todo to be researched (sort the elements in a list,set or sorted set)//key to Master Move key db (move a key t o another database)//understanding Migreate//todo to understand and use Object subcommand [argument[argument ...] (Inspect the internals of Redis objects) Pexpire key milliseconds (set a key ' s time to live in milliseconds) pexpireat key m Illiseconds (set the expiration for a key as a UNIX time specified on milliseconds) ptll key (get the time to LIve for a key in milliseconds) Renamenx key Newkey (rename a key,only if the new key does not exist) restore key TTL Seriali Zed-value [Replace] (Create a key using the provided serialized value, previously obtained using dum) wait numslaves timeou T (wait for the synchronous replication of all the write commands sent in the context of the current connection) scan Curso R [Match pattern] [count Count] (incrementally iterate the keys space) dump key (return a serialized verison of the value s Tored at the specified Eky)
String data type related commands
Append key value (append a value to a key)//Key Master Set key value [EX seconds] [PX milliseconds] [nx| XX] (set the string value of a key) Setex key seconds Vlaue (set the value and expiration of a key)//Key Master Setnx key value (s Et the value of a key, only if the key does not exist)//Key Master Get key (get the value of a key)//Key Master Getset key value (set th E string value of a key and return its old value)//Master Understanding mset Key Value[key Value ...] (set multiple keys to multiple values)//Key Master Msetnx key value [key value ...] (set multiple keys to multiple values,only if none of the keys exist)//key to Master Mget Key[key ...] (Get the values of all the given keys)//Key Master Strlen key (get the length of the value stored in a key)//Key Master INCR key (Increm ENT The integer value of a key by one)///Key Master incr key increment (increment the integer value of a key by the given Incremen  T)//Key Master DECR key (decrement the integer value of a key by one)///Key Master Decrby key Decrement (decrement the integer value of a Key by the given decrement)//Key Master GETRAnge key start end (get a sbustring of the string stored at a key) SetRange key offset value (overwrite part of a string at Key starting at the specified offset) incrbyfloat key increment (increment the float value of a key by the given increment ) Psetex key milliseconds value (set the value and expiration in milliseconds of a key)
总结:string类型的数据结构主要操作就是赋值和取值操作,以及赋值时设置失效时间。redis里面的大部分操作的时间复杂度都是O(1),因为redis存储数据时是先计算key的hash码根据这个码值来存储的。
Hash data structure related commands
Hset key field value (set the string value of a hash field)//Key Master Hget key field (get the value of a hash field)//Key Master Hmset key field value [field value ...] (set multiple hash to multiple values)//Key Master Hmget key field [field ...]  (Get the values of all the given hash field)//Key Master Hsetnx key fields value (set the value of a hash field,only if the field does not exist)//Understanding Hexists key field (determine if a hash field exists)//Mastering Understanding Hkeys Key (Get all the field in a hash)//mastering Learn Hvals key (get all of the values in a hash)//Mastering Understanding Hdel key field [field ...] (Delte one or moew hash fields)//Master Understanding Hlen Key (Get the number of fields in a hash) Hgetall key (Get all the fields and Valu Es in a hash) Hincrby key field increment (increment the integer value of a hash field by the given increment) hincrbyfloat Key field increment (increment the float value of a hash field by the given amount) Hstrlen key field (get the length of th E Value of a hash field) Hscan key cursor [match pattern] [count Count] (IncrementaLly iterate hash fields and associated values)//todo pending study 

How to understand the hash data structure inside Redis

准确的说是这种类型    Map<String key,Map<String field,String value>>    Map<String key,JavaBean javaBean>    userinfo [id:1,name:xxx,friends:10...]redis里使用的就是如此的数据结构,有点像java中对象的属性名和属性值来存储,即key-value键值。备注:类似java中的HashMap<String,Object>与Python总的字典结构
List data structure related commands
Lpush key value [value ...] (prepend one or multiple values to a list)//Important Master Lpushx key value (prepend a value to a list,only if the list exists) Rpush Key value [value ...] (Append one or multiple to a list)//Important Master Rpushx key value [append a value to a list,only if the list Exists]lpop key (Remov E and get the first element in a list)//Important Master Rpop key (remove and get the last element in a list)//Important Master Rpoplpush source des Tination (Remove the last element in a list,prepend it to another list and return it)//Important master Brpoplpush source destination T Imeout (pop a value from a list,push it to another list and return It;or block until one is available)//Important master, can do this method to block the queue L Len Key (get the length of a list) Lrange key start Stop (get a range of elements form a list)//Important Master Blpop key [key ...] Tim Eout (Remoce and get the first element in a list,or block until one is available) Brpop key [key ...] timeout (remove and g Et the last element in a list,or block until one was available) lindex key index (get an element forM a list by it index) Linsert key Before|after Pivot value (insert an element before or after another element in a list) ls ET key index value (set the value of an element in a list by it index) LTrim key start Stop (trim a list to the specified Range) Lrem key count value (remove elements from a list)
备注:redis中的list数据结构更像是队列结构,并且其可以实现阻塞队列功能。所以可能用来做消息队列简单实现,但是我个人并不推荐使用redis做消息队列,可以使用更标准的消息中间件来做队列服务,例如RabbitMQ。
Redis transaction-related commands
multi (mark the start of a transaction block,类似于开启一个事务)//重点掌握exec (execute all commands issued after multi,类似于提交事务)//重点掌握discard (discard all commands issued after multi,类似回滚事务)//重点掌握watch key [key ...] (watch the given keys to determine executio of the multi/exec block)//重点掌握unwatch (forget about all watched keys)//重点掌握关于watch说明,由于redis是单线程的,在redis2.2之前,没有引入watch功能,如果client_x开启事务了,对a变量执行更新,但是此时还没提交事务,在这个过程中如果client_y对变量a也执行了更新操作,那么对于clinet_x来说它读的a变量已经发生变化了,但是它自己并不知道,就导致业务处理上就错了,所以在redis2.2之后引入了watch功能,它可以监控key是否变化,如果在开启事务后提交事务前有其它线程对这个key执行过更新操作,那么本次提交事务就会失败,就会回滚。
Redis location (GEO)-related commands
备注:redis geo功能是redis3.2之后才支持的,使用需注意geoadd locationSet longitude latitude name [longitude latitude name...](增加地址坐标使用经度和纬度和定位)geopos locationSet name [name] (显示某个地址的坐标)geodist locationSet location_x location_y [unit] (计算地理位置两点的距离)georadius locationSet longitude  latitude radius m|km|ft|mi  [WITHCOORD] [WITHDIST] [ASC|DESC] [COUNT count] (范围查找)georadiusbymemer location-set location radius m|km|ft|mi [WITHCOORD] [WITHDIST] [ASC|DESC] [COUNT count] (范围查找)

No-sql's Redis common commands

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.