国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Database Mysql Tutorial redis學習筆記7(key操作)

redis學習筆記7(key操作)

Jun 07, 2016 pm 03:21 PM
key redis study operate notes

redis學習筆記7(key操作) 一、概述: 在該系列的前幾篇博客中,主要講述的是與Redis數據類型相關的命令,如String、List、Set、Hashes和Sorted-Set。這些命令都具有一個共同點,即所有的操作都是針對與Key關聯的Value的。而該篇博客將主要講述與Key相關的R

redis學習筆記7(key操作)

?

一、概述:

?

? ? ? 在該系列的前幾篇博客中,主要講述的是與Redis數據類型相關的命令,如String、List、Set、Hashes和Sorted-Set。這些命令都具有一個共同點,即所有的操作都是針對與Key關聯的Value的。而該篇博客將主要講述與Key相關的Redis命令。學習這些命令對于學習Redis是非常重要的基礎,也是能夠充分挖掘Redis潛力的利器。

? ? ? 在該篇博客中,我們將一如既往的給出所有相關命令的明細列表和典型示例,以便于我們現在的學習和今后的查閱。

?

二、相關命令列表:

命令原型 時間復雜度 命令描述 返回值

KEYS pattern O(N) 時間復雜度中的N表示數據庫中Key的數量。獲取所有匹配pattern參數的Keys。需要說明的是,在我們的正常操作中應該盡量避免對該命令的調用,因為對于大型數據庫而言,該命令是非常耗時的,對Redis服務器的性能打擊也是比較大的。pattern支持glob-style的通配符格式,如*表示任意一個或多個字符,?表示任意字符,[abc]表示方括號中任意一個字母。 匹配模式的鍵列表。

DEL key [key ...] O(N) 時間復雜度中的N表示刪除的Key數量。從數據庫刪除中參數中指定的keys,如果指定鍵不存在,則直接忽略。還需要另行指出的是,如果指定的Key關聯的數據類型不是String類型,而是List、Set、Hashes和Sorted Set等容器類型,該命令刪除每個鍵的時間復雜度為O(M),其中M表示容器中元素的數量。而對于String類型的Key,其時間復雜度為O(1)。 實際被刪除的Key數量。

EXISTS key O(1) 判斷指定鍵是否存在。 1表示存在,0表示不存在。

MOVE key db O(1) 將當前數據庫中指定的鍵Key移動到參數中指定的數據庫中。如果該Key在目標數據庫中已經存在,或者在當前數據庫中并不存在,該命令將不做任何操作并返回0。 ? 移動成功返回1,否則0。

RENAME key newkey O(1) 為指定指定的鍵重新命名,如果參數中的兩個Keys的命令相同,或者是源Key不存在,該命令都會返回相關的錯誤信息。如果newKey已經存在,則直接覆蓋。

RENAMENX key newkey O(1) 如果新值不存在,則將參數中的原值修改為新值。其它條件和RENAME一致。 1表示修改成功,否則0。

PERSIST key O(1) 如果Key存在過期時間,該命令會將其過期時間消除,使該Key不再有超時,而是可以持久化存儲。 1表示Key的過期時間被移出,0表示該Key不存在或沒有過期時間。

EXPIRE key seconds O(1) 該命令為參數中指定的Key設定超時的秒數,在超過該時間后,Key被自動的刪除。如果該Key在超時之前被修改,與該鍵關聯的超時將被移除。 1表示超時被設置,0則表示Key不存在,或不能被設置。

EXPIREAT key timestamp O(1) 該命令的邏輯功能和EXPIRE完全相同,唯一的差別是該命令指定的超時時間是絕對時間,而不是相對時間。該時間參數是Unix timestamp格式的,即從1970年1月1日開始所流經的秒數。 1表示超時被設置,0則表示Key不存在,或不能被設置。?

TTL key O(1) 獲取該鍵所剩的超時描述。 返回所剩描述,如果該鍵不存在或沒有超時設置,則返回-1。

RANDOMKEY O(1) ? 從當前打開的數據庫中隨機的返回一個Key。 返回的隨機鍵,如果該數據庫是空的則返回nil。

TYPE key O(1) 獲取與參數中指定鍵關聯值的類型,該命令將以字符串的格式返回。 返回的字符串為string、list、set、hash和zset,如果key不存在返回none。

SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination] O(N+M*log(M)) 這個命令相對來說是比較復雜的,因此我們這里只是給出最基本的用法,有興趣的網友可以去參考redis的官方文檔。 返回排序后的原始列表。

?

三、命令示例:

?

? ?1. KEYS/RENAME/DEL/EXISTS/MOVE/RENAMENX:

? ? #在Shell命令行下啟動Redis客戶端工具。

? ? /> redis-cli

? ? #清空當前選擇的數據庫,以便于對后面示例的理解。

? ? redis 127.0.0.1:6379> flushdb

? ? OK

? ? #添加String類型的模擬數據。

? ? redis 127.0.0.1:6379> set mykey 2

? ? OK

? ? redis 127.0.0.1:6379> set mykey2 "hello"

? ? OK

? ? #添加Set類型的模擬數據。

? ? redis 127.0.0.1:6379> sadd mysetkey 1 2 3

? ? (integer) 3

? ? #添加Hash類型的模擬數據。

? ? redis 127.0.0.1:6379> hset mmtest username "stephen"

? ? (integer) 1

? ? #根據參數中的模式,獲取當前數據庫中符合該模式的所有key,從輸出可以看出,該命令在執(zhí)行時并不區(qū)分與Key關聯的Value類型。

? ? redis 127.0.0.1:6379> keys my*

? ? 1) "mysetkey"

? ? 2) "mykey"

? ? 3) "mykey2"

? ? #刪除了兩個Keys。

? ? redis 127.0.0.1:6379> del mykey mykey2

? ? (integer) 2

? ? #查看一下剛剛刪除的Key是否還存在,從返回結果看,mykey確實已經刪除了。

? ? redis 127.0.0.1:6379> exists mykey

? ? (integer) 0

? ? #查看一下沒有刪除的Key,以和上面的命令結果進行比較。

? ? redis 127.0.0.1:6379> exists mysetkey

? ? (integer) 1

? ? #將當前數據庫中的mysetkey鍵移入到ID為1的數據庫中,從結果可以看出已經移動成功。

? ? redis 127.0.0.1:6379> move mysetkey 1

? ? (integer) 1

? ? #打開ID為1的數據庫。

? ? redis 127.0.0.1:6379> select 1

? ? OK

? ? #查看一下剛剛移動過來的Key是否存在,從返回結果看已經存在了。

? ? redis 127.0.0.1:6379[1]> exists mysetkey

? ? (integer) 1

? ? #在重新打開ID為0的缺省數據庫。

? ? redis 127.0.0.1:6379[1]> select 0

? ? OK

? ? #查看一下剛剛移走的Key是否已經不存在,從返回結果看已經移走。

? ? redis 127.0.0.1:6379> exists mysetkey

? ? (integer) 0

? ? #準備新的測試數據。 ? ?

? ? redis 127.0.0.1:6379> set mykey "hello"

? ? OK

? ? #將mykey改名為mykey1

? ? redis 127.0.0.1:6379> rename mykey mykey1

? ? OK

? ? #由于mykey已經被重新命名,再次獲取將返回nil。

? ? redis 127.0.0.1:6379> get mykey

? ? (nil)

? ? #通過新的鍵名獲取。

? ? redis 127.0.0.1:6379> get mykey1

? ? "hello"

? ? #由于mykey已經不存在了,所以返回錯誤信息。

? ? redis 127.0.0.1:6379> rename mykey mykey1

? ? (error) ERR no such key

? ? #為renamenx準備測試key

? ? redis 127.0.0.1:6379> set oldkey "hello"

? ? OK

? ? redis 127.0.0.1:6379> set newkey "world"

? ? OK

? ? #由于newkey已經存在,因此該命令未能成功執(zhí)行。

? ? redis 127.0.0.1:6379> renamenx oldkey newkey

? ? (integer) 0

? ? #查看newkey的值,發(fā)現它也沒有被renamenx覆蓋。

? ? redis 127.0.0.1:6379> get newkey

? ? "world"

? ? ? ??

? ?2. PERSIST/EXPIRE/EXPIREAT/TTL: ? ?

? ? #為后面的示例準備的測試數據。

? ? redis 127.0.0.1:6379> set mykey "hello"

? ? OK

? ? #將該鍵的超時設置為100秒。

? ? redis 127.0.0.1:6379> expire mykey 100

? ? (integer) 1

? ? #通過ttl命令查看一下還剩下多少秒。

? ? redis 127.0.0.1:6379> ttl mykey

? ? (integer) 97

? ? #立刻執(zhí)行persist命令,該存在超時的鍵變成持久化的鍵,即將該Key的超時去掉。

? ? redis 127.0.0.1:6379> persist mykey

? ? (integer) 1

? ? #ttl的返回值告訴我們,該鍵已經沒有超時了。

? ? redis 127.0.0.1:6379> ttl mykey

? ? (integer) -1

? ? #為后面的expire命令準備數據。

? ? redis 127.0.0.1:6379> del mykey

? ? (integer) 1

? ? redis 127.0.0.1:6379> set mykey "hello"

? ? OK

? ? #設置該鍵的超時被100秒。

? ? redis 127.0.0.1:6379> expire mykey 100

? ? (integer) 1

? ? #用ttl命令看一下當前還剩下多少秒,從結果中可以看出還剩下96秒。

? ? redis 127.0.0.1:6379> ttl mykey

? ? (integer) 96

? ? #重新更新該鍵的超時時間為20秒,從返回值可以看出該命令執(zhí)行成功。

? ? redis 127.0.0.1:6379> expire mykey 20

? ? (integer) 1

? ? #再用ttl確認一下,從結果中可以看出果然被更新了。

? ? redis 127.0.0.1:6379> ttl mykey

? ? (integer) 17

? ? #立刻更新該鍵的值,以使其超時無效。

? ? redis 127.0.0.1:6379> set mykey "world"

? ? OK

? ? #從ttl的結果可以看出,在上一條修改該鍵的命令執(zhí)行后,該鍵的超時也無效了。

? ? redis 127.0.0.1:6379> ttl mykey

? ? (integer) -1

?

? ?3. TYPE/RANDOMKEY/SORT:

? ? #由于mm鍵在數據庫中不存在,因此該命令返回none。

? ? redis 127.0.0.1:6379> type mm

? ? none

? ? #mykey的值是字符串類型,因此返回string。

? ? redis 127.0.0.1:6379> type mykey

? ? string

? ? #準備一個值是set類型的鍵。

? ? redis 127.0.0.1:6379> sadd mysetkey 1 2

? ? (integer) 2

? ? #mysetkey的鍵是set,因此返回字符串set。

? ? redis 127.0.0.1:6379> type mysetkey

? ? set

? ? #返回數據庫中的任意鍵。

? ? redis 127.0.0.1:6379> randomkey

? ? "oldkey"

? ? #清空當前打開的數據庫。

? ? redis 127.0.0.1:6379> flushdb

? ? OK

? ? #由于沒有數據了,因此返回nil。

? ? redis 127.0.0.1:6379> randomkey

? ? (nil)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel8 optimization points Laravel8 optimization points Apr 18, 2025 pm 12:24 PM

Laravel 8 provides the following options for performance optimization: Cache configuration: Use Redis to cache drivers, cache facades, cache views, and page snippets. Database optimization: establish indexing, use query scope, and use Eloquent relationships. JavaScript and CSS optimization: Use version control, merge and shrink assets, use CDN. Code optimization: Use Composer installation package, use Laravel helper functions, and follow PSR standards. Monitoring and analysis: Use Laravel Scout, use Telescope, monitor application metrics.

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Recommended Laravel's best expansion packs: 2024 essential tools Recommended Laravel's best expansion packs: 2024 essential tools Apr 30, 2025 pm 02:18 PM

The essential Laravel extension packages for 2024 include: 1. LaravelDebugbar, used to monitor and debug code; 2. LaravelTelescope, providing detailed application monitoring; 3. LaravelHorizon, managing Redis queue tasks. These expansion packs can improve development efficiency and application performance.

Laravel environment construction and basic configuration (Windows/Mac/Linux) Laravel environment construction and basic configuration (Windows/Mac/Linux) Apr 30, 2025 pm 02:27 PM

The steps to build a Laravel environment on different operating systems are as follows: 1.Windows: Use XAMPP to install PHP and Composer, configure environment variables, and install Laravel. 2.Mac: Use Homebrew to install PHP and Composer and install Laravel. 3.Linux: Use Ubuntu to update the system, install PHP and Composer, and install Laravel. The specific commands and paths of each system are different, but the core steps are consistent to ensure the smooth construction of the Laravel development environment.

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

How to configure slow query log in centos redis How to configure slow query log in centos redis Apr 14, 2025 pm 04:54 PM

Enable Redis slow query logs on CentOS system to improve performance diagnostic efficiency. The following steps will guide you through the configuration: Step 1: Locate and edit the Redis configuration file First, find the Redis configuration file, usually located in /etc/redis/redis.conf. Open the configuration file with the following command: sudovi/etc/redis/redis.conf Step 2: Adjust the slow query log parameters in the configuration file, find and modify the following parameters: #slow query threshold (ms)slowlog-log-slower-than10000#Maximum number of entries for slow query log slowlog-max-len

In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? Apr 19, 2025 pm 10:57 PM

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

See all articles