Redis, genellikle önbellekleme, mesajlaşma ve gerçek zamanlı veri yönetimi için kullanılan güçlü bir bellek içi veri deposudur. Bu kılavuz, Redis'i hem Java hem de Kotlin kullanarak bir Spring Boot uygulamasına nasıl entegre edeceğinizi açıklar.
Yazan
Şuayb Şimşek
Spring Boot, güvenlik, mikroservis ve cloud-native mimari konularında pratik teknik notlar paylaşan backend odaklı fullstack geliştirici.
Parola doğrulama veya SSL gibi ileri düzey kurulumlar için şunları ekleyin:
PROPERTIESapplication.properties
spring.redis.password=sifre
spring.redis.ssl=true
🛠️ Adım 3: Önbelleği Etkinleştirin
Ana uygulama sınıfınıza @EnableCaching ekleyin:
Servis Katmanında Önbellekleme Örneği
Aşağıdaki örnek, Servis Katmanında Önbellekleme Örneği için pratik bir bağlam sunar ve doğrudan uygulanabilir.
Önbellek Zaman Aşımı (TTL) Yapılandırması
application.properties dosyasında önbellek sona erme sürelerini ayarlayın:
PROPERTIESapplication.properties
spring.cache.redis.time-to-live=600000
Bu, zaman aşımını 10 dakika (600.000 milisaniye) olarak ayarlar.
Controller Örneği
Önbellekleme işlevini expose etmek için bir REST Controller oluşturun.
▶️ Uygulamayı Çalıştırma
Uygulamayı aşağıdaki komutla çalıştırın:
BASH
./mvnw spring-boot:run
🧪 API’yı Test Etme
API’yı cURL veya Postman kullanarak test edebilirsiniz:
Bir Kullanıcıyı Getir (önbellekli):
BASH
curl -X GET http://localhost:8080/users/1
🏁 Sonuç
Artık Redis Önbellekleme için üretim odaklı bir Spring Boot temeliniz var. Sonraki adımda ayarları kendi domainine uyarlayıp test ve gözlemlenebilirlik katmanını ekleyerek gerçek trafik altında doğrulayın.
JAVARedisApplication.java
package com.example.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
}
KOTLINRedisApplication.kt
package com.example.redis
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching
@SpringBootApplication
@EnableCaching
class RedisApplication
fun main(args: Array<String>) {
runApplication<RedisApplication>(*args)
}
JAVAUserService.java
package com.example.redis.service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Cacheable("users")
public String getUserById(String id) {
simulateSlowService();
return "User with ID: " + id;
}
private void simulateSlowService() {
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
KOTLINUserService.kt
package com.example.redis.service
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
@Service
class UserService {
@Cacheable("users")
fun getUserById(id: String): String {
simulateSlowService()
return "User with ID: $id"
}
private fun simulateSlowService() {
Thread.sleep(3000L)
}
}
JAVAUserController.java
package com.example.redis.controller;
import com.example.redis.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
@GetMapping("/{id}")
public String getUser(@PathVariable String id) {
return userService.getUserById(id);
}
}
KOTLINUserController.kt
package com.example.redis.controller
import com.example.redis.service.UserService
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/users")
class UserController(
private val userService: UserService
) {
@GetMapping("/{id}")
fun getUser(@PathVariable id: String): String = userService.getUserById(id)
}