`

Redis TUTORIAL

 
阅读更多

 

 
 

 

Welcome to Try Redis, a demonstration of the Redis database!

Please type TUTORIAL to begin a brief tutorial, HELP to see a list of supported commands, or any valid Redis command to play with the database.

Redis is what is called a key-value store, often referred to as a NoSQL database. The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it. We can use the command SET to store the value "fido" at key "server:name":


    SET server:name "fido"

Redis will store our data permanently, so we can later ask "What is the value stored at key server:name?" and Redis will reply with "fido":


    GET server:name => "fido"

Tip: You can click the commands above to automatically execute them. The text after the arrow (=>) shows the expected output.

Type NEXT to continue the tutorial.

> NEXT

Other common operations provided by key-value stores are DEL to delete a given key and associated value, SET-if-not-exists (called SETNX on Redis) that sets a key only if it does not already exist, and INCR to atomically increment a number stored at a given key:


    SET connections 10
    INCR connections => 11
    INCR connections => 12
    DEL connections
    INCR connections => 1

Type NEXT to continue the tutorial.

> NEXT

There is something special about INCR. Why do we provide such an operation if we can do it ourself with a bit of code? After all it is as simple as:

x = GET count
x = x + 1
SET count x

The problem is that doing the increment in this way will only work as long as there is a single client using the key. See what happens if two clients are accessing this key at the same time:

  1. Client A reads count as 10.
  2. Client B reads count as 10.
  3. Client A increments 10 and sets count to 11.
  4. Client B increments 10 and sets count to 11.

We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this way is not an atomic operation. Calling the INCR command in Redis will prevent this from happening, because it is an atomic operation. Redis provides many of these atomic operations on different types of data.

Type NEXT to continue the tutorial.

> NEXT

Redis can be told that a key should only exist for a certain length of time. This is accomplished with the EXPIRE and TTL commands.


    SET resource:lock "Redis Demo"
    EXPIRE resource:lock 120

This causes the key resource:lock to be deleted in 120 seconds. You can test how long a key will exist with the TTL command. It returns the number of seconds until it will be deleted.


    TTL resource:lock => 113
    // after 113s
    TTL resource:lock => -2

The -2 for the TTL of the key means that the key does not exist (anymore). A -1 for the TTL of the key means that it will never expire. Note that if you SET a key, its TTL will be reset.


    SET resource:lock "Redis Demo 1"
    EXPIRE resource:lock 120
    TTL resource:lock => 119
    SET resource:lock "Redis Demo 2"
    TTL resource:lock => -1

Type NEXT to continue the tutorial.

> NEXT
list

Redis also supports several more complex data structures. The first one we'll look at is a list. A list is a series of ordered values. Some of the important commands for interacting with lists are RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. You can immediately begin working with a key as a list, as long as it doesn't already exist as a different type.

RPUSH puts the new value at the end of the list.


    RPUSH friends "Alice"
    RPUSH friends "Bob"

LPUSH puts the new value at the start of the list.


    LPUSH friends "Sam"

LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve elements until the end of the list.


    LRANGE friends 0 -1 => 1) "Sam", 2) "Alice", 3) "Bob"
    LRANGE friends 0 1 => 1) "Sam", 2) "Alice"
    LRANGE friends 1 2 => 1) "Alice", 2) "Bob"

Type NEXT to continue the tutorial.

> NEXT

LLEN returns the current length of the list.


    LLEN friends => 3

LPOP removes the first element from the list and returns it.


    LPOP friends => "Sam"

RPOP removes the last element from the list and returns it.


    RPOP friends => "Bob"

Note that the list now only has one element:


    LLEN friends => 1
    LRANGE friends 0 -1 => 1) "Alice"

Type NEXT to continue the tutorial.

set

The next data structure that we'll look at is a set. A set is similar to a list, except it does not have a specific order and each element may only appear once. Some of the important commands in working with sets are SADD, SREM, SISMEMBER, SMEMBERS and SUNION.

SADD adds the given value to the set.

    SADD superpowers "flight"
    SADD superpowers "x-ray vision"
    SADD superpowers "reflexes"

SREM removes the given value from the set.

    SREM superpowers "reflexes"

Type NEXT to continue the tutorial.

> NEXT

SISMEMBER tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not.

    SISMEMBER superpowers "flight" => 1
    SISMEMBER superpowers "reflexes" => 0

SMEMBERS returns a list of all the members of this set.

    SMEMBERS superpowers => 1) "flight", 2) "x-ray vision"

SUNION combines two or more sets and returns the list of all elements.

    SADD birdpowers "pecking"
    SADD birdpowers "flight"
    SUNION superpowers birdpowers => 1) "pecking", 2) "x-ray vision", 3) "flight"

 

Sorted Sets

Sets are a very handy data type, but as they are unsorted they don't work well for a number of problems. This is why Redis 1.2 introduced Sorted Sets.

A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.

    ZADD hackers 1940 "Alan Kay"
    ZADD hackers 1906 "Grace Hopper"
    ZADD hackers 1953 "Richard Stallman"
    ZADD hackers 1965 "Yukihiro Matsumoto"
    ZADD hackers 1916 "Claude Shannon"
    ZADD hackers 1969 "Linus Torvalds"
    ZADD hackers 1957 "Sophie Wilson"
    ZADD hackers 1912 "Alan Turing"

In these examples, the scores are years of birth and the values are the names of famous hackers.

    ZRANGE hackers 2 4 => 1) "Claude Shannon", 2) "Alan Kay", 3) "Richard Stallman"
Hashes

Simple strings, sets and sorted sets already get a lot done but there is one more data type Redis can handle: Hashes.

Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: A User with a number of fields like name, surname, age, and so forth):

    HSET user:1000 name "John Smith"
    HSET user:1000 email "john.smith@example.com"
    HSET user:1000 password "s3cret"

To get back the saved data use HGETALL:

    HGETALL user:1000

You can also set multiple fields at once:

    HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"

If you only need a single field value that is possible as well:

    HGET user:1001 name => "Mary Jones"

Type NEXT to continue the tutorial.

> NEXT

Numerical values in hash fields are handled exactly the same as in simple strings and there are operations to increment this value in an atomic way.


    HSET user:1000 visits 10
    HINCRBY user:1000 visits 1 => 11
    HINCRBY user:1000 visits 10 => 21
    HDEL user:1000 visits
    HINCRBY user:1000 visits 1 => 1

Check the full list of Hash commands for more information.

Type NEXT to continue the tutorial.

> NEXT

That wraps up the Try Redis tutorial. Please feel free to goof around with this console as much as you'd like.

Check out the following links to continue learning about Redis.

 

 

This site was originally written by Alex McHale (github twitter) and inspired by Try Mongo. It's now maintained and hosted by Jan-Erik Rediger (github twitter)

The source code to Try Redis is available on GitHub.

 启动和关闭

cd 到 redis 目录,启动redis服务器

$ src/redis-server

开一个redis终端

$ src/redis-cli

 

关闭

在 redis-cli 端  shutdown ,命令关闭 redis-server

quit  退出 redis-cli

 

在python中使用redis

 

分享到:
评论

相关推荐

    shiro-redis-tutorial:shiro-redis教程

    shiro-redis-tutorial 这是一个教程,可帮助您了解如何使用shiro-redis 。 本教程使用shiro.ini配置shiro和shiro-redis 。如何使用它? 使用以下注释将shiro-redis-tutorial克隆到磁盘: git clone ...

    Redis-Tutorial.docx

    ### Redis 开发教程知识点概述 #### 一、Redis 基础概念与应用场景 - **定义**:Redis 是一个开源的内存数据结构存储系统,它不仅可以用作数据库,还可以作为缓存和消息代理。 - **许可协议**:Redis 采用 BSD 许可...

    redis-tutorial.pdf

    ### Redis 教程知识点概述 #### 一、Redis简介与特性 Redis是一种开源的、BSD许可的高级键值存储系统。由于其强大的数据结构服务功能,Redis常常被称为数据结构服务器。这里的键可以包含多种类型的数据,例如字符...

    redis中hash表内容删除的方法代码

    这将创建一个名为`runoobkey`的哈希,其中包含四个键值对:`name`对应`redis tutorial`,`description`对应`redis basic commands for caching`,`likes`对应`20`,以及`visitors`对应`23000`。 当我们想要删除Hash...

    Windows服务器中PHP如何安装redis扩展

    $redis->set("tutorial-name", "Redis tutorial"); echo "Stored string in redis:: " . $redis->get("tutorial-name"); ``` 7. **启动Redis服务**:确保Redis服务器已在本地运行。可以下载Redis的Windows版本,...

    Java使用Redis的方法实例分析

    jedis.set("w3ckey", "Redis tutorial"); // 获取存储的数据并输出 System.out.println("Stored string in redis:: " + jedis.get("w3ckey")); } } ``` 编译并运行上面的程序,我们可以看到存储的字符串数据。 ...

    Redis分布式集群配置文档

    Redis分布式集群配置文档 Redis是一种基于内存的NoSQL数据库,它...1. Redis官方文档:http://www.redis.cn/topics/cluster-tutorial.html 2. Redis集群安装和配置:http://www.linuxidc.com/Linux/2015-08/121845.htm

    redis哈希和集合_动力节点Java学院整理

    redis 127.0.0.1:6379> HMSET yiibai name redis tutorial description redis basic commands for caching likes 20 visitors 23000 OK redis 127.0.0.1:6379> HGETALL yiibai 1) name 2) redis tutorial 3) ...

    Redis-4.0.11集群配置

    参考:http://redis.io/topics/cluster-tutorial。 集群部署交互式命令行工具:https://github.com/eyjian/redis-tools/tree/master/deploy 集群运维命令行工具:https://github.com/eyjian/redis-tools/tree/master...

    redis-tutorial:基于docker容器使用redis在内存上托管数据

    redis-tutorial:基于docker容器使用redis在内存上托管数据

    Redis3.x集群配置操作文档

    - [Redis Cluster Tutorial – Redis](http://redis.io/topics/cluster-tutorial) - [集群教程 — Redis 命令参考](http://redisdoc.com/topic/cluster-tutorial.html) #### 创建工作目录 ```bash # 创建集群测试...

    tutorial-master_redis_quotes_MongoDB_scrapy_

    在本教程中,我们将深入探讨如何使用`scrapy-redis`框架实现一个完全分布式的爬虫,用于抓取quotes网站的数据,并将数据存储到`Redis`和`MongoDB`数据库中。`scrapy-redis`是`Scrapy`的一个扩展,它使Scrapy能够与`...

    scrapy-heroku-redis-tutorial

    在这个"scrapy-heroku-redis-tutorial"中,我们将探讨如何将这三个技术结合起来,实现一个在Heroku上运行的Scrapy爬虫,并利用Redis进行任务调度。 首先,让我们深入了解Scrapy。Scrapy提供了一个结构化的框架,...

    Learning Redis

    A simple step-by-step tutorial explaining the main concepts of Redis through practical examples Who This Book Is For This book is for SQL developers who want to learn about Redis, the key value ...

    phpstudy 常用 PHP 版本的 php_memcache.dll 及 php_redis.dll 扩展

    描述中提到的“在phpstudy/PHPTutorial文件夹内解压缩”,意味着这些扩展文件已经打包在压缩包中,用户只需将其解压到phpstudy的相应目录下,然后编辑`php.ini`配置文件,将扩展添加到`extension=`行后,重启PHP服务...

    Java 使用 Redis开发.pdf

    jedis.lpush("tutorial-list", "Redis"); jedis.lpush("tutorial-list", "Mongodb"); jedis.lpush("tutorial-list", "Mysql"); // 获取存储的数据并输出 List<String> list = jedis.lrange("tutorial-list", 0,...

    分布式键值-redis1

    集群教程(http://www.redis.cn/topics/cluster-tutorial.html)详细介绍了如何搭建和管理Redis集群,包括节点发现、槽分配以及数据分片等核心概念。 Redis的核心在于其丰富的数据结构,如字符串、列表、哈希、集合...

    Orleans.Redis刚才翻看Orleans的官方网站,发现除了大家关注比较多的"Step-by-step Tutorial

    Orleans.Redis结合redis使用方法。 HelloWorld.Grains HelloWorld.Interfaces RedisClient RedisGatewayHost RedisSiloHost 示例源代码,期望能够帮助用到的同学。

    redis资源整合包下载(包含群集部署相关资料)

    1. `redis-cluster-tutorial.pdf` - 这可能是一个初学者向导,详细介绍如何设置和管理Redis集群。 2. `configuring_replication.md` - 这可能是关于配置主从复制的文档,详细解释如何配置和维护节点间的复制关系。 3...

Global site tag (gtag.js) - Google Analytics