Redisは小さなゲームを実現します.同僚の前でXを装うようにします.
31979 ワード
Redisは小さなゲームを実現
本編を見る前にspringbootプロジェクト統合Swagger-UI実装オンラインAPIドキュメントを見ることができます
本編を見る前にspringbootプロジェクト統合Swagger-UI実装オンラインAPIドキュメントを見ることができます
package com.macro.mall.grade.controller;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.grade.domain.SucessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Set;
/**
* @Author: liuxiang
* @Date: 2020/6/4
* @Description:
*/
@RestController
@Api(tags = "WordController", description = " ")
@RequestMapping("/word")
public class WordController {
private static String[] word = null;
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
*
* @param userId
* @return
*/
@ApiOperation(" ")
@RequestMapping(value = "/putWord", method = RequestMethod.GET)
@ResponseBody
public CommonResult putWord(@RequestParam String userId) {
String userPerfix = "user:word:" + userId;
// hashKey
BoundHashOperations<String, Object, Object> stringObjectObjectBoundHashOperations = stringRedisTemplate.boundHashOps(userPerfix);
//
for (int i = 0; i < 100; i++) {
stringObjectObjectBoundHashOperations.put(String.valueOf(i), " ");
}
return CommonResult.success("ok");
}
/**
*
* @param userId
* @param id
* @return
*/
@ApiOperation(" ")
@RequestMapping(value = "/getWord", method = RequestMethod.GET)
@ResponseBody
public CommonResult getWord(@NotNull @RequestParam String userId, @NotNull @RequestParam String[] id) {
//
ArrayList<String> strings1 = getUserWord(userId, id);
// id
word = id;
//
ZSetOperations<String, String> stringStringZSetOperations = stringRedisTemplate.opsForZSet();
//
stringStringZSetOperations.incrementScore("userScore", userId, id.length);
return strings1 == null ? CommonResult.failed(" ") : CommonResult.success(strings1);
}
/**
*
*
* @param userId
* @param id
* @return
*/
private ArrayList<String> getUserWord(@NotNull @RequestParam String userId, @NotNull @RequestParam @NotNull String[] id) {
String userPerfix = "user:word:" + userId;
BoundHashOperations<String, Object, Object> stringObjectObjectBoundHashOperations = stringRedisTemplate.boundHashOps(userPerfix);
ArrayList<String> strings1 = new ArrayList<>();
for (String i : id) {
Object o1 = stringObjectObjectBoundHashOperations.get(i);
strings1.add((String) o1);
}
return strings1;
}
/**
*
* @param userId
* @return
*/
@ApiOperation(" ")
@RequestMapping(value = "/success", method = RequestMethod.GET)
@ResponseBody
public CommonResult<SucessResult> success(@NotNull @RequestParam String userId) {
String userPerfix = "user:word:" + userId;
BoundHashOperations<String, Object, Object> stringObjectObjectBoundHashOperations = stringRedisTemplate.boundHashOps(userPerfix);
//
ArrayList<String> strings1 = new ArrayList<>();
for (String i : word) {
Object o1 = stringObjectObjectBoundHashOperations.get(i);
strings1.add((String) o1);
}
SucessResult sucessResult = new SucessResult();
sucessResult.setStrings1(strings1);
//
stringObjectObjectBoundHashOperations.delete(word);
//
Double userScore = stringRedisTemplate.opsForZSet().score("userScore", userId);
if (userScore == null) {
Set<String> userScore1 = stringRedisTemplate.opsForZSet().reverseRange("userScore", 0, -1);
sucessResult.setUserScore1(userScore1);
return CommonResult.success(sucessResult);
}
Set<String> userScore1 = stringRedisTemplate.opsForZSet().reverseRange("userScore", 0, userScore.longValue());
sucessResult.setUserScore1(userScore1);
//
word = null;
return CommonResult.success(sucessResult);
}
}
package com.macro.mall.grade.domain;
import lombok.Data;
import java.util.ArrayList;
import java.util.Set;
/**
* @Author: liuxiang
* @Date: 2020/6/5
* @Description:
*/
@Data
public class SucessResult {
private Set<String> userScore1;
private ArrayList<String> strings1;
}
package com.macro.mall.grade.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
```java
/**
* Swagger2API
* Created by macro on 2018/4/26.
*/
@Configuration
@EnableSwagger2
public class GradeSwaggerConfig {
@Bean
public Docket createRestSeckillApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.grade.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("mall ")
.description("mall ")
.version("1.0")
.build();
}
}