package com.panku.web.redis;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
import org.springframework.data.redis.connection.RedisZSetCommands.Range;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Spring RedisTemplate操作-ZSet操作
* @author ccx
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class RedisTemplateZSet {
@Resource(name="redisTemplate")
private RedisTemplate rt;
public void flushdb(){
rt.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return "ok";
}
});
}
@Test
public void set(){
flushdb();
ZSetOperations vo = rt.opsForZSet();
vo.add("book", "a", 1d);
vo.add("book", "b", 2d);
Set> tuples = new HashSet>();
TypedTuple tuple0 = new DefaultTypedTuple("c", 3d);
tuples.add(tuple0);
TypedTuple tuple1 = new DefaultTypedTuple("d", 4d);
tuples.add(tuple1);
TypedTuple tuple2 = new DefaultTypedTuple("e", 5d);
tuples.add(tuple2);
vo.add("book", tuples);
vo.incrementScore("book", "e", 1d);
vo.add("book", "f", 7);
vo.add("book", "g", 8);
vo.add("book", "h", 9);
// vo.remove("book", "g","h");
vo.removeRange("book", 7, 8);
vo.removeRangeByScore("book", 8, 9);
}
@Test
public void get(){
ZSetOperations vo = rt.opsForZSet();
out(vo.score("book", "e"));
out(vo.rank("book", "e"));
out(vo.reverseRank("book", "e"));
out(vo.zCard("book"));
out(vo.range("book", 0, -1));
// out(vo.rangeWithScores("book", 0, -1));
out(vo.count("book", 2, 4)); //包含的
out(vo.rangeByLex("book", Range.range().lte("f").gte("b")));
out(vo.rangeByLex("book", Range.range(), Limit.limit().count(2).offset(0)));
out(vo.rangeByLex("book", Range.range(), Limit.limit().count(2).offset(2)));
out(vo.rangeByScore("book", 2, 4));
out(vo.rangeByScore("book", 2, 4, 0, 2));
// out(vo.rangeByScoreWithScores("book", 2, 4));
// out(vo.rangeByScoreWithScores("book", 2, 4, 0, 2));
out(vo.reverseRange("book", 0, -1));
out(vo.reverseRangeByScore("book", 2, 4));
out(vo.reverseRangeByScore("book", 2, 4, 0, 2));
}
public void out(String str){
System.out.println(str);
}
public void out(Collection list){
System.out.println(list);
}
public void out(long num){
System.out.println(num);
}
public void out(boolean num){
System.out.println(num);
}
public void out(DataType num){
System.out.println(num);
}
public void out(Object str){
System.out.println(str);
}
public void out(Set> set){
for(TypedTuple t : set){
System.out.println(t.getValue()+"---"+t.getScore());
}
}
}