Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

52.2. 通过构造方法实例化 Redis

			
package cn.netkiller.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.security.Principal;
import java.util.*;
import java.util.stream.IntStream;

@RestController
@Slf4j
@RequestMapping("/test")
@Scope(value = "prototype")
public class TestController {

    private final RedisTemplate redisTemplate;

    public TestController(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @GetMapping("cache/set")
    public void save(String key, String value) {
        redisTemplate.opsForValue().set("name", "Neo");

    }

    @GetMapping("/cache/get")
    public Mono<String> cache() {
        String name = (String) redisTemplate.opsForValue().get("name");
        log.info(name);
        return Mono.justOrEmpty(name);
    }
}