redis命令操作之generic和string.java

简介: redis是键值对的内存数据库,属于Nosql范畴 登录redis自带的客户端 #redis-cli 127.0.0.1:6379> 1.设置redis 键值对 > set foo bar  #设置键foo的值为"bar" OK #设置成功 2.获得redis 键值对 > get foo #根据键获得值 "bar" #返回的结果 3.删除键值对  1
redis是键值对的内存数据库,属于Nosql范畴
登录redis自带的客户端
#redis-cli
127.0.0.1:6379>
1.设置redis 键值对
> set foo bar  #设置键foo的值为"bar"
OK #设置成功


2.获得redis 键值对
> get foo #根据键获得值
"bar" #返回的结果


3.删除键值对 
127.0.0.1:6379> del foo foo2 foo3 #删除键值对,可删除多个
(integer) 3 #删除成功的键值对的个数


4.返回空
127.0.0.1:6379> get foo #如果键值对不存在,返回nil
(nil)


5.判断键值对是否存在,返回0:不存在,返回1:存在;只能查询一个键。


127.0.0.1:6379> EXISTS foo
(integer) 0
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> EXISTS foo
(integer) 1
127.0.0.1:6379> EXISTS foo foo2 foo3
(error) ERR wrong number of arguments for 'exists' command


6.设置键值对有效期
127.0.0.1:6379> EXPIRE foo 32 #设置foo键有效期为32秒
(integer) 1


7.根据键的表达式,查找符合表达式的键
127.0.0.1:6379> keys foo*
1) "foo3"
2) "foo2"
3) "foo"


8.自动将一个key从一个redis实例传输到另一个
MIGRATE host port key destination-db timeout
summary: Atomically transfer a key from a Redis instance to another one.
since: 2.6.0


9.将一个key移到另一个数据库
MOVE key db
summary: Move a key to another database
since: 1.0.0


10.检查内部的redis对象
OBJECT subcommand [arguments [arguments ...]]
summary: Inspect the internals of Redis objects
since: 2.2.3


11.从key中移除过期时间
PERSIST key
summary: Remove the expiration from a key
since: 2.2.0


127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> EXPIRE foo 32
(integer) 1
127.0.0.1:6379> PERSIST foo
(integer) 1


12.获取键值对的存活时间(微秒)
PTTL key
summary: Get the time to live for a key in milliseconds
since: 2.6.0


127.0.0.1:6379> EXPIRE foo 32
(integer) 1
127.0.0.1:6379> pttl foo
(integer) 27944
13.获取键值对存活时间(秒)
TTL key
summary: Get the time to live for a key
since: 1.0.0




13.设置微秒级别的键值对存活时间
PEXPIRE key milliseconds
summary: Set a key's time to live in milliseconds
since: 2.6.0


14.设置微秒级别的unix形式的时间,键值对存活时间
PEXPIREAT key milliseconds-timestamp
summary: Set the expiration for a key as a UNIX timestamp specified in milliseconds
since: 2.6.0


15.随机获取一个键
RANDOMKEY -
summary: Return a random key from the keyspace
since: 1.0.0


127.0.0.1:6379> RANDOMKEY
"foo2"
16.重命名键
RENAME key newkey
summary: Rename a key
since: 1.0.0


127.0.0.1:6379> rename foo12 foo13
OK


17.重命名键,新键不存在时
RENAMENX key newkey
summary: Rename a key, only if the new key does not exist
since: 1.0.0


127.0.0.1:6379> RENAMENX foo13 foo3 #foo3已经存在执行不成功
(integer) 0


18.备份键值对
DUMP key
summary: Return a serialized version of the value stored at the specified key.
since: 2.6.0
127.0.0.1:6379> dump foo
"\x00\x03bar\x06\x00pS!\xe0\x1b3\xc1\x84"


19.还原键值对
RESTORE key ttl serialized-value
summary: Create a key using the provided serialized value, previously obtained using DUMP.
since: 2.6.0


20.获取键的类型
TYPE key
summary: Determine the type stored at key
since: 1.0.0
127.0.0.1:6379> type foo3
string


21.按键排序
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
summary: Sort the elements in a list, set or sorted set
since: 1.0.0


/****** string类型的命令操作 ********/
1.将值添加到键的值得后面
APPEND key value
summary: Append a value to a key
since: 2.0.0
127.0.0.1:6379> append foo3 barbar
(integer) 10
127.0.0.1:6379> get foo3
"bar3barbar"


2.
BITCOUNT key [start] [end]
summary: Count set bits in a string
since: 2.6.0
127.0.0.1:6379> bitcount foo3
(integer) 34
127.0.0.1:6379> bitcount foo13
(integer) 16


3."不懂"
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0


4.将值减小1(键对应的值必须为int)
DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0


127.0.0.1:6379> set num1 13
OK
127.0.0.1:6379> get num1
"13"
127.0.0.1:6379> decr num1
(integer) 12
127.0.0.1:6379> get num1
"12"


5.将值减小指定大小(键对应的值必须为int)
DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0
127.0.0.1:6379> get num1
"12"
127.0.0.1:6379> DECRBY num1 6
(integer) 6
127.0.0.1:6379> get num1
"6"


6.为键重新复制,并返回之前的旧值
GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0


7. 
GETBIT key offset
summary: Returns the bit value at offset in the string value stored at key
since: 2.2.0


8.
GETRANGE key start end
summary: Get a substring of the string stored at a key
since: 2.4.0


9.为key设置string值,并返回原先的值
GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0


127.0.0.1:6379> get foo13
"bar12nametest"
127.0.0.1:6379> getset foo13 test
"bar12nametest"


10. 将value类型为int的值加1
INCR key
summary: Increment the integer value of a key by one
since: 1.0.0
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> get num
"10"
127.0.0.1:6379> INCR num
(integer) 11


11. 将value类型为int的值加指定的数
INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0
127.0.0.1:6379> get num
"11"
127.0.0.1:6379> INCRBY num 10
(integer) 21
127.0.0.1:6379> get num
"21"


12.将value类型为float的值加上指定的数
INCRBYFLOAT key increment
summary: Increment the float value of a key by the given amount
since: 2.6.0


127.0.0.1:6379> set flo 11.2
OK
127.0.0.1:6379> get flo
"11.2"
127.0.0.1:6379> INCRBYFLOAT flo 12.0
"23.2"


13.获取多个key的value
MGET key [key ...]
summary: Get the values of all the given keys
since: 1.0.0
127.0.0.1:6379> MGET foo2 foo3 foo4
1) "bar2"
2) "bar3barbar"
3) (nil)




14.设置多个key的value
MSET key value [key value ...]
summary: Set multiple keys to multiple values
since: 1.0.1
127.0.0.1:6379> MSET foo2 bar22 foo3 bar33 foo4 bar44
OK
127.0.0.1:6379> MGET foo2 foo3 foo4
1) "bar22"
2) "bar33"
3) "bar44"


15.当所有的key都不存在时,设置value
MSETNX key value [key value ...]
summary: Set multiple keys to multiple values, only if none of the keys exist
since: 1.0.1
127.0.0.1:6379> MSETNX foo2 bar23 foo5 bar55 ##因为foo2键已经存在
(integer) 0


16.设置键的微秒生存时间和值
PSETEX key milliseconds value
summary: Set the value and expiration in milliseconds of a key
since: 2.6.0


17.设置键的秒生存时间和值
SETEX key seconds value
summary: Set the value and expiration of a key
since: 2.0.0




18.设置值
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0


19.
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0


20.在键不存在时,设置值
SETNX key value
summary: Set the value of a key, only if the key does not exist
since: 1.0.0


21.从offset位置写入新的值内容
SETRANGE key offset value
summary: Overwrite part of a string at key starting at the specified offset
since: 2.2.0
127.0.0.1:6379> set foo5 jing
OK
127.0.0.1:6379> get foo5
"jing"
127.0.0.1:6379> SETRANGE foo5 3 test
(integer) 7
127.0.0.1:6379> get foo5
"jintest"


22.获取值得长度
STRLEN key
summary: Get the length of the value stored in a key
since: 2.2.0
127.0.0.1:6379> STRLEN foo5
(integer) 4































相关文章
|
8月前
|
存储 缓存 监控
Redis设计与实现——Redis命令参考与高级特性
Redis 是一个高性能的键值存储系统,支持丰富的数据类型(字符串、列表、哈希、集合等)和多种高级功能。本文档涵盖 Redis 的核心命令分类,包括数据类型操作、事务与脚本、持久化、集群管理、系统监控等。特别介绍了事务的原子性特性、Lua 脚本的执行方式及优势、排序机制、发布订阅模型以及慢查询日志和监视器工具的使用方法。适用于开发者快速掌握 Redis 常用命令及其应用场景,优化系统性能与可靠性。
|
4月前
|
存储 SQL NoSQL
Redis-常用语法以及java互联实践案例
本文详细介绍了Redis的数据结构、常用命令及其Java客户端的使用,涵盖String、Hash、List、Set、SortedSet等数据类型及操作,同时提供了Jedis和Spring Boot Data Redis的实战示例,帮助开发者快速掌握Redis在实际项目中的应用。
339 1
Redis-常用语法以及java互联实践案例
|
4月前
|
存储 缓存 NoSQL
Redis基础命令与数据结构概览
Redis是一个功能强大的键值存储系统,提供了丰富的数据结构以及相应的操作命令来满足现代应用程序对于高速读写和灵活数据处理的需求。通过掌握这些基础命令,开发者能够高效地对Redis进行操作,实现数据存储和管理的高性能方案。
147 12
|
4月前
|
存储 消息中间件 NoSQL
【Redis】常用数据结构之List篇:从常用命令到典型使用场景
本文将系统探讨 Redis List 的核心特性、完整命令体系、底层存储实现以及典型实践场景,为读者构建从理论到应用的完整认知框架,助力开发者在实际业务中高效运用这一数据结构解决问题。
|
7月前
|
缓存 监控 NoSQL
Redis 实操要点:Java 最新技术栈的实战解析
本文介绍了基于Spring Boot 3、Redis 7和Lettuce客户端的Redis高级应用实践。内容包括:1)现代Java项目集成Redis的配置方法;2)使用Redisson实现分布式可重入锁与公平锁;3)缓存模式解决方案,包括布隆过滤器防穿透和随机过期时间防雪崩;4)Redis数据结构的高级应用,如HyperLogLog统计UV和GeoHash处理地理位置。文章提供了详细的代码示例,涵盖Redis在分布式系统中的核心应用场景,特别适合需要处理高并发、分布式锁等问题的开发场景。
500 41
|
5月前
|
存储 缓存 人工智能
Redis六大常见命令详解:从set/get到过期策略的全方位解析
本文将通过结构化学习路径,帮助读者实现从命令语法掌握到工程化实践落地的能力跃迁,系统性提升 Redis 技术栈的应用水平。
|
6月前
|
NoSQL Redis
Lua脚本协助Redis分布式锁实现命令的原子性
利用Lua脚本确保Redis操作的原子性是分布式锁安全性的关键所在,可以大幅减少由于网络分区、客户端故障等导致的锁无法正确释放的情况,从而在分布式系统中保证数据操作的安全性和一致性。在将这些概念应用于生产环境前,建议深入理解Redis事务与Lua脚本的工作原理以及分布式锁的可能问题和解决方案。
249 8
|
7月前
|
缓存 NoSQL Java
Java Redis 面试题集锦 常见高频面试题目及解析
本文总结了Redis在Java中的核心面试题,包括数据类型操作、单线程高性能原理、键过期策略及分布式锁实现等关键内容。通过Jedis代码示例展示了String、List等数据类型的操作方法,讲解了惰性删除和定期删除相结合的过期策略,并提供了Spring Boot配置Redis过期时间的方案。文章还探讨了缓存穿透、雪崩等问题解决方案,以及基于Redis的分布式锁实现,帮助开发者全面掌握Redis在Java应用中的实践要点。
417 6
|
8月前
|
存储 缓存 NoSQL
Redis中的常用命令-get&set&keys&exists&expire&ttl&type的详细解析
总的来说,这些Redis命令提供了处理存储在内存中的键值对的便捷方式。通过理解和运用它们,你可以更有效地在Redis中操作数据,使其更好地服务于你的应用。
512 17
|
8月前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
944 16