`
sljackson
  • 浏览: 33756 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

redis学习

 
阅读更多
1.redis的incr命令
    SET connections 10
    INCR connections => 11
    INCR connections => 12
    DEL connections
    INCR connections => 1

当多个客户去操作connections时,由于INCR命令是原子操作,所以会保持数据的一致性。

2.redis的存储失效设置
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


3.redis复杂结构的存储
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"


4.redis的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"


5.排序的set
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"


6.redis的hash
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"

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.
分享到:
评论

相关推荐

    Redis学习指南 Redis学习手册

    Redis,全称Remote Dictionary Server,是一款高性能的键值存储系统,常被用于...通过阅读"Redis学习手册",你将能够深入了解Redis的工作原理,熟练使用其各种功能,从而在你的IT职业生涯中发挥出Redis的强大效能。

    Redis学习笔记整理

    二、 redis学习笔记之数据类型 3 三、 redis学习笔记之排序 11 四、 redis学习笔记之事务 16 五、 redis学习笔记之pipeline 20 六、 redis学习笔记之发布订阅 23 七、 redis学习笔记之持久化 28 八、 redis学习笔记...

    超详细的redis学习笔记

    ### 超详细的Redis学习笔记知识点汇总 #### 1. Redis 的启动与停止 ##### 1.1 直接启动 Redis 服务 - **默认端口启动**:使用 `$ redis-server` 命令,默认监听端口为 `6379`。 - **指定端口启动**:使用 `$ ...

    redis学习笔记

    redis学习笔记redis 是一个开源的 key-value 数据库。它又经常被认为是一个数据结构服务器。 因为它的 value 不仅包括基本的 string 类型还有 list,set ,sorted set 和 hash 类型。当 然这些类型的元素也都是 string...

    redis学习相关资料

    redis学习相关资料 redis命令 redis文档总结 Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash...

    redis学习笔记.zip

    这个“redis学习笔记.zip”压缩包很可能是包含了关于Redis的学习资料,可能包括概念解释、操作教程、实践案例等内容,适合初学者和有一定基础的学习者参考。 Redis的学习可以分为以下几个主要部分: 1. **基础知识...

    Redis学习手册带详细教程

    Redis学习手册带详细教程 一、Redis概述 Redis是当前最流行的NoSQL数据库之一,它是一个开源的、基于内存的数据存储系统,可以用作数据库、消息队列、缓存等。Redis的主要特点是高性能、低延迟、支持事务、支持...

    redis学习笔记Redis.md

    ### Redis 学习笔记知识点概览 #### 一、Redis 概述与应用场景 ##### 1.1 NoSQL 数据库简介 - **定义**: NoSQL(Not Only SQL)泛指非关系型数据库,它们通常不使用传统的表格关系来存储数据。 - **特性**: NoSQL ...

    Redis学习-实战.docx

    ### Redis 学习与实战应用 #### 一、Redis 概述 Redis 是一款非常流行的非关系型(NoSQL)数据库。它不仅提供了快速的数据访问速度,还支持数据的持久化,使其成为许多应用场景下的首选。 ##### 1.1 NoSQL 数据库...

    redis学习笔记.docx

    Redis学习笔记 Redis是基于键值对存储的NoSQL数据库,可以用来存储和检索数据。下面是Redis的基础知识点: 基础命令 * set key value:保存一个数据,重复set相同的key只会保存最新的value * get key:获取一个...

    手慢无!redis学习资料打开网盘即可下载.rar

    在本压缩包中,你将找到一系列的Redis学习资料,涵盖了从基础到深入的多个方面,对于初学者来说是很好的学习资源。 首先,让我们了解一下Redis的基础知识。Redis是一个开源、基于内存的数据结构存储系统,它可以将...

    redis学习资料,不看后悔!5张精品知识脑图,免费下载!

    Redis,全称Remote Dictionary Server,是一款高性能的键值存储系统,常被用作数据库、缓存和消息中间件。它的特点是速度快,支持多种数据结构,如...所以,不要错过这份"redis学习资料",它将是你学习Redis的好帮手!

    Redis学习笔记.pdf

    Redis还支持主从复制和哨兵机制(Sentinel),前者可以实现数据的同步备份,后者则用于管理多个Redis服务器,实现故障转移。Redis集群的建立和管理可以进一步提升数据库的高可用性和扩展性,支持数据分片和负载均衡...

    Redis学习笔记

    首先,"深入redis学习(一)--readme and conf.doc"介绍了Redis的安装和配置过程。在配置文件中,我们可以设置服务器的端口号、绑定的IP地址、最大客户端连接数、数据库数量、超时时间、日志级别等参数。理解这些...

    java整个redis学习笔记整理

    java整个redis学习笔记整理,个人整理的学习日记,有一定的参考性

    redis学习文档

    总之,"redis学习文档"包含的两本书籍将带领读者全面了解Redis的核心原理和实际应用,无论是对初学者还是有经验的开发者来说,都是提升Redis技能的重要资源。通过系统学习,你可以更好地利用Redis来解决各种实际问题...

    redis学习笔记(详细总结)

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它支持存储的 value 类型相对更多,包括 ...

    Redis学习资料

    Redis学习手册 Hash数据类型 doc Redis学习手册 Key操作命令 doc Redis学习手册 List数据类型 doc Redis学习手册 Set数据类型 doc Redis学习手册 Sorted Sets数据类型 doc Redis学习手册 String数据类型 doc Redis...

Global site tag (gtag.js) - Google Analytics