Redis với Khởi động mùa xuân
Trong bài đăng này, chúng ta sẽ thảo luận về Redis , Redis là cơ sở dữ liệu NoSQL dựa trên giá trị khóa có thể được sử dụng cho nhiều mục đích. Nó là mã nguồn mở và nó là một kho lưu trữ cấu trúc dữ liệu InMemory. Bởi vì Redis hỗ trợ tất cả các cấu trúc dữ liệu cơ bản như danh sách, tập hợp, bản đồ và tập hợp đã sắp xếp.
Sử dụng Redis, chúng tôi có thể phát triển nhiều loại ứng dụng khác nhau như ứng dụng trò chuyện, ứng dụng lưu trữ phiên, bảng điều khiển trò chơi, v.v.
Nếu bạn muốn tìm hiểu Redis thì trước tiên hãy hiểu về các cấu trúc dữ liệu khác nhau mà Redis hỗ trợ ngoài các lệnh sử dụng mà chúng ta có thể lưu trữ và lấy dữ liệu từ Redis.
Một số lệnh quan trọng
Redis hỗ trợ Chuỗi, Tập hợp, Tập hợp được sắp xếp, Danh sách và HashMap dưới dạng khóa — giá trị. Dưới đây là một số lệnh cơ bản để hiểu về các cấu trúc dữ liệu khác nhau.
String
SET foo bar (foo is key, bar is value)
GET foo
bar
HashMap
HMSET student name: "shubham" age: 25
HGETALL student
List
LPUSH product car
LPUSH product bike
LRANGE product 0 10
Set
SADD product car
SADD product bike
SMEMBERS product
Các trường hợp sử dụng của Redis
- Redis có thể được sử dụng làm cơ sở dữ liệu NoSQL dựa trên khóa-giá trị.
- Redis có thể được sử dụng làm nhà cung cấp bộ đệm.
- Redis có thể được sử dụng làm nhà xuất bản và người đăng ký, được sử dụng trong xử lý sự kiện.
- Redis có thể được sử dụng như một cửa hàng phiên.
- Redis có thể được sử dụng trong các ứng dụng trò chuyện.
Vì vậy, nếu bạn cảm thấy hứng thú sau khi đọc phần này và bây giờ bạn muốn thực hành một số thao tác thực hành trên Redis thì hãy thiết lập máy chủ Redis trong hệ thống của bạn.
- Cài đặt homebrew trong hệ thống của bạn nếu không có.
- Các lệnh Run bên dưới tuần tự
brew install redis
After succesfull installation
brew services start redis
After starting redis if you want to try above commands
redis-cli
To test whether redis server is working
PING
If yu get PONG then its connected
If you want to monitor which all commands are getting executed on redis
redis-monitor
Vì vậy, sau này, nếu bạn muốn sử dụng Redis từ một số hệ thống khác thì bạn cần một máy khách sẽ tương tác với máy chủ Redis.
Bây giờ trong phần này, chúng ta sẽ xem cách chúng ta có thể sử dụng Redis với spring boot và sử dụng nó như một NoSQL DB.
Thành lập
Bước đầu tiên là tạo một dự án khởi động Spring mẫu từ trình khởi tạo mùa xuân và thêm phần phụ thuộc spring-boot-starter-data-redis .
Bây giờ, sau khi nhập dự án vào IDE yêu thích của bạn, hãy tạo các gói và lớp.
Lớp đầu tiên sẽ dành cho cấu hình, Chúng tôi có hai trình điều khiển cho Redis, một là Jedis, một là rau diếp (Nó được khởi tạo một cách lười biếng và hiệu suất cũng tốt hơn.)
Ngoài trình điều khiển, chúng tôi cần một mẫu để thực hiện các thao tác trên máy chủ Redis, giống như cách chúng tôi có RestTemplate để thực hiện các thao tác nghỉ ngơi.
package com.redisexample.redisdemo.config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
//@EnableRedisRepositories
public class AppConfig {
//There are two ways of creating driver, one is jedis another is lettuce,
//lettuce is bit lazy in intialization so it creates beans lazily
//It is using default host and port
@Bean
RedisConnectionFactory jedisConnectionFactory() {
return new LettuceConnectionFactory();
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
//For setting host and port
// @Bean
// JedisConnectionFactory jedisConnectionFactory() {
// JedisConnectionFactory jedisConFactory
// = new JedisConnectionFactory();
// jedisConFactory.setHostName("localhost");
// jedisConFactory.setPort(6379);
// return jedisConFactory;
// }
}
Tạo một lớp Thực thể
@RedisHash("student") // this is a set so we can use set command to see data via redis cli
public class Student {
public enum Gender {
MALE, FEMALE
}
private Long id;
private String name;
private int age;
private String city;
//getters and setters
}
@Repository
public interface StudnetRepo extends CrudRepository<Student, Long> {
}
package com.redisexample.redisdemo.controller;
import com.redisexample.redisdemo.model.Student;
import com.redisexample.redisdemo.repo.StudnetRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudnetRepo studnetRepo;
@PostMapping
public void saveStudent(@RequestBody Student student){
studnetRepo.save(student);
}
@GetMapping
public Iterable<Student> getStudent(){
return studnetRepo.findAll();
}
}
Vì vậy, đây là một ví dụ đơn giản về việc sử dụng Redis làm DB với ứng dụng khởi động mùa xuân.
Trong bài đăng tiếp theo, chúng ta sẽ xem cách sử dụng Redis làm Nhà cung cấp bộ đệm.
Cảm ơn vì đã đọc!!