Redis入门简介
深入了解redis请阅读官方文档:redis documentation
http://redis.io/documentation
再深入了解redis阅读源代码分析:redis under the hood
http://pauladamsmith.com/articles/redis-under-the-hood.html
1、认识redis
http://redis.io/
Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.
2、下载
Redis 2.4.7 is the latest stable version.
当前版本:2.4.7 http://redis.googlecode.com/files/redis-2.4.7.tar.gz
代码还是存放在googlecode,但是建设自己的门户了,便于宣传。如果代码放自己站点,不然没有放googlecode那么好的下载性能了(这玩意儿跟我们很多人挂视频,都利用youku的站外播放器一样)。
Other downloads are available on GitHub and Google Code.
3、安装
http://redis.io/download
Installation
Download, extract and compile Redis with:
$ wget http://redis.googlecode.com/files/redis-2.4.7.tar.gz
$ tar xzf redis-2.4.7.tar.gz
$ cd redis-2.4.7
$ make
你发现原来安装这么简单,甚至都不用configure和make install,怎么这么简单呢?!
4、启动
The binaries that are now compiled are available in the src directory. Run Redis with:
经过上面的安装后,二进制的可执行文件就生成了,就放在src子目录下(src: script脚本的意思),这点又很简单,并没有放到/usr/local/bin之类的目录下,就感觉不需要懂别的,光用redis就可以了。
$ src/redis-server
注意:这种启动是在前台启动的,不是后台。
You can interact with Redis using the built-in client:
Redis多周到,客户端命令行工具都给你准备了,马上就可以体验。
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
4、教程
Are you new to Redis? Try our online, interactive tutorial.
Ready for a test drive? Check this interactive tutorial that will walk you through the most important features of Redis.
教程地址: http://try.redis-db.com/
顺便说下,目前redis还不支持集群,但是支持主从复制,redis开发人员说预计在redis2.6版本将支持集群。
Redis数据结构入门教程
教程地址: http://try.redis-db.com/
计数器(INCR累加命令:原子操作,防“Lost Update”)
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:
Client A reads count as 10.
Client B reads count as 10.
Client A increments 10 and sets count to 11.
Client B increments 10 and sets count to 11.
备注:这个是典型的Lost Update问题。原子累加命令INCR,也是为什么redis适合应用在“计数器”的应用中,比如:PV统计计数,播放量采集。
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.
缓存(过期时间的设置和查询)
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 for with the TTL command. It returns the number of seconds until it will be deleted.
TTL resource:lock => 113
TTL count => -1
The -1 for the TTL of the key count means that it will never expire. Note that if you SET a key, its TTL will 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
摘要:
1、 redis默认的set是不过期的;
2、 过期时间单位是:秒,不是毫秒,这点跟memcached一样,很容易理解“毫秒”没有意义,毕竟redis是网络操作的。
3、 Redis的过期时间不仅可以设置,还可以查询什么时候会过期。而且,redis的过期时间,可以随时更新。
4、 Redis的过期时间检测采用两种技术:Lazy Expiration,既读取的时候判断是否过期;和利用监控线程,周期性的对keys进行抽样检测,发现过期的,就删除。这么说来,如果你要捕获过期事件,是没有严格意义的。
SNS好友列表(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 "Tom"
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 all elements in the list.
LRANGE friends 0 -1 => ["Sam","Tom","Bob"]
LRANGE friends 0 1 => ["Sam","Tom"]
LRANGE friends 1 2 => ["Tom","Bob"]
摘要:
1、 LIST提供“左边插入”,“左边移除”,“右边插入”,“右边移除”的操作;
2、 LIST还提供“子集查询”,通过LRANGE命令可以任意查看LIST的任意连续子序列。编号从0开始,-1表示结尾。区间是:双闭区间。
3、 LIST的应用场景:SNS中的好友列表。比如新浪微博的“粉丝”就可以用redis的LIST来CACHE,以减少对DB的压力。在Redis出现前,我们要实现列表CACHE是很麻烦的,比如memcached如果缓存了用户 uid的好友列表fids={f1,f2,…,fn};如果某个时候用户uid删除了某个好友,这个时候我们需要更新列表,而memcached的key-value的value数据结构太笨了,或者应该说value是无结构的,要删除其中的fids={f1,f2,…,fn}是很费劲的。
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"
SISMEMBER tests if the given value is in the set.
SISMEMBER superpowers "flight" => true
SISMEMBER superpowers "reflexes" => false
SMEMBERS returns a list of all the members of this set.
SMEMBERS superpowers => ["flight","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 => ["flight","x-ray vision","pecking"]
有序集(SortedSet:LIST和SET的折中)
The last data structure which Redis supports is the sorted set. It 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 1953 "Richard Stallman"
ZADD hackers 1965 "Yukihiro Matsumoto"
ZADD hackers 1916 "Claude Shannon"
ZADD hackers 1969 "Linus Torvalds"
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 => ["Alan Kay","Richard Stallman","Yukihiro Matsumoto"]
Redis官方文档
http://redis.io/documentation
Topics
? Data types: a summary of the different types of values that Redis supports.
? Replication: what you need to know in order to set up master-slave replication.
? Persistence: know your options when configuring Redis' durability.
? Virtual memory: when your dataset doesn't fit in RAM, you can use VM.
? Pipelining: learn how to send multiple commands at once, saving on round trip time.
? Redis Pub/Sub: Redis is a fast and stable Publish/Subscribe messaging system! Check it out.
? Memory optimization: understand how Redis uses RAM and learn some tricks to use less of it.
? High latency troubleshooting: read this document if you want to understand the possible causes of high latency in Redis.
? Redis Administration: selected administration topics.
? Benchmarks: see how fast Redis is in different platforms.
? FAQ: some common questions about Redis.
? Protocol specification: if you're implementing a client, or out of curiosity, learn how to communicate with Redis at a low level.
? Debugging Redis: in the unlikely event you are experiencing a Redis crash, you can use this guide to send the right information to the Redis Core Team.
? Internals: learn details about how Redis is implemented under the hood.
? Who's using it?
附录:Redis命令大全
http://redis.io/commands
分享到:
相关推荐
### Redis入门介绍与基础知识 #### 一、Redis简介 Redis是一种高性能的(key/value)分布式内存数据库,也被称为数据结构服务器。它由纯C语言编写,完全开源且免费使用,遵循BSD许可协议。作为NoSQL数据库的一种,...
本指南将帮助初学者理解 Redis 的基本概念、安装与配置、数据类型以及常用命令,从而快速入门 Redis。 1. **Redis 简介** - Redis 源自意大利程序员 Salvatore Sanfilippo,最初是为了提高网站性能而设计的。 - ...
以下是对 Redis 入门指南的一些关键知识点的详细说明: 1. **Redis 数据类型** - **字符串(String)**:Redis 最基本的数据类型,可以存储字符串、数字等,支持自增、自减操作。 - **哈希(Hash)**:用于存储...
Redis入门到精通最新教学视频!!!!!!!!!!!!!!!!!!!
Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南
Redis入门简单实例,附带详细说明,代码可直接运行,欢迎交流。
Redis入门指南(第2版)Redis入门指南(第2版)Redis入门指南(第2版)Redis入门指南(第2版)Redis入门指南(第2版)Redis入门指南(第2版)
Redis入门指南(第2版)完整版
**Redis入门第二版** Redis,全称Remote Dictionary Server,是一个开源的、高性能的键值存储系统,常被用作数据库、缓存和消息中间件。它支持多种数据结构,如字符串、哈希、列表、集合、有序集合等,这使得Redis...
《Redis入门手册》是一份专为初学者准备的中文文档,旨在帮助读者快速理解和掌握Redis的基本概念和操作。通过阅读这份手册,你可以了解到以下关键知识点: 1. **安装与配置**:了解如何在不同的操作系统上安装Redis...
Redis从入门到精通2024版 视频教程 下载 ├─第 01 章 开篇 │ 001.Redis 录制计划.mp4 │ 002.Redis 介绍.mp4 │ 003.Redis 安装.mp4 │ ├─第 02 章 基本数据类型 │ 01.在后台启动 Redis.mp4 │ 02....
博客链接中的文章《.NET下Redis入门演示》可能会进一步解释这些操作的细节,包括如何配置连接、处理连接池、使用事务和发布/订阅功能等。同时,它可能还会涉及错误处理和性能优化技巧。 在实际项目中,Redis还可以...
Redis(REmote DIctionary Server)是一个key-value存储... 2 Lua脚本语言的介绍, 3 Redis和Lua结合,Redis的Lua脚本编程,构建强大的Redis服务。 4 Redis整合Spring等。 5 Redis集群实现Tomcat集群的Session共享等
本“Redis入门指南”将带领读者全面了解Redis的基础知识、安装配置、常用命令以及实际应用。 首先,让我们了解一下Redis的基本概念。Redis是一个开源、基于内存、网络交互的NoSQL数据库,它将数据存储在内存中,以...
Redis 入门指南 第二版 mobi文字版 特别适合用kindle(或者手机kindle app)看,随时随地学习!
Scrapy和Redis是两个在IT领域中非常重要的工具,它们在数据抓取和处理方面有着广泛的应用。本文将深入探讨如何结合使用Scrapy框架和Redis数据库来构建一个高效的定向爬虫。 Scrapy是一个用Python编写的开源Web爬虫...