concept cache in category microservice

appears as: caches, cache, cache
Spring Microservices in Action

This is an excerpt from Manning's book Spring Microservices in Action.

Use HTTP status codes to communicate the status of a service call—The HTTP protocol has developed a rich set of status codes to indicate the success or failure of a service. REST-based services take advantage of these HTTP status codes and other web-based infrastructure, such as reverse proxies and caches, which can be integrated with your microservices with relative ease.

  • When an organization record changes via an update or delete, you want the licensing service to recognize that there has been a state change in the organization service— The licensing service should then invalidate any cached data it has for that specific organization and evict it from the cache.
  • package com.thoughtmechanix.licenses.clients;
    //Imports removed for conciseness
    @Component
    public class OrganizationRestTemplateClient {
        @Autowired
        RestTemplate restTemplate;
        @Autowired
        OrganizationRedisRepository orgRedisRepo;                                                     #1
        private static final Logger logger = LoggerFactory.getLogger(OrganizationRestTemplateClient.class);
        private Organization checkRedisCache(
           String organizationId) {                                                                   #2
            try {
     return orgRedisRepo.findOrganization(organizationId);
      }
      catch (Exception ex){
     logger.error("Error encountered while trying to retrieve organization {} check Redis Cache.Exception {}", organizationId, ex);
                return null;
      }
    }
    private void cacheOrganizationObject(Organization org) {                     
      try {
     orgRedisRepo.saveOrganization(org);
      }catch (Exception ex){
      logger.error("Unable to cache organization {} in Redis.Exception {}" org.getId(), ex);
      }
    }
    public Organization getOrganization(String organizationId){
      logger.debug("In Licensing Service.getOrganization: {}", UserContext.getCorrelationId());
      Organization org = checkRedisCache(organizationId);
      if (org!=null){                                                                                 #3
        logger.debug("I have successfully retrieved an organization {} from the redis cache: {}", organizationId, org);
        return org;
      }
      logger.debug("Unable to locate organization from the redis cache: {}.", organizationId);
      ResponseEntity<Organization> restExchange =
         restTemplate.exchange(
            "http://zuulservice/api/organization/v1/organizations/{organizationId}",
               HttpMethod.GET,
               null, 
               Organization.class, 
               organizationId);
      /*Save the record from cache*/
      org = restExchange.getBody();
      if (org!=null) {                                                                                #4
        cacheOrganizationObject(org);
      }
      return org;
    }
    }