No-sql之redis常用命令

时间:2023-01-17 16:42:11

转自:http://blog.csdn.net/nicewuranran/article/details/51793760

No-SQL之Redis


介绍

Redis是一种基于内存存储的key-value高性能存储系统,类似memcached,但是redis支持丰富的数据结构类型,并且其还支持数据持久化到磁盘。

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 June
2015. 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 Satisfaction
and 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.

备注:

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

Redis常用命令

我觉得最好用的命令是

help command
参考redis官网:http://redis.io

Connection命令

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 命令

info [section] (get information and statistics about the server)//重点掌握
client list(get the list of client connections)//掌握
dbsize (return the number of keys in the selected database)//掌握
flushall (remove all keys form all databases)//掌握慎用
flushdb (remove all key from the current database)//掌握慎用
role (return the role of the instance in the context of replication)//掌握了解
save (Synchronously save the dataset to disk)//掌握
slaveof host port (make the server a slave of another instance,or promote it as master)//掌握了解
bgrewriteaof(Asynchronously rewrite the append-only file)
bgsave(Asynchronously save the database to disk)
client setname(set the current 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 keys 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 rewrite(rewrite the configuration file with the in memory configuration)
config set parameter (set a configuration parameter to the give value)
config resetstat (reset the stats returned by info)
debug object key (get debugging information about a key)
debug segfault (make the server crash)
lastsave (get the unix timestamp of the last successful save to disk)
monitor (listen for all 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 repliation)
time (return the currect server time)

keys 命令

del key [key...] (delete a key or some keys)//重点掌握
exists key [key ...] ( determine if a key exists)//重点掌握
expire key seconds (set a key's time to live in seconds)//重点掌握
persist key (remove the expiration form a key)//重点掌握
randomkey (return a random key from the keyspace)//重点掌握
rename key newkey (rename a key)//重点掌握
keys pattern (find all keys matching the given pattern)//重点掌握
ttl key (get the time to live for a key)//重点掌握
type key (determine the type stored at key)//重点掌握
expireat key timestatmp (set the expiration for a key as a unix timestatmp)//重点掌握
sort key [by pattern][limit offset count] //TODO 待研究 (sort the elements in a list,set or sorted set)//重点掌握
move key db (move a key to another database)//了解
migreate //TODO 待理解和使用
object subcommand [argument[argument...]] (inspect the internals of Redis objects)
pexpire key milliseconds (set a key's time to live in milliseconds)
pexpireat key milliseconds (set the expiration for a key as a unix time specified in 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 serialized-value [replace] (create a key using the provided serialized value, previously obtained using dum)
wait numslaves timeout (wait for the Synchronous replication of all the write commands sent in the context of the current connection)
scan cursor [match pattern] [count count] (incrementally iterate the keys space)
dump key (return a serialized verison of the value stored at the specified eky)

String数据类型相关命令

append key value (append a value to a key)//重点掌握
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)//重点掌握
setnx key value (set the value of a key ,only if the key does not exist)//重点掌握
get key (get the value of a key)//重点掌握
getset key value (set the string value of a key and return its old value)//掌握了解
mset key value[key value ...] (set multiple keys to multiple values)//重点掌握
msetnx key value [key value ...] (set multiple keys to multiple values,only if none of the keys exist)//重点掌握
mget key[key ...] (get the values of all the given keys)//重点掌握
strlen key (get the length of the value stored in a key)//重点掌握
incr key (increment the integer value of a key by one)//重点掌握
incr key increment (increment the integer value of a key by the given increment)//重点掌握
decr key (decrement the integer value of a key by one)//重点掌握
decrby key decrement (decrement the integer value of a key by the given decrement)//重点掌握
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数据结构相关命令

hset key field value (set the string value of a hash field)//重点掌握
hget key field (get the value of a hash field)//重点掌握
hmset key field value [field value...] (set multiple hash fields to multiple values)//重点掌握
hmget key field [field...] (get the values of all the given hash fields)//重点掌握
hsetnx key field value (set the value of a hash field,only if the field does not exist)//了解
hexists key field (determine if a hash field exists)//掌握了解
hkeys key (get all the fields in a hash)//掌握了解
hvals key (get all the values in a hash)//掌握了解
hdel key field [field...] (delte one or moew hash fields)//掌握了解
hlen key (get the number of fields in a hash)
hgetall key (get all the fields and values 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 the value of a hash field)
hscan key cursor [match pattern] [count count] (incrementally iterate hash fields and associated values)//TODO 待研究

如何理解Redis里面的Hash数据结构

准确的说是这种类型
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数据结构相关命令

lpush key value [value...] (prepend one or multiple values to a list)//重要掌握
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)//重要掌握
rpushx key value [append a value to a list,only if the list exists]
lpop key (remove and get the first element in a list)//重要掌握
rpop key (remove and get the last element in a list)//重要掌握
rpoplpush source destination (remove the last element in a list,prepend it to another list and return it)//重要掌握
brpoplpush source destination timeout (pop a value from a list,push it to another list and return it;or block until one is available)//重要掌握,可以用这个方法做阻塞队列
llen key (get the length of a list)
lrange key start stop (get a range of elements form a list)//重要掌握
blpop key [key ...] timeout (remoce and get the first element in a list,or block until one is available)
brpop key [key ...] timeout (remove and get the last element in a list,or block until one is available)
lindex key index (get an element form a list by its index)
linsert key before|after pivot value (insert an element before or after another element in a list)
lset key index value (set the value of an element in a list by its 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事务相关命令

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定位(GEO)相关命令

备注: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之redis常用命令的更多相关文章

  1. Redis常用命令

    Redis常用命令Redis提供了丰富的命令对数据库和各种数据类型进行操作,这些命令可以再Linux终端使用.1.键值相关命令2.服务器相关命令 一.键值相关命令 1.get get 键值 当 key ...

  2. 第2讲 Redis常用命令与高级应用

    目录 一.redis数据类型 5. sorted sets类型和操作 二.Redis常用命令 1.键值相关命令 2.服务器相关命令 三. redis高级应用 1. 给redis服务器设置密码 2.持久 ...

  3. Redis常用命令手册:服务器相关命令

    Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如各类语言包,这些命令都有对应的方法.下面将Redis提供的命令做一 ...

  4. MySQL进口&period;sql文件和常用命令

    MySQL进口.sql文件和常用命令 在MySQL Qurey   Brower中直接导入*.sql脚本,是不能一次运行多条sql命令的.在mysql中运行sql文件的命令: mysql> so ...

  5. redis&&num;160&semi;redis常用命令及内存分析总结&lpar;附RedisClient工具简介

    redis常用命令及内存分析总结(附RedisClient工具简介 by:授客 QQ:1033553122 redis-cli工具 查看帮助 连接redis数据库 常用命令 exists key se ...

  6. Redis常用命令与高级应用

    附: 127.0.0.1:6379> set xiaofei 小飞 OK 127.0.0.1:6379> get xiaofei "\xe5\xb0\x8f\xe9\xa3\x9 ...

  7. Redis快速起步及Redis常用命令大全

    本系列教程内容提要 Java工程师之Redis实战系列教程教程是一个学习教程,是关于Java工程师的Redis知识的实战系列教程,本系列教程均以解决特定问题为目标,使用Redis快速解决在实际生产中的 ...

  8. redis配置密码 redis常用命令

    redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 [plain] view plain copy   #requi ...

  9. redis常用命令及持久化机制

    redis  常用命令 查找redis服务文件 find / -name  redis-server 查找配置文件 find / -name redis.conf 启动服务时候,要指定配置文件 启动r ...

  10. Redis 常用命令 大全

    Redis 常用命令 发现几个很好的 Redis 常用命令汇总大全网页,分享给小伙伴们~ 1.Redis 命令参考 http://redisdoc.com/string/index.html 2.W3 ...

随机推荐

  1. hibernate学习笔记之二 基本环境搭建

    1.创建一个普通的java项目 2.加入Hibernate所有的jar包 3.建立包名com.bjpowernode.hibernate 4.建立实体类User.java package com.bj ...

  2. WPF 自定义DateControl DateTime控件

    自定义日期控件,月份选择.如下是日期的一些效果图. 具体的样式.颜色可以根据下面的代码,自己调节即可    1.日期控件的界面 <UserControl x:Class="WpfApp ...

  3. zend studio 注释快捷键

    使用zend studio编写程序时,我们经常要做一些注释.zend studio为我们提供了行注释和块注释的快捷键ctrl+slash和ctrl+shift+slash,开始不明白什么是slash, ...

  4. Quartz&period;NET 2&period;0 学习笔记&lpar;1&rpar; :Quartz&period;NET简介

    http://www.cnblogs.com/lzrabbit/archive/2012/04/13/2447609.html

  5. c&num; 数据库更新操作-文本更新和数值更新小差别

    1.文本更新 string strName; sql = "update 模式表 a SET 模式名称 ='"+ strName +"'where a.模式ID =&qu ...

  6. Java VisualVM无法检测到本地java程序 的 解决办法

    win10系统下启动jvisualvm应用,报"VisualVM无法检测到本地java程序"的错误!在网上查了一些方法, 大概原因有2种: 1.操作系统的临时文件目录所在的磁盘格式 ...

  7. MyBatis 学习总结 01 快速入门

    本文测试源码下载地址: http://onl5wa4sd.bkt.clouddn.com/MyBatis0918.rar 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级 ...

  8. Linux 部署 xxl-job 注意问题

    问题:Failed to create parent directories for [/data/applogs/xxl-job/xxl-job-admin.log][原因:权限不足] 启动终端: ...

  9. 二十六&period; Python基础&lpar;26&rpar;--类的内置特殊属性和方法

    二十六. Python基础(26)--类的内置特殊属性和方法 ● 知识框架 ● 类的内置方法/魔法方法案例1: 单例设计模式 # 类的魔法方法 # 案例1: 单例设计模式 class Teacher: ...

  10. Linux 日常运维

    查看用户信息:w 查看系统负载:uptime 查看系统资源使用情况:vmstat 查看进程动态:top 查看网卡流量:sar 查看网卡流量:nload 查看磁盘读写:iostat 查看磁盘读写:iot ...