- 浏览: 561391 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (478)
- lucene (45)
- oracle (19)
- nutch (2)
- blog (2)
- 垂直搜索 (19)
- java综合 (89)
- spring (15)
- Hibernate (9)
- Struts (9)
- Hadoop (16)
- Mysql (12)
- nosql (10)
- Linux (3)
- MyEclipse (4)
- Ant (1)
- 设计模式 (19)
- JBPM (1)
- JSP (1)
- HtmlParser (5)
- SVN (2)
- 插件 (2)
- 收藏 (7)
- Others (1)
- Heritrix (18)
- Solr (4)
- 主题爬虫 (31)
- 内存数据库 (24)
- 分布式与海量数据 (32)
- httpclient (14)
- Tomcat (1)
- 面试宝典 (6)
- Python (14)
- 数据挖掘 (1)
- 算法 (6)
- 其他 (4)
- JVM (12)
- Redis (18)
最新评论
-
hanjiyun:
本人水平还有待提高,进步空间很大,看这些文章给我有很大的指导作 ...
JVM的内存管理 Ⅲ -
liuxinglanyue:
四年后的自己:这种方法 不靠谱。 使用javaagent的方式 ...
计算Java对象占用内存空间的大小(对于32位虚拟机而言) -
jaysoncn:
附件在哪里啊test.NoCertificationHttps ...
使用HttpClient过程中常见的一些问题 -
231fuchenxi:
你好,有redis,memlink,mysql的测试代码吗?可 ...
MemLink 性能测试 -
guyue1015:
[color=orange][/color][size=lar ...
JAVA同步机制
Expiry
The EXPIRE
command enables a timeout to be set for a specified key. Once the timeout is reached, the server can delete the key. We call a key volatile
if it is set to expire.
Note that expiry semantics changed very significantly with Redis 2.1.3. It is important to be aware of the differences, especially given the wide install base of Redis 2.0.
Redis Versions Prior to 2.1.3
The initial design of Redis' expiry mechanism is extremely concerned with consistency.
Here, if the value of a volatile key is modified in any way, then the key becomes non-volatile and will not expire unless explicitly EXPIRE
’d again.
Furthermore, the following restrictions apply:
- If a key is volatile, the EXPIRE command cannot be re-issued to change the expiry time
- A write operation against a volatile key will destroy the key first and thenperform the write
For example:
redis> SET foo bar
OK
redis> EXPIRE foo 100000
(integer) 1
redis> GET foo
"bar"
redis> APPEND foo 123
(integer) 3
redis> GET foo
"123"
In order to enforce consistency requirements, this is expected behaviour. (To be strongly consistent, if n
servers receive the same list of commands in the same sequence, they must always end with the same dataset in memory. However, with replication over unreliable network links, and without the restrictions detailed above, slowdowns in the network could cause the master and slaves to get out sync, thus violating the consistency requirement.)
Redis Versions 2.1.3 and Above
None of these constraints apply to the later versions of Redis (i.e., write operations no longer destroy the key before performing the write, and expiry times can be modified after initially being set). The implementation was changed to remove the constraints,without sacrificing the consistency requirements.
The new implementation simply injects a DEL
(delete) operation into the replication stream and the AOF file every time that the server expires a key.
More on Strings
We previously demonstrated the GET
, SET
, and APPEND
commands.
Increment
The INCR
command makes it possible to use strings as an atomic counter.
For example:
redis> SET foo 0
OK
redis> INCR foo
(integer) 1
redis> INCR foo
(integer) 2
redis> GET foo
"2"
redis> SET bar baz
OK
redis> INCR bar
(error) ERR value is not an integer
SETNX and Locking
The SETNX
(“set if not exists”) command works like SET
, but performs no operation if the key already exists.
This command can be used to implement a locking system. For example, the Redis documentation describes a locking algorithm.
Lists
Lists are used to store an (ordered) collection of items. Stacks and queues can be very easily modelled with lists.
For example:
redis> LPUSH newset a
(integer) 1
redis> LRANGE newset 0 -1
1. "a"
redis> LPUSH newset b
(integer) 2
redis> RPUSH newset c
(integer) 3
redis> LRANGE newset 0 -1
1. "b"
2. "a"
3. "c"
redis> LPUSH myset z
(integer) 1
redis> RPOPLPUSH newset myset
"c"
redis> LRANGE newset 0 -1
1. "b"
2. "a"
redis> LRANGE myset 0 -1
1. "c"
2. "z"
Sets
Sets store an un-ordered, unique collection of items. In addition to supporting sets, Redis has native support for for union, intersection, and diff operations.
redis> SADD set1 a
(integer) 1
redis> SADD set1 b
(integer) 1
redis> SADD set1 c
(integer) 1
redis> SADD set2 c
(integer) 1
redis> SADD set2 d
(integer) 1
redis> SADD set2 e
(integer) 1
redis> SINTER set1 set2
1. "c"
redis> SUNION set1 set2
1. "c"
2. "d"
3. "a"
4. "b"
5. "e"
Sets are a critical tool for building highly-optimized indexes. For example, if your application allows users to like articles and follow users, then you should be manually maintaining indexes (i.e. sets) to pre-compute the answers to questions like “which users liked article x
” and “which users follow user y
”.
Sorted Sets
Sorted sets (also known as “zsets”) are similar to sets, but each member of the set has an associated floating point score. Members are stored in sorted order, so no sorting is required to retrieve the data ordered by the floating point score.
For example:
redis> ZADD days 1 Monday
(integer) 1
redis> ZADD days 2 Tuesday
(integer) 1
redis> ZADD days 3 Wednesday
(integer) 1
redis> ZADD days 4 Thursday
(integer) 1
redis> ZADD days 5 Friday
(integer) 1
redis> ZADD days 6 Saturday
(integer) 1
redis> ZADD days 7 Sunday
(integer) 1
redis> ZRANGE days 0 -1
1. "Monday"
2. "Tuesday"
3. "Wednesday"
4. "Thursday"
5. "Friday"
6. "Saturday"
7. "Sunday"
Sorted sets are essential whenever a range query is needed. For a clever application of sorted sets, see Auto Complete with Redis.
Furthermore, as with plain sets, sorted sets are an important tool to consider when constructing indexes.
Hashes
Redis hashes are conceptually equivalent to the Ruby hash, Python dictionary, Java hash table or hash map, etc.
For example:
redis> HSET user:1 name bob
(integer) 1
redis> HSET user:1 email b@bob.com
(integer) 1
redis> HGET user:1 name
"bob"
redis> HGETALL user:1
1. "name"
2. "bob"
3. "email"
4. "b@bob.com"
Before this data type was added to Redis, users had two main options for storing hashes: use one key to store a JSON structure with multiple fields, or use one key for each field in the hash. Both of these approaches have many downsides versus using the native hash data type; the former introduces concurrency issues, and the latter requires significantly more memory.
Redis Transactions
Redis transactions can be used to make a series of commands atomic. (Every Redis command is already atomic; transactions, however, allow us to combine a number of commands into a single atomic unit.)
In this example, the SET and two INCR commands are all executed atomically within the context of a transaction:
redis> MULTI
OK
redis> SET foo 0
QUEUED
redis> INCR foo
QUEUED
redis> INCR foo
QUEUED
redis> EXEC
1. OK
2. (integer) 1
3. (integer) 2
Here, MULTI
delineates the start of a transaction block; EXEC
is responsible for actually running the transaction (i.e., executing all commands between the MULTI
and the EXEC
, with no other commands executed in-between).
There is an important caveat: because the commands are queued and then executed sequentially (all at once, as if they were a single unit), it is not possible to read a value while a transaction is being executed and to then subsequently use this value at any point during the course of the same transaction.
To address this restriction, Redis supports “Check and Set” (CAS) transactions. CAS is available via the WATCH
command.
The following example (in pseudo-code) is an example of a good implementation of a function that only increments a key if the current value of the key is even.
WATCH mykey
val = GET mykey
MULTI
if val % 2 == 0:
SET mykey (val+1)
EXEC
If mykey
changes after the WATCH
command is executed but before the EXEC
, then the entire transaction is aborted. The client can retry indefinitely on transaction failure. Any implementation that does not not use CAS is subject to race conditions, unless some other locking mechanism is employed (for example, the locking system described during the treatment of SETNX
).
A more involved example, taken from an earlier post I made to the Redis mailing list:
I'm looking to build a FIFO queue, with one important/ non-standard property: if an item that already exists in the queue is added again, then the insert should effectively be ignored. (This data structure can be useful for managing certain types of jobs, like warming caches, etc.)
An efficient way to model this structure in Redis would be to use a ZSET, where the score of each item in the set is the unix timestamp of when the item was added.
However, the semantics of ZADD are that if an item already exists in the set, that the score is updated to the score specified in the command. If there were some way to tell ZADD to not update the score if an element already exists, or perhaps to supply a certain score if the element is new and a different score if the element is not new, then this queue-like data structure could be easily (and very efficiently) modelled in Redis.
There are potentially a few other ways to do this, with the obvious approach being to check if an item already exists in the ZSET (using a set intersection) before issuing the ZADD command. However, this isn't bullet-proof, because it doesn't look like we will be able to do this within the context of a MULTI/EXEC.
Does anyone have any other ideas?
Coincidentally, this was posted on the exact same day that the WATCH
command made it into the Redis master branch.
A solution (in pseudocode, with thanks to Salvatore):
FUNCTION ZADDNX(key,score,element):
WATCH key
MULTI
IF (ZSCORE key element) == NIL
ZADD key score element
retval = EXEC
IF retval == NIL:
ZADDNX(key,score,element)
ELSE
DISCARD
(In this example: if the element already exists in the ZSET, then do nothing.)
发表评论
-
Redis: under the hood(收藏)
2011-01-03 10:54 1133Redis: under the hood How ... -
Redis指令文档(非常有用的)
2011-01-01 15:32 1628连接控制QUIT 关闭连接AUTH (仅限启用时)简单的密 ... -
Webdis – 为 Redis 提供 HTTP 接口
2010-12-31 09:24 2083Redis 一直以来只提供纯文本操作协议(只有在 C ... -
Redis几个认识误区
2010-12-05 09:25 1062来自timyang的博客:Redi ... -
Redis tutorial, April 2010
2010-12-01 13:38 1351文章太长了,下面是其中的一小部分 转:http://simo ... -
redis常用命令
2010-12-01 13:22 21091、redis-benchmark redis基准信息,red ... -
使用Jredis做的小例子(入门级)
2010-11-30 16:02 5844redis入门级例子: package com. ... -
Redis命令总结
2010-11-30 13:03 801Redis提供了丰富的命令(command)对 ... -
Redis, from the Ground Up
2010-11-30 10:58 786Redis, from the Ground Up A ... -
Redis, from the Ground Up(4)
2010-11-30 10:57 915Redis Virtual Memory The go ... -
Redis, from the Ground Up(2)
2010-11-30 10:55 674Key Disadvantages Redis req ... -
Redis, from the Ground Up(1)
2010-11-30 10:52 834A deep dive into Redis' orig ... -
深入Redis,读redis-from-the-ground-up有感(转)
2010-11-30 10:50 1139上有一篇介绍Redis的文章,由浅入深地讲解了Redis: ... -
JRedisQuickStart
2010-11-29 22:20 1050JRedisQuickStart #Get g ... -
键值数据库—Redis(一) 基础入门
2010-11-29 21:46 1519Redis的知识准备 redis的基础介绍:http:/ ... -
Redis配置文件redis.conf参数解读
2010-11-29 20:43 1864转:http://blog.csdn.net/Java2K ... -
linux下redis的安装
2010-11-29 20:41 1004源地址:http://hanqunfeng.iteye.c ...
相关推荐
Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装Redis3集群安装...
Redis is the most popular in-memory key-value data store. It is very lightweight and its data types give it an edge over other competitors. If you need an in-memory database or a high-performance ...
(3)多主复制:Redis 3.0支持多主复制,可以将多个Redis实例之间的数据进行同步,有效高了Redis的可靠性; (4)集群:Redis 3.0支持集群,可以将多个Redis实例拼接成一个集群,并实现负载均衡,提高Redis的可用性...
Redis Essentials is a fast-paced guide that teaches the fundamentals on data types, explains how to manage data through commands, and shares experiences from big players in the industry. We start off...
本项目是基于C#的NewLife.Redis高性能Redis协议封装设计源码,包含119个文件,其中包括95个C#源文件、6个csproj文件、3个YAML文件、2个PNG图片文件、2个pubxml文件、1个Editorconfig文件、1个gitignore文件、1个eddx...
NULL 博文链接:https://sichen84.iteye.com/blog/2419876
A SpringBoot project based on Redis from nowcoder.
You'll begin by getting Redis set up properly and then exploring the key-value model. Then, you'll dive into real use cases including simple caching, distributed ad targeting, and more. You'll learn ...
& W3 B, |3 M4 O5 |- k 本书一共由三个部分组成。第一部分对Redis进行了介 绍,说明了Redis的基本使用方法、它拥有的5种数据结构以及操作这5种数据结构的命令,并讲解了如何使用Redis去构建文章展示网站、cookie、...
通过这个压缩包中的"redis-desktop-manager-0.9.3.817.exe"文件,用户可以安装和运行RedisDesktopManager。该可执行文件是经过编译的Windows程序,包含了所有必要的库和资源,使得用户无需额外配置环境即可直接使用...
redis配置文件redis.conf
redis可视化工具,便于管理和查看redis数据 redis可视化工具,便于管理和查看redis数据 redis可视化工具,便于管理和查看redis数据 redis可视化工具,便于管理和查看redis数据 redis可视化工具,便于管理和查看redis...
**RedisDesktopManager 2021.3 for Windows:** RedisDesktopManager(RDM)是一款流行的开源图形化界面工具,专为管理和操作Redis键值存储系统而设计。2021.3版本是针对Windows平台的更新,旨在提供更加稳定、高效...
最近需要在C#中使用Redis,在Redis的官网找到了ServiceStack.Redis,最后在测试的时候发现这是个坑,4.0已上已经收费,后面只好找到3系列的最终版本,最后测试发现还是有BUG或者是我不会用。没有办法,最好找到了...
3. **Redis 命令行工具**: - `redis-cli.exe`是Redis的命令行客户端,用户可以通过它连接到Redis服务器,执行各种操作,如SET、GET、DEL等命令,或者进行键空间的监控。 - `redis-benchmark.exe`是性能测试工具,...
redis-5.0.3 redis-5.0.4 redis-5.0.5
Redis 是一个开源的、基于键值对的数据存储系统,它通常用作数据库、缓存和消息中间件。Redis 的名称是“Remote Dictionary Server”的缩写,由意大利开发者 Salvatore Sanfilippo 创建。在 Windows 64 位环境下安装...
3. **持久化**:Redis支持两种持久化方式:RDB(快照)和AOF(Append Only File)。RDB会在特定时间点创建数据库的全量备份,而AOF记录所有写操作日志,确保数据安全。 4. **主从复制**:为了提高可用性和扩展性,...
Redis binaries are built with the original source Redis and have been compiled with free Visual Studio 2019 (VC16,Community Edition, version 16.2.5) to obtain higher performance and better stability ...