title: Spring Cache get key from the Value
body: <p>I have used spring cache in a spring boot application to store value against a certain key. I now have the value, is it possible to get the key from cache against the value? If so Please help. </p>
  
  <p>I tried using solutions regarding <code>net.sf.ehcache.Cache</code> but for some reason it doesn't show any import suggestions and gives error <strong>net.sf.ehcache.Cache cannot be resolved to a type</strong>. I am new to spring cache so have no clue what to do further.</p>
  
  <p>The dependencies in my project are</p>
  
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
          &lt;groupId&gt;org.ehcache&lt;/groupId&gt;
          &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
          &lt;groupId&gt;javax.cache&lt;/groupId&gt;
          &lt;artifactId&gt;cache-api&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>The code I am using is </p>
  
  <pre><code>public String getEmailByOtp(String otp)
  {
      String email = "";
      Ehcache cache = (Ehcache) CacheManager.getCache("otpCache").getNativeCache();
      for (Object key: cache.getKeys()) {
          Element element = cache.get(key);
          if (element != null) {
              Object value = element.getObjectValue();     // here is the value
              if(value.equals(otp)) {
                  email = key.toString();
              }
          }
      }
  
      return email;
  
  }
  </code></pre>