25 Ocak 2021 Pazartesi

SpringData Redis Kullanımı

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
Şöyle yaparız
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
Şöyle yaparız
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>
1. SpringData Redis application.properties Ayarları ile bağlantı ayarları belirtilir.
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.
JedisConnectionFactory yazısına bakabilirsiniz

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
Şöyle yaparız
@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);
  }
}
Redis nesnesi şöyledir
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;
}

Hiç yorum yok:

Yorum Gönder