redis入门——散列类型(hash)
来源:互联网 发布:网络视讯赌博真假 编辑:程序博客网 时间:2024/05/19 09:50
1.介绍 什么是散列类型
redis是采用字典结构以键值对形式存储数据的,而散列类型的键值也是一种字典结构,其存储类字段(field)和字段值的映射,但是字段值只能是字符串,不支持其他类型,也就是说不能嵌套其他类型
我们先来看下redis中,散列类型的存储结构(图我是拿其他博主的)
散列结构非常适合存储对象:使用对象类别和ID构成键名,使用字段表示对象的属性,字段表示存储的属性值。
和关系型数据库相比,这种存储结构更加有利于拓展,想想,如果为了关系型数据库中的一个元组添加属性,就得创建一个新的列,这是非常没有必要的
2.操作命令
1.赋值与取值
127.0.0.1:6379> HSET car1 price 90(integer) 1
127.0.0.1:6379> HGET car1 price"90"赋多个值有两种方式 如下两种,亲测,但是貌似网上找不到文章提到这一点的,我用的版本是4.0.2
127.0.0.1:6379> HSET car1 name 奥迪 color 白色(integer) 2
127.0.0.1:6379> HMSET car1 h 1 w 1OK
127.0.0.1:6379> HMGET car1 name color price1) "\xe5\xa5\xa5\xe8\xbf\xaa"2) "\xe7\x99\xbd\xe8\x89\xb2"3) "90"
127.0.0.1:6379> HGETALL car11) "price"2) "90"3) "name"4) "\xe5\xa5\xa5\xe8\xbf\xaa"5) "color"6) "\xe7\x99\xbd\xe8\x89\xb2"HSET命令不区分插入和更新操作,如果执行插入操作会返回1,更新操作返回0,键不存在时会自动建立它
127.0.0.1:6379> HGET car1 price"90"
127.0.0.1:6379> HSET car1 price 30
(integer) 0
127.0.0.1:6379> HGET car1 price"30"2.判断字段是否存在 存在返回1 不存在返回0
127.0.0.1:6379> HEXISTS car1 price(integer) 1
127.0.0.1:6379> HEXISTS car1 nonono(integer) 0
3.当字段不存在时赋值
127.0.0.1:6379> HGET car1 price"30"127.0.0.1:6379> HSETNX car1 price 15(integer) 0127.0.0.1:6379> HGET car1 price"30"
4.添加数字
127.0.0.1:6379> HGET car1 price"30"127.0.0.1:6379> HINCRBY car1 price 15(integer) 45127.0.0.1:6379> HGET car1 price"45"
散列类型中,没有HINCR命令,可任意通过将增加值设为1来实现
5.删除字段
127.0.0.1:6379> HDEL car1 name color (integer) 2可以删除一个或多个字段,返回值是被删除的字段个数
当删除的字段不存在时返回0
127.0.0.1:6379> HDEL car1 nonono(integer) 0
127.0.0.1:6379> HKEYS car11) "price"
127.0.0.1:6379> HVALS car11) "45"
8.获取字段数量
127.0.0.1:6379> HLEN car1(integer) 1
阅读全文
0 0
- redis入门——散列类型(hash)
- Redis学习笔记(四)——Redis常用命令入门——散列类型
- Redis入门(四)散列Hash
- Redis快速入门之散列类型
- Redis研究(四)—散列类型
- Redis-Redis实战(散列类型)
- redis散列类型
- redis基本结构(2)------散列(hash)
- redis数据类型(三)散列类型
- Redis从基础命令到实战之散列类型(Hash)
- Redis学习2--散列hash
- redis详解-(5)hash散列
- Redis学习-3.2:Hash散列详解
- 散列(hash)
- 散列(hash)
- redis之散列类型
- Redis简单入门三——对Hash操作
- Redis数据库(string和hash类型)
- spring boot(10)-tomcat jdbc连接池
- parquet压缩格式参数设置以及简单操作
- 1.编译openjdk1.8源码
- Android夜间模式的切换
- 线程池的理解及使用
- redis入门——散列类型(hash)
- spring boot(11)-druid监控
- WifiMonotor.java
- centos 找回密码
- 关于本博客
- spring boot(12)-数据源配置原理
- css3 transition 动画基础
- 计蒜客 Frequent Subsets Problem&&2017 Icpc南宁赛
- spring boot(13)-logback和access日志