Bir eticaret platformum var. Anasayfada popüler ürünleri gösteriyorum. Bunu her ziyaretçiye gösteriyorum ve cache'e almak istiyorum. Cache olarak redis kullanıyorun. Db tarafında postgresql ve spring data kullanıyorum. Spring boot ile bu senaryoyu kodla



Spring Boot, Redis ve PostgreSQL ile bir e-ticaret platformunda popüler ürünleri cache'leme için bir örnek senaryo ve kod parçaları aşağıdaki gibidir:


Ön Koşullar

Redis ve PostgreSQL'in kurulu ve yapılandırılmış olması gerekir.

Spring Boot projesinde spring-boot-starter-data-redis ve spring-boot-starter-data-jpa bağımlılıklarının eklenmiş olması gerekir.

Adımlar

Redis Konfigürasyonu: Redis bağlantısını yapılandırın.

Entity Sınıfı: PostgreSQL'de saklanan ürünler için bir entity sınıfı oluşturun.

Repository Katmanı: Spring Data JPA ile repository tanımlayın.

Service Katmanı: Ürünleri çekme ve cache'leme işlemleri için bir service yazın.

Controller Katmanı: HTTP isteklerini karşılayacak bir controller oluşturun.

Örnek Kod Parçaları

RedisConfig.java

java

Copy code

@Configuration

@EnableCaching

public class RedisConfig {


    @Bean

    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();

        template.setConnectionFactory(connectionFactory);

        return template;

    }

}

Product.java

java

Copy code

@Entity

public class Product {

    @Id

    private Long id;

    private String name;

    private BigDecimal price;


    // getters ve setters

}

ProductRepository.java

java

Copy code

public interface ProductRepository extends JpaRepository<Product, Long> {

    List<Product> findTop10ByOrderByPopularityDesc();

}

ProductService.java

java

Copy code

@Service

public class ProductService {


    @Autowired

    private ProductRepository productRepository;


    @Autowired

    private RedisTemplate<String, Object> redisTemplate;


    public List<Product> getPopularProducts() {

        String key = "popularProducts";

        List<Product> products = (List<Product>) redisTemplate.opsForValue().get(key);


        if (products == null) {

            products = productRepository.findTop10ByOrderByPopularityDesc();

            redisTemplate.opsForValue().set(key, products, 1, TimeUnit.HOURS);

        }


        return products;

    }

}

ProductController.java

java

Copy code

@RestController

@RequestMapping("/products")

public class ProductController {


    @Autowired

    private ProductService productService;


    @GetMapping("/popular")

    public ResponseEntity<List<Product>> getPopularProducts() {

        return ResponseEntity.ok(productService.getPopularProducts());

    }

}

Notlar

Bu örnek, popüler ürünlerin listesini her saat başı güncelleyerek Redis'te cache'ler.

RedisTemplate kullanılarak nesneler Redis'te saklanır.

ProductRepository sınıfı, popüler ürünleri sorgulamak için kullanılır.

ProductService sınıfı, Redis cache'ini yönetir.

ProductController sınıfı, HTTP isteklerine yanıt verir.

Bu kodlar, projenize uygun şekilde düzenlenmelidir. Veritabanı bağlantı ayarları ve diğer konfigürasyonlar projenizin ihtiyaçlarına göre ayarlanmalıdır.

Hiç yorum yok

Rastgele İçerik

DonanımHaber

© tüm hakları saklıdır
made with by templateszoo