title: How to return response from cache if request come with same parameters
body: <p>I am not sure if understand the concept correctly and I am confused.</p>
  <p>I have a rest api that takes single parameter, apply some logic and returns a string. Same input leads to same output so I think returning result values from cache or database would be nice idea.</p>
  <p>Example Input:</p>
  <pre><code>{
  &quot;url&quot;: &quot;ParameterWithId1&quot;
  }
  </code></pre>
  <p>Example Output</p>
  <pre><code>{
  &quot;resultUrl&quot;:&quot;brandNewUrlForId1&quot;
  }
  </code></pre>
  <p>so In spring In my Service I am doing this:</p>
  <pre><code>public LinkResponse toLinkService(LinkRequest LinkRequest) {
  
      WebLink webLink;
      String url = LinkRequest.getUrl();
      webLink = webRepository.getLinkByUrl(url);
      if (webLink != null) {
          return new LinkResponse(webLink.getConvertedUrl());
      }
  
      webLink = new WebLink();
      webLink.setUrl(url) // add to db input url
  
      String resultUrl = converter.toLink(url); // apply some logic
      
      webLink.setConvertedUrl(resultUrl); 
      webRepository.save(webLink); // Save output url to db
  
      return new LinkResponse(resultUrl);
  }
  </code></pre>
  <p>I tried to apply this with cache because I thought checking cache is a better idea than making query to database every time. I think, it should save every request ( <code>url</code> )  and  response ( <code>resultUrl</code> ) to the database and cache. When the field <code>url</code> found in the cache It should not check database and directly return <code>resultUrl</code> from cache.</p>
  <p>That is what I came up with:</p>
  <pre><code>public class ConverterServiceImpl implements ConverterService {
  
      ConverterFactory factory = new ConverterFactory();
      private final WebRepository webRepository;
  
      @Cacheable
      public WebLink getlinkFromCache(String url){
          System.out.println(&quot;Retreiving from database&quot;);
          WebLink weblink = webRepository.getLinkByUrl(url);
          if (weblink != null){
              System.out.println(weblink.getCreatedAt()); 
          }
          return weblink;
      }
  
      @CachePut
      public WebLink saveWebToCache(WebLink webLink){
          System.out.println(&quot;Adding to Database &quot;);
          return webRepository.save(webLink);
      }
  
      public LinkResponse toLink(LinkRequest LinkRequest) {
  
          WebLink webLink;
          String url = LinkRequest.getUrl();
          webLink = webRepository.getLinkByUrl(url);
  
          webLink = new WebLink();
          webLink.setUrl(url) // add to db input url
  
          String resultUrl = converter.toLink(url); // apply some logic
          
          webLink.setConvertedUrl(resultUrl); 
          webRepository.save(webLink); // Save output url to db
  
          return new LinkResponse(resultUrl);
      }
  }
  </code></pre>
  <p>Console is like this:</p>
  <pre><code>Retreiving from database
  Adding to Database 
  Retreiving from database
  2021-10-10 19:28:28.0
  Adding to Database 
  Retreiving from database
  javax.persistence.NonUniqueResultException: query did not return a unique result: 2
      at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:128) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
      at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1659) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
  </code></pre>