在redis客户端可以使用zpopmax
redisTemplate不支持zpopmax
解决方案
使用reverseRangeWithScore
接下来我们进行测试
我们要返回最大的value,应该是c
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Set;
@SpringBootTest
@RunWith(SpringRunner.class)
public class TEST1 {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void Data(){
// redisTemplate.opsForZSet().add("goods","a",10);
// redisTemplate.opsForZSet().add("goods","b",10);
// redisTemplate.opsForZSet().add("goods","c",11);
String goods = zPopMaxFromSet("goods");
System.out.println(goods);
}
public String zPopMaxFromSet(String setName) {
ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
Set<ZSetOperations.TypedTuple<String>> maxElements = zSetOperations.reverseRangeWithScores(setName, 0, 0);
if (!maxElements.isEmpty()) {
ZSetOperations.TypedTuple<String> maxElement = maxElements.iterator().next();
//获取值
String element = maxElement.getValue();
//获取得分
Double score = maxElement.getScore();
zSetOperations.remove(setName, element);
return element;
}
return null; // 集合为空
}
}
有人想问了zSetOperations.reverseRangeWithScores(setName, 0, 0); 0,0代表什么
开始索引位置和结束位置
测试
@ApiOperation("测试")
@GetMapping("/test/{start}/{end}")
public List<String> testRedis(@PathVariable Integer start, @PathVariable Integer end){
ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
Set<ZSetOperations.TypedTuple<String>> maxElements = zSetOperations.reverseRangeWithScores("goods", start,end );
Iterator<ZSetOperations.TypedTuple<String>> iterator = maxElements.iterator();
ArrayList<String> list = new ArrayList<>();
while(iterator.hasNext()){
list.add(iterator.next().getValue());
}
return list; // 集合为空
}
记得序列化
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
public class RedisConfig {
/**
* @param lettuceConnectionFactory
* @return
*
* redis序列化的工具配置类,下面这个请一定开启配置
* 127.0.0.1:6379> keys *
* 1) "ord:102" 序列化过
* 2) "\xac\xed\x00\x05t\x00\aord:102" 野生,没有序列化过
*/
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//设置key序列化方式string
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置value的序列化方式json
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}