refactor: 按用户缓存
This commit is contained in:
parent
cdfdb4b0ce
commit
a50007a526
|
|
@ -1,14 +1,16 @@
|
||||||
package com.nflg.product.bomnew.api.user;
|
package com.nflg.product.bomnew.api.user;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.nflg.product.base.core.api.BaseApi;
|
import com.nflg.product.base.core.api.BaseApi;
|
||||||
|
import com.nflg.product.base.core.conmon.util.SessionUtil;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import nflg.product.common.vo.ResultVO;
|
import nflg.product.common.vo.ResultVO;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -22,26 +24,46 @@ public class CacheApi extends BaseApi {
|
||||||
|
|
||||||
private static final String PREFIX = "frontend:";
|
private static final String PREFIX = "frontend:";
|
||||||
|
|
||||||
@Autowired
|
@Resource
|
||||||
private RedisTemplate<String, String> redisTemplate;
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置用户缓存
|
||||||
|
* @param key 缓存key
|
||||||
|
* @param content 缓存内容
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@PostMapping("set")
|
@PostMapping("set")
|
||||||
@ApiOperation("设置缓存")
|
@ApiOperation("设置缓存")
|
||||||
public ResultVO setCache(@Valid @RequestParam @NonNull String key, @Valid @RequestParam @NonNull String content) {
|
public ResultVO<String> setCache(@Valid @RequestParam @NonNull String key, @Valid @RequestParam @NonNull String content) {
|
||||||
redisTemplate.boundValueOps(PREFIX + key).set(content);
|
redisTemplate.boundValueOps(buildKey(key)).set(content);
|
||||||
return ResultVO.success();
|
return ResultVO.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户缓存
|
||||||
|
* @param key 缓存key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@GetMapping("get")
|
@GetMapping("get")
|
||||||
@ApiOperation("获取缓存")
|
@ApiOperation("获取缓存")
|
||||||
public ResultVO getCache(@Valid @RequestParam @NonNull String key) {
|
public ResultVO<String> getCache(@Valid @RequestParam @NonNull String key) {
|
||||||
return ResultVO.success(redisTemplate.boundValueOps(PREFIX + key).get());
|
return ResultVO.success(redisTemplate.boundValueOps(buildKey(key)).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("del")
|
/**
|
||||||
|
* 删除用户缓存
|
||||||
|
* @param key 缓存key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@DeleteMapping("del")
|
||||||
@ApiOperation("删除缓存")
|
@ApiOperation("删除缓存")
|
||||||
public ResultVO delCache(@Valid @RequestParam @NonNull String key) {
|
public ResultVO<String> delCache(@Valid @RequestParam @NonNull String key) {
|
||||||
redisTemplate.delete(PREFIX + key);
|
redisTemplate.delete(buildKey(key));
|
||||||
return ResultVO.success();
|
return ResultVO.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String buildKey(String key) {
|
||||||
|
return StrUtil.format("{}:{}:{}", PREFIX, SessionUtil.getUserCode(), key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue