数据类型(五大基本类型,3种特殊类型)
概述
官方文档
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
redis-key
move key dbindex //移除当前数据库中的key
expire key time(s) //设置key的过期时间,单位s
keys * //查询当前数据库中的key
exists key //判断key是否存在
ttl key //查看当前key的剩余时间,还有多长时间过期
type key //查看当前key的类型
String (字符串类型)
- get key
- set key
- exists key
- append key value 追加字符串
- strlen key查看字符串长度
strlen name
- 增加减少
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379> decr views
(integer) 1
127.0.0.1:6379> incrby views 10
(integer) 11
127.0.0.1:6379> get views
"11"
127.0.0.1:6379> decrby views 8
(integer) 3
127.0.0.1:6379> get views
- 字符串操作
127.0.0.1:6379> getrange name 1 2 # 获取部分字符串
"23"
127.0.0.1:6379> getrange name 1 5
"23ttt"
127.0.0.1:6379> getrange name 1 -1
"23tttfdafda"
127.0.0.1:6379> setrange name 2 nihao # 替换指定位置的字符串
(integer) 12
127.0.0.1:6379> get name
"12nihaodafda"
127.0.0.1:6379>
#####################################################################
# setex(set with expire) 设置过期时间
# setnx(set if not exist) 当键不存在,设置键值,存在设置失败;通常用在分布式数据库中用来作为分布式锁127.0.0.1:6379> setex name "hello" 10
(error) ERR value is not an integer or out of range
127.0.0.1:6379> setex name 30 "hello"
OK
127.0.0.1:6379> ttl name
(integer) 27
127.0.0.1:6379> setnx name
(error) ERR wrong number of arguments for 'setnx' command
127.0.0.1:6379> setnx name "world"
(integer) 0
127.0.0.1:6379> get name
"hello"
127.0.0.1:6379> setnx name1 "world"
(integer) 1
127.0.0.1:6379> get name1
"world"
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
####################################################################################################
# mset 同时设置key
127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3
OK
127.0.0.1:6379> keys *
1) "age"
2) "key2"
3) "views"
4) "key3"
5) "hh"
6) "name1"
7) "key1"
# mget同时获取Key
127.0.0.1:6379> mget key1 key2 key3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379>
# msetnx不存在设置,是一个原子性操作,要么一起成功,要么一起失败
127.0.0.1:6379> msetnx key1 v1 key4 v4
(integer) 0
127.0.0.1:6379> msetnx key5 v1 key4 v4
(integer) 1
# 先获取Key值,在设置Key
getset key value
使用场景
value除了可以是字符串还可以是数字。
- 计算器(incr,decr)
- 统计多单位的数量
- 粉丝数
- 对象缓存
List
基本数据类型,列表
在Redis里面可以把list作为栈、队列、阻塞队列
以l开头的都是List的操作命令
127.0.0.1:6379> flushall
OK
# lpush 头插法
127.0.0.1:6379> lpush list one
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lrang list 0 -1
(error) ERR unknown command `lrang`, with args beginning with: `list`, `0`, `-1`,
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrang list 0 1
(error) ERR unknown command `lrang`, with args beginning with: `list`, `0`, `1`,
127.0.0.1:6379> lrange list 0 1
1) "three"
2) "two"
127.0.0.1:6379> lrange list 0 0
1) "three"
# 尾插法,插入列表的最后
127.0.0.1:6379>rpush list four
# 移除使用lpop移除列表第一个元素,rpop移除列表的最后一个元素
127.0.0.1:6379> lpop list
"three"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "four"
127.0.0.1:6379> rpop list
"four"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379>
# 获取指定下标的元素
127.0.0.1:6379> lindex list 1
"one"
127.0.0.1:6379> lindex list 0
"two"
127.0.0.1:6379> lrem list 1 one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "two"
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "two"
# 删除指定值的元素
127.0.0.1:6379> lrem list 2 two
(integer) 2
127.0.0.1:6379> lrange list 0 -1
# 截取列表 ltrim 根据下标截取指定范围的列表
127.0.0.1:6379> rpush mylist "v1"
(integer) 1
127.0.0.1:6379> rpush mylist "v2"
(integer) 2
127.0.0.1:6379> rpush mylist "v3"
(integer) 3
127.0.0.1:6379> rpush mylist "v4"
(integer) 4
127.0.0.1:6379> lrang mylist
(error) ERR unknown command `lrang`, with args beginning with: `mylist`,
127.0.0.1:6379> lrange mylist 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> ltrim mylist 1 2
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "v2"
2) "v3"
小结
- List实际上是一个链表,before node,after node,left,right都可以插入值
- 如果key不存在,创建新的链表
- 如果key存在,新增内容
- 如果移除了所有值,空链表,也代表列表不存在
- 在两边插入或者改动值,效率最高,中间元素,相对来说效率较低。
- 可以用在消息队列、栈。
set(集合,值不能重复,无序)
命令以s开头
# 集合添加元素
127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "world"
(integer) 1
# 集合查看元素
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
# 判断元素是否在集合中
127.0.0.1:6379> sismember myset hello
(integer) 1
127.0.0.1:6379> sismember myset hello1
(integer) 0
# 查看集合大小
127.0.0.1:6379> scard myset
(integer) 2
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
# 移除元素
srem myset hello
127.0.0.1:6379> smembers myset
1) "world"
# 获取随机元素
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> srandmember myset
"hello"
127.0.0.1:6379> srandmember myset
"hello"
127.0.0.1:6379> srandmember myset
"world"
127.0.0.1:6379>
##############################################
# 随机删除
127.0.0.1:6379> spop myset
"world"
127.0.0.1:6379> smembers myset
1) "hello"
##################################################
# 集合操作
# 差集:sdiff key1 key2
# 交集:sinter key1 key2
# 并集: sunion key1 key2
127.0.0.1:6379> sadd myset2 hello
(integer) 1
127.0.0.1:6379> sadd myset2 world
(integer) 1
127.0.0.1:6379> smembers myset
1) "hello"
127.0.0.1:6379> smembers myset2
1) "world"
2) "hello"
127.0.0.1:6379> sdiff myset myset2
(empty array)
127.0.0.1:6379> sdiff myset2 myset1
1) "world"
2) "hello"
127.0.0.1:6379> sdiff myset2 myset2
(empty array)
127.0.0.1:6379> sdiff myset2 myset
1) "world"
127.0.0.1:6379> sinter myset myset2
1) "hello"
127.0.0.1:6379> sunion myset myset2
1) "world"
2) "hello"
Hash
Map集合,key-map是一个map集合,本质上和String类型没有太大的区别,还是异地的key-value。
以h开头的命令
127.0.0.1:6379> hset myhash f1 v1
(integer) 1
127.0.0.1:6379> hget myhash f1
"v1"
127.0.0.1:6379> hmset myhash f1 h1 f2 h2 f3 h3
OK
127.0.0.1:6379> hmget myhash f1 f2 f3
1) "h1"
2) "h2"
3) "h3"
127.0.0.1:6379>
# hgetall获取map集合中的所有值
127.0.0.1:6379> hgetall myhash
1) "f1"
2) "h1"
3) "f2"
4) "h2"
5) "f3"
6) "h3"
127.0.0.1:6379>
使用场景
hash变量适合存储对象,string更加适合存储字符串。
zset(有序集合)
在set的基础上,增加了一个值
127.0.0.1:6379> zadd myset 1 one
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three
(integer) 2
127.0.0.1:6379> zrange myset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379># 删除zset中的元素
127.0.0.1:6379> zrem myset one
0
############################################################
# 按照值排序
127.0.0.1:6379> zadd salary 2500 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 1000 xiaoming
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf # 按照最小到最大排序
1) "xiaoming"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZRANGEBYSCORE salary +inf -inf
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE salary 0 -1
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf
1) "xiaoming"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZRANGEBYSCORE salary 0 -1
(empty array)
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores
1) "xiaoming"
2) "1000"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 1000 withscores
1) "xiaoming"
2) "1000"
127.0.0.1:6379>
# 指定最小最大值排序
127.0.0.1:6379> ZRANGEBYSCORE salary 1000 5000
1) "xiaoming"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZRANGEBYSCORE salary 1000 5000 withscores
1) "xiaoming"
2) "1000"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379>
场景
set 排序,存储班级成绩、工资表排序。
带权重进行判断。
排行榜应用实现。
查看命令如何使用、数据类型相关的命令有哪些
- ? 查询help有哪些命令
- help command 查看命令如何使用
- help @group 查询数据类型相关的操作命令
27.0.0.1:6379> ?
redis-cli 6.0.9
To get help about Redis commands type:"help @<group>" to get a list of commands in <group>"help <command>" for help on <command>"help <tab>" to get a list of possible help topics"quit" to exitTo set redis-cli preferences:":set hints" enable online hints":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6379> help setSET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]summary: Set the string value of a keysince: 1.0.0group: string127.0.0.1:6379> help @stringAPPEND key valuesummary: Append a value to a keysince: 2.0.0BITCOUNT key [start end]summary: Count set bits in a stringsince: 2.6.0BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]summary: Perform arbitrary bitfield integer operations on stringssince: 3.2.0BITOP operation destkey key [key ...]summary: Perform bitwise operations between stringssince: 2.6.0BITPOS key bit [start] [end]summary: Find first bit set or clear in a stringsince: 2.8.7DECR keysummary: Decrement the integer value of a key by onesince: 1.0.0DECRBY key decrementsummary: Decrement the integer value of a key by the given numbersince: 1.0.0GET keysummary: Get the value of a keysince: 1.0.0GETBIT key offsetsummary: Returns the bit value at offset in the string value stored at keysince: 2.2.0GETRANGE key start endsummary: Get a substring of the string stored at a keysince: 2.4.0GETSET key valuesummary: Set the string value of a key and return its old valuesince: 1.0.0INCR keysummary: Increment the integer value of a key by onesince: 1.0.0INCRBY key incrementsummary: Increment the integer value of a key by the given amountsince: 1.0.0INCRBYFLOAT key incrementsummary: Increment the float value of a key by the given amountsince: 2.6.0MGET key [key ...]summary: Get the values of all the given keyssince: 1.0.0MSET key value [key value ...]summary: Set multiple keys to multiple valuessince: 1.0.1MSETNX key value [key value ...]summary: Set multiple keys to multiple values, only if none of the keys existsince: 1.0.1PSETEX key milliseconds valuesummary: Set the value and expiration in milliseconds of a keysince: 2.6.0SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]summary: Set the string value of a keysince: 1.0.0SETBIT key offset valuesummary: Sets or clears the bit at offset in the string value stored at keysince: 2.2.0SETEX key seconds valuesummary: Set the value and expiration of a keysince: 2.0.0SETNX key valuesummary: Set the value of a key, only if the key does not existsince: 1.0.0SETRANGE key offset valuesummary: Overwrite part of a string at key starting at the specified offsetsince: 2.2.0STRALGO LCS algo-specific-argument [algo-specific-argument ...]summary: Run algorithms (currently LCS) against stringssince: 6.0.0STRLEN keysummary: Get the length of the value stored in a keysince: 2.2.0127.0.0.1:6379> help @hashHDEL key field [field ...]summary: Delete one or more hash fieldssince: 2.0.0HEXISTS key fieldsummary: Determine if a hash field existssince: 2.0.0HGET key fieldsummary: Get the value of a hash fieldsince: 2.0.0HGETALL keysummary: Get all the fields and values in a hashsince: 2.0.0HINCRBY key field incrementsummary: Increment the integer value of a hash field by the given numbersince: 2.0.0HINCRBYFLOAT key field incrementsummary: Increment the float value of a hash field by the given amountsince: 2.6.0HKEYS keysummary: Get all the fields in a hashsince: 2.0.0HLEN keysummary: Get the number of fields in a hashsince: 2.0.0HMGET key field [field ...]summary: Get the values of all the given hash fieldssince: 2.0.0HMSET key field value [field value ...]summary: Set multiple hash fields to multiple valuessince: 2.0.0HSCAN key cursor [MATCH pattern] [COUNT count]summary: Incrementally iterate hash fields and associated valuessince: 2.8.0HSET key field value [field value ...]summary: Set the string value of a hash fieldsince: 2.0.0HSETNX key field valuesummary: Set the value of a hash field, only if the field does not existsince: 2.0.0HSTRLEN key fieldsummary: Get the length of the value of a hash fieldsince: 3.2.0HVALS keysummary: Get all the values in a hashsince: 2.0.0
三种特殊数据类型
geospatial地理位置
朋友的定位,附件的人,打车距离计算等
hyperloglog
基数统计
bitmaps
按位存储
统计用户信息。
Bitmaps位图,数据结构!都是操作二进制位来进行记录,就只有0和1两个状态。
365天=365 bit
签到打卡场景。
数据类型(五大基本类型,3种特殊类型)相关推荐
- java单精度实型_Java的八种基本类型及其各种数据类型的相互转换
一.八种基本类型 1.六种数字类型(四个整数型,两个浮点型) 字节型byte 8位-2^7到2^7短整型short 16位 整型int 32位 长整型long 64位 单精度float 32位 ...
- Gridview数据控件的七种字段类型
9.8 数据控件的七种字段类型(Fields Type)的应用 GridView共支持七种字段类型,字段原本应该叫"Column"比较恰当,但ASP.NET 2.0却采用另一个名 ...
- redis常用的五大数据类型和redis新增类型以及对应的命令
常用五大数据数据类型 1.1 String 类型 String类型是一个key对应一个value. String类型是二进制安全的,也就是Redis的string可以包含任务数据.比如jpg图片或者序 ...
- MySQL 学习笔记(12)— 数据类型(定长字符、变长字符、字符串大对象、数字类型、日期时间类型、二进制类型)
MySQL 常见的数据类型有字符串类型.数字类型.时间类型.二进制类型.具体的分类如下图: 1. 字符串类型 字符串类型用于存储字符和字符串数据,主要包含三种具体的类型:定长字符串.变长字符串以及字符 ...
- python中内置的四种数值类型为_浅谈python语言四种数值类型
Python语言支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下.希望对 ...
- PHP标量类型中整型类型的,PHP数据类型概述
在PHP中,有8种基本数据类型和一些伪类型,其中基本数据类型又分为标量类型.复合类型和特殊类型.相对C#,类型少了不少,但同样可实现很多功能,也不比其它语言差. 一.基本类型 1.标量类型(4种) 1 ...
- 64位 java 数据类型_全面解析Java支持的数据类型及Java的常量和变量类型
基本数据类型变量就是用来储存值而保留的内存位置.这就意味着当你创建一个变量时就会在内存中占用一定的空间. 基于变量的数据类型,操作系统会进行内存分配并且决定什么将被储存在保留内存中.因此,通过给变量分 ...
- redis的五种存储类型的具体用法
一.String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 $redis-&g ...
- python是一种解释类型的编程语言-Python入门你要懂哪些?这篇文章总算讲清楚了...
原标题:Python入门你要懂哪些?这篇文章总算讲清楚了 作者 | 小土豆Yuki 来源 | 洁癖是一只狗(ID: rookie-dog) 从今天开始学习Python,今后会不定期更新Python的相 ...
最新文章
- 自动化工具之二:win32gui
- 算法题26 复杂链表的复制
- 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言—— 1061:求整数的和与均值
- MQTT(2)---MQTT协议
- 标准单元测试步骤:A -B-C-D-E-F
- “MapReduce: Simplified Data Processing on Large Clusters”
- pp助手苹果版本_最全的苹果越狱源,收藏备用
- 智慧小区智能安防设计方案
- 电脑无线网络(WiFi)密码查看(cmd命令控制符)
- 操作系统(02326)自考学习笔记/备考资料
- VEMD11940FX01光学传感器
- 苹果邮件怎么添加qq邮箱_QQ邮箱格式怎么写?这有一份详细教程
- ESP32定时器睡眠模式
- 不仅可以邮件撤回还能误删恢复?!
- 在物理机上安装虚拟机
- List.isEmpty()与CollectionUtils.isEmpty的区别
- 虚拟机安装windows10
- Calendar类常用方法 日期间的转换 set方法有巨坑
- C# LINQ TO SQL
- Predicting Buffer Overflow Vulnerabilities through Mining Light-Weight Static Code Attributes