Jdeis api使用redis有序集
来源:互联网 发布:linux清空所有文件内容 编辑:程序博客网 时间:2024/06/11 00:25
Jedis 是 Redis 官方首选的 Java 客户端开发包。这篇文章我们将介绍如何使用 Sorted Set 排序集合(zsets)。
Sorted Set 跟一个集合一样,它是不会存在重复的数值,最大的不同是 Sorted Set 中每个元素都是经过排序的。
我们先看一些命令:
01
import
java.util.HashMap;
02
import
java.util.Map;
03
04
import
redis.clients.jedis.Jedis;
05
public
class
TestJedis {
06
07
public
static
void
main(String[] args) {
08
String key =
"mostUsedLanguages"
;
09
Jedis jedis =
new
Jedis(
"localhost"
);
10
//Adding a value with score to the set
11
jedis.zadd(key,
100
,
"Java"
);
//ZADD
12
13
//We could add more than one value in one calling
14
Map<Double, String> scoreMembers =
new
HashMap<Double, String>();
15
scoreMembers.put(90d,
"Python"
);
16
scoreMembers.put(80d,
"Javascript"
);
17
jedis.zadd(key, scoreMembers);
18
19
//We could get the score for a member
20
System.out.println(
"Number of Java users:"
+ jedis.zscore(key,
"Java"
));
21
22
//We could get the number of elements on the set
23
System.out.println(
"Number of elements:"
+ jedis.zcard(key));
//ZCARD
24
}
25
}
上述例子中我们看到了 Zset 命令,为了将元素添加到 zet 中,我们使用 zadd 方法,不同的是我们还传递了一个元素的评分值,我们可以使用 Map 对象来一次传递很多个对象,而 zadd 方法可用于增加和更新已有元素的评分值。
我们可使用 zscore 来获取某元素的评分,通过 zcard 获取元素个数。
下面的例子我们可看到来自 zsets 的其他命令:
01
import
java.util.Set;
02
03
import
redis.clients.jedis.Jedis;
04
import
redis.clients.jedis.Tuple;
05
public
class
TestJedis {
06
07
public
static
void
main(String[] args) {
08
String key =
"mostUsedLanguages"
;
09
Jedis jedis =
new
Jedis(
"localhost"
);
10
11
//get all the elements sorted from bottom to top
12
System.out.println(jedis.zrange(key,
0
, -
1
));
13
14
//get all the elements sorted from top to bottom
15
System.out.println(jedis.zrevrange(key,
0
, -
1
));
16
//We could get the elements with the associated score
17
Set<Tuple> elements = jedis.zrevrangeWithScores(key,
0
, -
1
);
18
for
(Tuple tuple: elements){
19
System.out.println(tuple.getElement() +
"-"
+ tuple.getScore());
20
}
21
22
//We can increment a score for a element using ZINCRBY
23
System.out.println(
"Score before zincrby:"
+ jedis.zscore(key,
"Python"
));
24
//Incrementing the element score
25
jedis.zincrby(key,
1
,
"Python"
);
26
System.out.println(
"Score after zincrby:"
+ jedis.zscore(key,
"Python"
));
27
}
28
}
通过 zrange 我们能获取给定范围的元素,它将返回经过排序后的元素列表(自底向上),也可以通过 zrevrrange 获取自顶向下的元素列表。Redis 还允许我们通过关联的评分来获取元素,传递 “withscores“ 参数即可。使用 Jedis API 的zrevrangeWithScores 方法可返回对象集合。另外一个有用的命令是 zincrby 可用于增加元素的评分值。
zsets 还有其他的命令,这里我们只是介绍跟 Jedis API 相关的一些基本用法。这里还有一些关于排序集合的介绍。
- Jdeis api使用redis有序集
- Redis 有序集(Sorted set)操作
- redis源码分析之有序集SortedSet
- redis的java API使用
- Redis常用API-使用文档
- Redis中API的使用
- Redis常用API-使用文档
- Redis常用API-使用文档
- Redis + php 使用hash(哈希) ,zset(有序集合)实例
- php-redis中有序集合 zset的使用
- Redis命令 -- 有序集合
- redis-有序集合
- redis 有序集合
- redis实现有序队列
- redis 有序集合
- Redis 有序集合
- Redis-zset (有序集合)
- Redis 有序集合
- Debian中源代码方式安装Mysql+PHP+Nginx
- CGI入门学习
- Debian中实现nginx和spawn-fcgi开机启动
- Apache Mina 开发简介
- ubuntu10.04下vmware workstation破解安装
- Jdeis api使用redis有序集
- 一个很好的java反编译器
- Click,感觉还不错
- python 专题八 SMTP 邮件发送
- 东经120,月季花,星期天
- 调试3G模块语音通话
- sleep()和wait()区别
- ubuntu10.04网络打印机设置及openoffice实现双面打印
- 发现javaeye论坛的一个bug