首页 > 其他 > 详细

redis简单使用

时间:2020-02-26 21:06:06      阅读:69      评论:0      收藏:0      [点我收藏+]

springboot简单整合redis

windows地址下载

https://github.com/MicrosoftArchive/redis/tags

redis基本语法

set key value
get key
    value

引入依赖

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

redis在springboot中的默认配置

spring:
  redis:
    database: 0
    host: localhost
    port: 6379

redis大型代码crud操作

@RestController
public class StudentController {

    /**
     * redis模板 自动创建 注入即可
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 存入
     * @param student
     */
    @PostMapping("/set")
    public void set(@RequestBody Student student) {

        redisTemplate.opsForValue().set("student", student);
        System.out.println(redisTemplate.opsForValue().get("student"));
    }

    /**
     * 查询
     * @param key
     * @return
     */
    @GetMapping("/get/{key}")
    public Student get(@PathVariable("key") String key) {

        return (Student) redisTemplate.opsForValue().get(key);
    }

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

    /**
     * 字符串
     * @return
     */
    @GetMapping("/string")
    public String stringTest() {
        HashMap<String, Student> map = new HashMap<>();
        Student student = new Student();
        student.setId(1);
        student.setName("shirly");
        map.put("student", student);
        redisTemplate.opsForValue().set("str", map.toString());
        redisTemplate.opsForValue().set("str1", "hello world!");
        String str = (String) redisTemplate.opsForValue().get("str");
        return str;
    }

    /**
     * list集合
     * @return
     */
    @GetMapping("/list")
    public List<String> list() {

        ListOperations<String, String> opsForList = redisTemplate.opsForList();
        opsForList.leftPush("list", "hello");
        opsForList.leftPush("list", "world");
        opsForList.leftPush("list", "java");
        /**
         * 从左面往里push
         * 从第0个位置到第1个位置
         * 取出的值为 java与world
         */
        List<String> list = opsForList.range("list", 0, 1);

        return list;
    }


    /**
     * 无序set
     * @return
     */
    @GetMapping("/set")
    public Set<String> set() {
        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
        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;
    }

    /**
     * 有序set
     * @return
     */
    @GetMapping("/zset")
    public Set<String> zset() {
        ZSetOperations<String, String> zsetOperations =redisTemplate.opsForZSet();
        zsetOperations.add("zset", "hello",1);
        zsetOperations.add("zset", "world",2);
        zsetOperations.add("zset", "java",3);
        Set<String> set = zsetOperations.range("zset",0,1);

        return set;
    }

    /**
     * hash存储
     * @return
     */
    @GetMapping("/hash")
    public String hash() {

        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        /**
         * 可以理解为Map<String,Map<String,String>>
         */
        hashOperations.put("key","hashkey", "hello");
        String s = hashOperations.get("key", "hashkey");//取出值为 hello

        return s;
    }

}

redis常用存取

@PostMapping("/set")
public void set(@RequestBody Student student) {

    redisTemplate.opsForValue().set("student", student);
    System.out.println(redisTemplate.opsForValue().get("student"));
}

前端json数据

{
    "id":99,
    "name":"david",
    "score":96.1,
    "birthday":"1996-01-01"
}

redis简单使用

原文:https://www.cnblogs.com/hitenine/p/12368375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!