Commit b1007865 by Antonio Rueda

Añadido cacheado de hoteles por id y localidad

parent 3c3b4c84
...@@ -35,6 +35,11 @@ ...@@ -35,6 +35,11 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId> <groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId> <artifactId>mysql-connector-j</artifactId>
</dependency> </dependency>
......
...@@ -4,6 +4,7 @@ package es.ujaen.dae.reservahoteles.app; ...@@ -4,6 +4,7 @@ package es.ujaen.dae.reservahoteles.app;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
...@@ -16,6 +17,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; ...@@ -16,6 +17,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
}) })
@EntityScan(basePackages="es.ujaen.dae.reservahoteles.entidades") @EntityScan(basePackages="es.ujaen.dae.reservahoteles.entidades")
//@EnableScheduling //@EnableScheduling
@EnableCaching
public class ReservaHoteles { public class ReservaHoteles {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ReservaHoteles.class); SpringApplication.run(ReservaHoteles.class);
......
...@@ -9,6 +9,9 @@ import jakarta.persistence.PersistenceContext; ...@@ -9,6 +9,9 @@ import jakarta.persistence.PersistenceContext;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -23,6 +26,7 @@ public class RepositorioHoteles { ...@@ -23,6 +26,7 @@ public class RepositorioHoteles {
@PersistenceContext @PersistenceContext
EntityManager em; EntityManager em;
@Cacheable("hoteles")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public Optional<Hotel> buscarPorId(int id) { public Optional<Hotel> buscarPorId(int id) {
return Optional.ofNullable(em.find(Hotel.class, id)); return Optional.ofNullable(em.find(Hotel.class, id));
...@@ -32,7 +36,7 @@ public class RepositorioHoteles { ...@@ -32,7 +36,7 @@ public class RepositorioHoteles {
public Optional<Hotel> buscarPorIdBloqueando(int id) { public Optional<Hotel> buscarPorIdBloqueando(int id) {
return Optional.ofNullable(em.find(Hotel.class, id, LockModeType.PESSIMISTIC_WRITE)); return Optional.ofNullable(em.find(Hotel.class, id, LockModeType.PESSIMISTIC_WRITE));
} }
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public List<Hotel> buscarPorNombreLocalidad(String nombre, String localidad) { public List<Hotel> buscarPorNombreLocalidad(String nombre, String localidad) {
return em.createQuery("select h from Hotel h where " + return em.createQuery("select h from Hotel h where " +
...@@ -41,7 +45,8 @@ public class RepositorioHoteles { ...@@ -41,7 +45,8 @@ public class RepositorioHoteles {
.setParameter(2, "%" + normalizar(localidad) + "%") .setParameter(2, "%" + normalizar(localidad) + "%")
.getResultList(); .getResultList();
} }
@Cacheable(value="hotelesPorLocalidad", key="T(es.ujaen.dae.reservahoteles.util.UtilString).normalizar(#localidad)")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public List<Hotel> buscarPorLocalidad(String localidad) { public List<Hotel> buscarPorLocalidad(String localidad) {
return em.createQuery("select h from Hotel h where " + return em.createQuery("select h from Hotel h where " +
...@@ -60,6 +65,10 @@ public class RepositorioHoteles { ...@@ -60,6 +65,10 @@ public class RepositorioHoteles {
em.persist(hotel); em.persist(hotel);
} }
@Caching(evict={
@CacheEvict(value="hoteles", key="#hotel.id()"),
@CacheEvict(value="hotelesPorLocalidad", key="T(es.ujaen.dae.reservahoteles.util.UtilString).normalizar(#hotel.localidad())")
})
public Hotel actualizar(Hotel hotel) { public Hotel actualizar(Hotel hotel) {
return em.merge(hotel); return em.merge(hotel);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment