Get to know Redis

Xihong Mainland 2022-09-23 08:05:00 阅读数:376

knowredis

摘要:Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API.Redis 通常被称为数据结构服务器,因为值(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型.Because of its powerful performance,高并发,Big data capabilities are well received by developers,Get a closer look belowRedis的简单操作!

环境:redis版本7.0.3

一、连接redis

redis-cli -h 127.0.0.1 -p 6379 -a {
输入你的密码}

二、基本操作命令
1、列出所有的key

127.0.0.1:6379[1]> keys *
1) "63295ba13b1b1413508180:standard_ref"
2) "tag:page-list:key"
3) "74e5248aec8639a5794ec0a1956a8460aaa57313:page_list_data-84263f9e0dc9c359169e8465deabee89"

2、匹配key

127.0.0.1:6379[1]> keys 74e5248aec8639a5794ec0a1956a8460aaa57313*
1) "74e5248aec8639a5794ec0a1956a8460aaa57313:page_list_data-84263f9e0dc9c359169e8465deabee89"

3、判断一个key是否存在

127.0.0.1:6379[1]> EXISTS 74e5248aec8639a5794ec0a1956a8460aaa57313:page_list_data-84263f9e0dc9c359169e8465deabee89
(integer) 1

4、设置一个key的过期秒数(秒)

127.0.0.1:6379[1]> EXPIRE 74e5248aec8639a5794ec0a1956a8460aaa57313:page_list_data-84263f9e0dc9c359169e8465deabee89 2000
(integer) 1

5、获取key的有效时间(秒),如果key不存在,返回 -2

127.0.0.1:6379[1]> TTL 74e5248aec8639a5794ec0a1956a8460aaa57313:page_list_data-84263f9e0dc9c359169e8465deabee89
(integer) 1984

6、清除当前库

flushdb

7、清除全部库(慎重操作)

flushall

三、redis的常用数据类型
1、String类型:
Redis Strings store sequences of bytes,包括文本、序列化对象和二进制数组.因此,字符串是最基本的 Redis 数据类型.They are usually used for caching,But they support extra functionality,Lets you also implement counters and perform bitwise operations.

2、List列表:
Redis 列表是字符串值的链表.Redis Lists are often used:
实现堆栈和队列.
Build queue management for background work systems.

3、Set
Redis无序集合,Track Unique Items(例如,Tracks all unique visits to a given blog post IP 地址).
表示关系(例如,A collection of all users with the given role).
Perform common set operations,例如交集、并集和差集.

4、Hash
Redis A hash is a record type structured as a collection of field-value pairs. 是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象.

5、Sort Set
Redis有序集合,Redis Sorted sets are unique strings ordered by their associated scores(成员)的集合.When multiple strings have the same score,The strings are in lexicographical order.Some use cases for sorted sets include:
排行榜.例如,You can use sorted sets to easily maintain ordered lists of the highest scores in large online games.
Weight sorting, etc.

注:Redis默认有16个库.

如果对你有帮助,看完别忘了转发,收藏哦~~

copyright:author[Xihong Mainland],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209230724117271.html