博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Redis - 01入门

Posted on 2021-01-10 15:48  Kingdomer  阅读(83)  评论(0)    收藏  举报

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");
    }