/** * LoginController.java * com.ctb.smart.user.controller * Copyright (c) 2018, IT版權所有. * @author IT_BULL */ package com.ctb.smart.user.controller; import java.util.ArrayList; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.data.redis.core.ZSetOperations.TypedTuple; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.ctb.msa.common.base.result.ResultMap; import com.ctb.msa.common.swagger.SwaggerOuter; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; /** * TODO(這里用一句話描述這個類的作用) * <p> * TODO(這里描述這個類補充說明 – 可選) * @author IT_BULL * @Date 2018年12月24日 */ @RestController @RequestMapping("/login") @Api(tags = "[/login]", description = "用戶信息表") @SwaggerOuter @Validated public class LoginController { @Autowired private StringRedisTemplate stringRedisTemplate; @ApiOperation(value = "登錄接口_1.0.0【可對接】") @SwaggerOuter @ApiImplicitParams({ }) @RequestMapping(value = "/login", headers = "api-version=1.0.0", method = RequestMethod.GET) public ResultMap<Object> login( ) { String key = "user_01"; String dev_id = "huawei_00003"; //獲取對象實例 ZSetOperations<String, String> opsForZSet = stringRedisTemplate.opsForZSet(); //添加 // opsForZSet.add(key, dev_id, System.currentTimeMillis()); //如果key一樣就進行時間刷新 opsForZSet.add(key, dev_id, 9528); Set<TypedTuple<String>> reverseRangeWithScores = opsForZSet.reverseRangeWithScores(key, 0, -1); ArrayList<String> arrayList = new ArrayList<String>(); ArrayList<Double> arrayList2 = new ArrayList<Double>(); for (TypedTuple<String> typedTuple : reverseRangeWithScores) { String value = typedTuple.getValue(); Double score = typedTuple.getScore(); System.out.println("value---->"+value); System.out.println("score---->"+score); arrayList.add(value); arrayList2.add(score); } System.out.println("======>"+arrayList.get(0)); return ResultMap.success(reverseRangeWithScores); } }
運行結果:
19:27:41.111 [http-nio-9999-exec-2] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
19:27:41.112 [http-nio-9999-exec-2] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started
19:27:41.126 [http-nio-9999-exec-2] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 14 ms
value---->huawei_00004
score---->1.545649558691E12
value---->huawei_00002
score---->1.545644129584E12
value---->huawei_00001
score---->1.545643703471E12
value---->huawei_00003
score---->9528.0
======>huawei_00004
接口返回值:
{ "errCode": "0", "errMsg": "OK", "data": [ { "score": "1.545649558691E12", "value": "huawei_00004" }, { "score": "1.545644129584E12", "value": "huawei_00002" }, { "score": "1.545643703471E12", "value": "huawei_00001" }, { "score": "9528.0", "value": "huawei_00003" } ] }