Redis详解及Spring Boot 整合 Redis

    技术2022-07-10  137

    Redis

    基于内存进行存储,支持key-value的存储形式,底层是C 基于key-value形式的数据字典,没有数据表的概念,直接用键值对的形式完成数据管理 支持五种数据类型: ·字符串 ·列表 ·集合 ·有序集合 ·哈希

    安装Redis

    1.下载Redis:https://redis.io/download 2.解压并在本地硬盘人任意位置创建文件夹redis,再创建三个子文件夹: ·bin 放置启动Redis的可执行文件 ·db 放置数据文件 ·etc 放置配置文件,设置Redis端口、日志文件位置、数据文件位置

    启动Redis服务 1.进入Redis目录,启动redis-server sudo ./bin/redis-server./etc/redis.config 2.启动redis-cli ./bin/redis-cli 3.对数据进行操作 set key value get key 4.关闭Redis服务 shutdown 5.退出客户端 control+c

    Spring Boot 整合 Redis

    Spring Data Redis 操作 Redis. 1.创建maven工程,再pome.xml中配置 org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE

    <dependencies> <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> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> 2.创建实体类,实现序列化接口,否则无法存入Redis数据库。 package com.tianchen.entity; import java.util.Date; import java.io.Serializable; import lombok.Data; @Data public class Student implements Serializable{ private Integer id; private String name; private Double score; private Date birthday; } 3.创建控制器 package com.tianchen.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import com.tianchen.entity.Student; //import org.springframework.web.bind.annotation.PostMapping; //import org.springframework.web.bind.annotation.RequestBody; //import org.springframework.web.bind.annotation.RestController; @RestController public class StudentHandler { @Autowired private RedisTemplate redisTemplate; @PostMapping("/set") public void set(@RequestBody Student student) { redisTemplate.opsForValue().set("student",student); } @GetMapping("/get/{key}") public Student get(@PathVariable("key") String key) { return (Student)redisTemplate.opsForValue().get(key); } @DeleteMapping("/delete/{key}") public boolean delete(@PathVariable("key") String key) { redisTemplate.delete(key); return redisTemplate.hasKey(key); } } 4.创建配置文件application.yml spring: redis: database: 0 host: localhost port: 6379 5.创建启动类 package com.tianchen; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }

    Redis 5 种数据类型 ·字符串 @GetMapping("/string") public String StringTest() { redisTemplate.opsForValue().set(“str”,“Hello World”); String str = (String)redisTemplate.opsForValue().get(“str”); return str; }

    ·列表 @GetMapping("/list") public List<String> ListTest() { ListOperations<String,String> 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; } ·集合 //如下只会存入Hello World Java 各一个 @GetMapping("/set") public Set<String> setTest(){ 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; } ·有序集合 @GetMapping("/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; } ·哈希 HashMap key value HashOperations key hashkey value key 是每一组数据的 ID,hashkey 和 value 是一组完整的 HashMap 数据,通过key来区分不同的HashMap 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<String,String,String> hashOperations = ridisTemplate.opsForHash(); hashOperations.put(hashMap1,key1,value1); hashOperations.put(hashMap2,key2,value2); hashOperations.put(hashMap2,key2,value2); @GetMapping("/hash") public void hashTest() { HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash(); hashOperations.put("key", "hashkey", "Hello"); System.out.println(hashOperations.get("key","hashkey")); }
    Processed: 0.012, SQL: 9