1. 程式人生 > 實用技巧 >Redis - 01入門

Redis - 01入門

Redis - 01入門

(1)專案

POM檔案

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

application.properties

spring.redis.database=0
spring.redis.host=192.168.100.86
spring.redis.port=6379

Student類

@Data
public class Student implements Serializable {
    private String id;
    
private String name; private Integer age; private Date birthday; private Double score; }

(2)Redis五種資料型別

(2.1)常用操作:set/get/delte redisTemplate.opsForValue()

@RestController
@RequestMapping("/redis")
public class StudentHandler {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping(
"/set") public void set(@RequestBody Student student){ redisTemplate.opsForValue().set("student",student); } }

    @RequestMapping("/get/{key}")
    public Student get(@PathVariable("key") String key){
        return (Student) redisTemplate.opsForValue().get(key);   // ValueOperations
    }

    @DeleteMapping("/delete/{key}")
    public boolean delete(@PathVariable("key") String key){
        redisTemplate.delete(key);
        return redisTemplate.hasKey(key);
    }

(2.2)字串

    @GetMapping("/string")
    public String stringTest(){
        redisTemplate.opsForValue().set("str","hello world");
        String str = (String) redisTemplate.opsForValue().get("str");
        return str;
    }

(2.3)List列表

    @GetMapping("/list")
    public List<String> listTest(){
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list","Hello");
        listOperations.leftPush("list","World");
        listOperations.leftPush("list","Java");
        List<String> list = listOperations.range("list",0,2);
        return list;
    }

(2.4)Set集合

    @RequestMapping("/set")
    public Set<String> setTest(){
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set","hello");
        setOperations.add("set","hello");
        setOperations.add("set","hello");
        setOperations.add("set","World");
        setOperations.add("set","World");
        setOperations.add("set","java");
        setOperations.add("set","Java");
        Set<String> set = setOperations.members("set");
        return set;
    }

(2.5)Zset有序集合

    @RequestMapping("/zset")
    public Set<String> zsetTest(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset","Hello",1);
        zSetOperations.add("zset","World",2);
        zSetOperations.add("zset","Java",3);
        Set<String> zset = zSetOperations.range("zset",0,2);
        return zset;
    }

(2.6)Hash雜湊表

    @GetMapping("/hash")
    public String hashTest(){
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        HashMap hashMap1 = new HashMap();
        hashMap1.put("key1","value1");
        HashMap hashMap2 = new HashMap();
        hashMap2.put("key2","value2");
        HashMap hashMap3 = new HashMap();
        hashMap3.put("key3","value3");
        hashOperations.put("hash","key1","value1");
        hashOperations.put("hash","key2","value2");
        hashOperations.put("hash","key3","value3");
        return hashOperations.get("hash","key1");
    }