GirişRedis iki farklı amaç için kullanılabilir.
1. SpringCache'in için önbellek olarak. Bu kullanımda Redis ile direkt bir kodlamamız olmuyor.
2. Spring'in sağladığı sınıfları kullanarak Redis'i direkt kullanma.
Bağlantı için Jedis veya Lettuce kullanılabilir.
Örnek - Jedis - Spring Tarafından Varsayılan Kütüphane
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
Örnek - Lettuce
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>letus-core</artifactId>
<version>...</version>
</dependency>
2. İsteniyorsa
RedisConnection ve
RedisConnectionFactory arayüzlerini gerçekleştiren nesneler yaratılır. Bu arayüzler
org.springframework.data.redis.connection paketinde. Açıklaması
şöyle
RedisConnection, which manages communication with the Redis back end, offers the fundamental building block for Redis communication, and automatically converts the underlying connecting library exceptions to Spring’s reliable DAO exception hierarchy.
In order to create RedisConnection you can use the RedisConnectionFactory. The RedisConnectionFactory can be used most simply by configuring the proper connection through the IoC container and injecting it into the target class.
3.
RedisTemplate veya ReactiveRedisTemplate sınıfları kullanılarak opsForX() metodu çağrılır. Döndürülen nesne ile direkt Redis işlemleri yapılabilir.
4. Veya direkt RedisTemplate nesnesini kullanmak yerine Repository yöntemi kullanılır.
Örnek
@RedisHash("Student")
public class Student implements Serializable {
private String id;
private String name;
private Integer age;
}
public interface StudentRepository extends CrudRepository<Student, String> {
}
@Service
public class StudentService {
@Autowired
StudentRepository studentRepository;
public Student addStudent(Student student){
return studentRepository.save(student);
}
public List<Student> getStudents(){
List<Student> list = new ArrayList<Student>();
studentRepository.findAll().forEach( i -> {
list.add(i);
});
return list;
}
}Örnek
Kullanılacak Redis repository nesnelerini belirtmek için şöyle
yaparız@SpringBootApplication
@EnableJpaRepositories(basePackages = "org.medium.jpa")
@EnableRedisRepositories(basePackages = "org.medium.redis",
enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class MediumApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MediumApplication.class);
application.run(args);
}
}
package org.medium.redis;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
@RedisHash(value = "mediumuser")
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class MediumRedisUser implements Serializable {
@Id
Long id;
String name;
}