当前位置:网站首页>Redis-字符串类型
Redis-字符串类型
2022-07-06 01:29:00 【冰点契约丶】
字符串类型:string
- String是Redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value;
- String类型是二进制安全的,意味着Redis的string可以包含任何数据。比如jpg图片或者序列化的对象
- String类型是Redis最基本的数据类型,一个Redis中字符串value最多可以是512M
常用命令
添加键值对
set <key> <value>
获取key对应的value
get <key>
将指定value追加到原value的末尾
append <key> <value>
获取value的长度
strlen <key>
只有当key不存在时对key进行添加
setnx <key> <value>
将 key 中储存的数字值增1,只能对数字值操作,如果为空,新增值为1
incr <key>
将 key 中储存的数字值减1,只能对数字值操作,如果为空,新增值为-1
decr <key>
将key中存储的数字值自定义步长增减
incrby <key> <step> decrby <key> <step>
原子操作的概念
- 所谓原子操作是指不会被线程调度机制打断的操作,这种操作一旦开始,就一直运行到结束,中间不会有任何 context switch (切换到另一个线程)
- 在单线程中, 能够在单条指令中完成的操作都可以认为是"原子操作",因为中断只能发生于指令之间
- 在多线程中,不能被其它进程(线程)打断的操作就叫原子操作
- Redis单命令的原子性主要得益于Redis的单线程
字符串批量操作命令
同时为多个key设置值
mset <key1> <value1> <key2> <value2> ...
同时获取多个value
mget <key1> <key2> ...
同时设置多个value,且key不存在(原子性:有一个存在则都失败)
msetnx <key1> <value1> <key2> <value2> ...
其他命令
获取子串,从索引start到stop(左闭右闭)
getrang <key> <start> <stop>
从指定索引位置使用value覆盖子串
setrang <key> <index> <value>
设置键值对的同时设置过期时间(单位:s)
setex <key> <time> <value>
以旧换新(读取原始值,并进行写入)
getset <key> <value>
字符串的数据结构
- String的数据结构为简单动态字符串(Simple Dynamic String,缩写SDS)。是可以修改的字符串,内部结构实现上类似于Java的ArrayList,采用预分配冗余空间的方式来减少内存的频繁分配
- 内部为当前字符串实际分配的空间capacity一般要高于实际字符串长度len。当字符串长度小于1M时,扩容都是加倍现有的空间,如果超过1M,扩容时一次只会多扩1M的空间。需要注意的是字符串最大长度为512M
边栏推荐
- CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
- ORA-00030
- module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
- Unity | 实现面部驱动的两种方式
- UE4 unreal engine, editor basic application, usage skills (IV)
- 干货!通过软硬件协同设计加速稀疏神经网络
- MySQL learning notes 2
- Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
- Force buckle 9 palindromes
- What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
猜你喜欢
Electrical data | IEEE118 (including wind and solar energy)
[detailed] several ways to quickly realize object mapping
Basic process and testing idea of interface automation
Basic operations of databases and tables ----- unique constraints
ORA-00030
WordPress collection plug-in automatically collects fake original free plug-ins
VMware Tools installation error: unable to automatically install vsock driver
[solved] how to generate a beautiful static document description page
电气数据|IEEE118(含风能太阳能)
Unity | two ways to realize facial drive
随机推荐
ClickOnce does not support request execution level 'requireAdministrator'
MySQL learning notes 2
Win10 add file extension
A picture to understand! Why did the school teach you coding but still not
C web page open WinForm exe
ThreeDPoseTracker项目解析
Tcpdump: monitor network traffic
Docker compose配置MySQL并实现远程连接
Three methods of script about login and cookies
servlet(1)
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
File upload vulnerability test based on DVWA
Superfluid_ HQ hacked analysis
Paddle framework: paddlenlp overview [propeller natural language processing development library]
VMware Tools安装报错:无法自动安装VSock驱动程序
Leetcode 208. Implement trie (prefix tree)
yii中console方法调用,yii console定时任务
电气数据|IEEE118(含风能太阳能)
【详细】快速实现对象映射的几种方式
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry