vendredi 4 décembre 2020

Architectural design of Spring Redis and external API

I'm wondering how to design a service which will provide data about the nearest store. Below are my considerations.

--Service Layer--
@Service
class ShopService {
  Shop getShop(String criteria);
}

interface ShopAdapter {
  Shop getShop(String criteria);
}

--Integration Layer--

@Component
class ExternalApiIntegration implements ShopAdapter {
  Shop getShop(String criteria);
}

--Domain Layer--

interface ShopRepository extends CrudRepository<Shop, Integer> {
  Optonal<Shop> findNearestShop(String criteria); 
  // Probably I will have to use RedisTemplate instead of Crud to get this data.
  // CrudRepository for Redis is strong limited in sorting and filtering
}

@Component
class CacheShopAdapter implements ShopAdapter {
  Shop getShop(String criteria) {
      Optonal<Shop> shop = ShopRepository.findNearestShop();
      return shop.orElseGet(() -> {
        Shop shop = externalApiIntegration.getShop(criteria); //It doesn't look good and conflict with bean injection.
        return shopRepository.save(shop);
      });
  }
}
 

What do you think? Probably I can't use @Cachable annotation to this case because cache should also return similar data to given criteria which based inter alia GPS coordinates. In the example given, I don't like that CacheShopAdapater which implements inject other implementation of this interface.

Aucun commentaire:

Enregistrer un commentaire