Table of Contents
安装
基本数据类型
String
Hashes
Lists
Set
Sorted Sets
其他
有效期
事务(Transactions)
杂项
在应用程序中使用Redis
Home Database Mysql Tutorial Redis使用笔记

Redis使用笔记

Jun 07, 2016 pm 04:36 PM
redis use database notes

Redis是一个键值型数据库,之前花了些时间体验了一下Redis,感觉还是很不错的。而且Redis学习起来也很容易,差不多花几个小时应该就能入门了。当时我是看的《the little redis book》,初学者可以看看。 安装 下载软件包: http://redis.io/download 首先运

Redis是一个键值型数据库,之前花了些时间体验了一下Redis,感觉还是很不错的。而且Redis学习起来也很容易,差不多花几个小时应该就能入门了。当时我是看的《the little redis book》,初学者可以看看。

安装

下载软件包: http://redis.io/download

首先运行服务器程序:

$ redis-server
Copy after login

程序默认是监听6379端口。

然后再运行客户端程序:

$ redis-cli
Copy after login

基本数据类型

运行客户端程序连接上Redis服务之后,所有的操作都可以通过该命令行交互完成。

Redis有5种不同的数据类型:字符串、散列表、列表、集合、有序集合。所有的数据在Redis中都是以键值对的形式保存的。

以下介绍这几种类型数据的基本操作。

String

设置一个键的值为字符串:

set  
Copy after login

获取该键的值:

get 
Copy after login

字符串相关的命令:

help @string
Copy after login

Hashes

设置一个散列值:

hset   
Copy after login

获取该键的值:

hget  
hgetall 
Copy after login

散列相关的命令:

help @hash
Copy after login

Lists

添加值:

lpush  
Copy after login

移除值:

lpop 
help @list
Copy after login

Set

添加值:

sadd  [  ..]
help set
Copy after login

Sorted Sets

类似于集合(Set),但是提供了排序(sorting)和秩划分(ranking)的功能。

添加值:

zadd key score member [score] [member]
help @sorted_set
Copy after login

其他

有效期

设置某个值在seconds秒后到期:

expire  seconds
Copy after login

设置某个值在time时刻到期:

expireat  time
Copy after login

查看到期时间:

ttl 
Copy after login

清除到期时间:

persist 
Copy after login

事务(Transactions)

multi
do something
exec
Copy after login

multi和exec之间的命令作为原子操作。

在multi之前调用watch可监听值的变化,若该值被其他客户端修改,事务将会运行失败。

杂项

选择数据库:

select index
Copy after login

清除当前数据库所属key:

flushdb
Copy after login

清除所有数据库所属key:

flushall
Copy after login

查看key所储存的值的类型:

type key
Copy after login

Redis相关的命令手册: http://redis.io/commands

在应用程序中使用Redis

Redis提供了多种语言的绑定,以Ruby为例:

require 'redis'
r = Redis.new
r.set 'key', 1234
puts r.get 'key'
puts r.keys
Copy after login

输出结果应该为:

"1234"
["key"]
Copy after login
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to configure Lua script execution time in centos redis How to configure Lua script execution time in centos redis Apr 14, 2025 pm 02:12 PM

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to implement redis counter How to implement redis counter Apr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

See all articles