title: Cache memory not saving values in cache when running the project
body: <p>I am implementing cache system in maven project.I am not using spring or any framework.The concept is to :</p>
  
  <pre><code> 1.At first user will hit the API and Check whether the data is available in cache or not.
   2.If cache is empty ,call the database and put values in cache and send them back.
   3.If cache is not empty, then return the values from cache.
  </code></pre>
  
  <p>So,for proper organization,I used the Java caching System(JCS) library,which I took reference from:</p>
  
  <blockquote>
    <p><a href="https://www.javaworld.com/article/2077936/open-source-java-projects-java-caching-system.html" rel="nofollow noreferrer">https://www.javaworld.com/article/2077936/open-source-java-projects-java-caching-system.html</a></p>
  </blockquote>
  
  <p>So,I need to have <strong>cache.ccf</strong> file which will have all configuration for cache memory.</p>
  
  <p>The logic I used in my service class is.This method will be called from API call:</p>
  
  <pre><code> public List&lt;Album&gt; getAllMusic() {
          try
               {
               Properties props = new Properties();
              props.load(new FileInputStream("src/main/resources/log4j.properties"));
              PropertyConfigurator.configure(props); 
  
         List&lt;Album&gt; albumList = (List) cache.get("musicCache");
       System.out.println("Size of album is"+albumList.size());
          if(albumList!=null) {
              System.out.println("Returning data from cache");
            return albumList;
             }
       } catch(Exception e ){
  
               try{
                   System.out.println("Putting data in cache");
                   musicDao.putIncache();
               }
               catch(Exception ef){
  
               }
               return musicDao.getAllTopHundredMusic();
       }
  
          return null;
  
      }
  </code></pre>
  
  <p>At,first I will check the cache using:  </p>
  
  <pre><code>List&lt;Album&gt; albumList = (List) cache.get("musicCache");
  </code></pre>
  
  <p>and in the first instance it will be always empty,so I will call the DAO layer and also put the datas in the cache in same time which is:</p>
  
  <pre><code>System.out.println("Putting data in cache");
                   musicDao.putIncache();
  </code></pre>
  
  <p>So,the data gets loaded in the cache,but again when i hit the URL,the cache is still null and it again hits the DAO layer(as in 2nd time hit,it needs to get data from cache not from DAO).I try to check my cache size,but its always empty.When i put the data in cache,its being added in cache but when I try to retrieve it,it is always empty. The logic to put all the data in cache is in DAO layer is:</p>
  
  <pre><code>public void putIncache() {
          try {
  
  
              CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
              Properties props = new Properties();
  
              props.put("jcs.default","DC");
              props.put("jcs.default.cacheattributes",
                      "org.apache.jcs.engine.CompositeCacheAttributes");
  // lots more props.put - this is basically the contents of cache.ccf
              Logger log
                      = Logger.getLogger(MusicDao.class);
  
              ccm.configure(props);
  
              // Load the cache
              cache = JCS.getInstance("musicCache");
  
  
              int i=0;
              for(Album a:albumList){
                  cache.put(new Integer(i+1), a);
  
              }
  
  
          } catch (CacheException e) {
              System.out.println("here");
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>I am putting my full codes now:
  my model class is:</p>
  
  <pre><code>package com.ashwin.cacheapp.model;
  
  import java.io.Serializable;
  
  public class Album implements Serializable {
      private Integer id;
      private String artist;
      private String title;
  
      public Album() {
      }
  
      public Album( Integer id, String artist, String title ) {
          this.id = id;
          this.artist = artist;
          this.title = title;
      }
  
      public Integer getId() {
          return id;
      }
  
      public void setId( Integer id ) {
          this.id = id;
      }
  
      public String getArtist() {
          return artist;
      }
  
      public void setArtist( String artist ) {
          this.artist = artist;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle( String title ) {
          this.title = title;
      }
  
      public String toString() {
          return artist + ": " + title;
      }
  }
  </code></pre>
  
  <p>The APi call happens through this class:</p>
  
  <pre><code>package com.ashwin.cacheapp;
  
  import com.ashwin.cacheapp.model.Album;
  import com.ashwin.cacheapp.response.ResponseGen;
  import com.ashwin.cacheapp.service.MusicService;
  
  import java.util.ArrayList;
  import java.util.List;
  import javax.enterprise.context.RequestScoped;
  import javax.inject.Inject;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.UriInfo;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.Consumes;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.PUT;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  
  @Path("hello")
  @RequestScoped
  public class ApiResource {
  
      MusicService musicService=new MusicService();
  
  
      @GET
      @Path("getAllMusic")
      @Produces(MediaType.APPLICATION_JSON)
      public List&lt;Album&gt; getAllMusic() {
          List&lt;Album&gt; albumList=musicService.getAllMusic();
  
          return musicService.getAllMusic();
  
      }
  
  
  }
  </code></pre>
  
  <p>MusicService.java</p>
  
  <pre><code>package com.ashwin.cacheapp.service;
  
  import com.ashwin.cacheapp.dao.MusicDao;
  import com.ashwin.cacheapp.model.Album;
  
  import org.apache.jcs.JCS;
  
  import java.util.List;
  
  import org.apache.log4j.PropertyConfigurator;
  
  import java.io.FileInputStream;
  import java.util.Properties;
  
  //@Stateless
  public class MusicService {
  
      private MusicDao musicDao=new MusicDao();
    private JCS cache;
  
  
      public List&lt;Album&gt; getAllMusic() {
          try
               {
               Properties props = new Properties();
              props.load(new FileInputStream("src/main/resources/log4j.properties"));
              PropertyConfigurator.configure(props); 
  
         List&lt;Album&gt; albumList = (List) cache.get("musicCache");
       System.out.println("Size of album is"+albumList.size());
          if(albumList!=null) {
              System.out.println("Returning data from cache");
            return albumList;
             }
       } catch(Exception e ){
  
               try{
                   System.out.println("Putting data in cache");
                   musicDao.putIncache();
               }
               catch(Exception ef){
  
               }
               return musicDao.getAllTopHundredMusic();
       }
  
          return null;
  
      }
  }
  </code></pre>
  
  <p>The Dao Class is:(I am using dummy datas)</p>
  
  <pre><code>package com.ashwin.cacheapp.dao;
  
  
  import com.ashwin.cacheapp.model.Album;
  import org.apache.jcs.JCS;
  import org.apache.jcs.access.exception.CacheException;
  import org.apache.jcs.engine.control.CompositeCacheManager;
  import org.apache.log4j.Logger;
  
  import javax.enterprise.context.RequestScoped;
  import javax.enterprise.context.SessionScoped;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Properties;
  
  
  public class MusicDao {
    private List&lt;Album&gt; albumList=new ArrayList&lt;Album&gt;();
      private JCS cache;
  
      public List&lt;Album&gt; getAllMusic() {
             return null;
  
      }
  
      public List&lt;Album&gt; getAllTopHundredMusic() {
  
  
          albumList.add(new Album(1, "Toby Mac", "Diverse City"));
          albumList.add(new Album(2, "Toby Mac", "Diverse City"));
          albumList.add(new Album(3, "Toby Mac", "Diverse City"));
          albumList.add(new Album(4, "Toby Mac", "Diverse City"));
          albumList.add(new Album(5, "Toby Mac", "Diverse City"));
          albumList.add(new Album(6, "Toby Mac", "Diverse City"));
          albumList.add(new Album(7, "Toby Mac", "Diverse City"));
          albumList.add(new Album(8, "Toby Mac", "Diverse City"));
          albumList.add(new Album(9, "Toby Mac", "Diverse City"));
          albumList.add(new Album(10, "Toby Mac", "Diverse City"));
          albumList.add(new Album(11, "Toby Mac", "Diverse City"));
          albumList.add(new Album(12, "Toby Mac", "Diverse City"));
          albumList.add(new Album(13, "Toby Mac", "Diverse City"));
          albumList.add(new Album(14, "Toby Mac", "Diverse City"));
          albumList.add(new Album(15, "Toby Mac", "Diverse City"));
          albumList.add(new Album(16, "Toby Mac", "Diverse City"));
          albumList.add(new Album(17, "Toby Mac", "Diverse City"));
          albumList.add(new Album(18, "Toby Mac", "Diverse City"));
          albumList.add(new Album(19, "Toby Mac", "Diverse City"));
  
         return albumList;
  
      }
  
      public void putIncache() {
          try {
  
  
              CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
              Properties props = new Properties();
  
              props.put("jcs.default","DC");
              props.put("jcs.default.cacheattributes",
                      "org.apache.jcs.engine.CompositeCacheAttributes");
  // lots more props.put - this is basically the contents of cache.ccf
              Logger log
                      = Logger.getLogger(MusicDao.class);
  
              ccm.configure(props);
  
              // Load the cache
              cache = JCS.getInstance("musicCache");
  
  
              int i=0;
              for(Album a:albumList){
                  cache.put(new Integer(i+1), a);
  
              }
  
  
          } catch (CacheException e) {
              System.out.println("here");
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>In <code>src/main/resource</code> i have config file cache.ccf as:</p>
  
  <pre><code>jcs.default=
  jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
  jcs.default.cacheattributes.MaxObjects=200
  jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
  jcs.default.elementattributes.IsEternal=false
  jcs.default.elementattributes.MaxLifeSeconds=86400
  jcs.default.elementattributes.IdleTime=86400
  </code></pre>
  
  <p>I dont know whether this file is either called from class path or not so i added in my pom.xml as:</p>
  
  <pre><code> &lt;resources&gt;
        &lt;resource&gt;
          &lt;directory&gt;src/main/resources&lt;/directory&gt;
          &lt;includes&gt;
            &lt;include&gt;**/*.ccf&lt;/include&gt;
          &lt;/includes&gt;
        &lt;/resource&gt;
          &lt;/resources&gt;
  </code></pre>
  
  <p>in pom.xml it is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.ashwin&lt;/groupId&gt;
      &lt;artifactId&gt;CacheApp&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;name&gt;CacheApp&lt;/name&gt;
  
      &lt;properties&gt;
          &lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
      &lt;repositories&gt;  
          &lt;repository&gt;
      &lt;id&gt;jfrog-libs&lt;/id&gt;
      &lt;name&gt;jfrog-libs&lt;/name&gt;
      &lt;url&gt;http://repo.jfrog.org/artifactory/libs-releases&lt;/url&gt;
      &lt;snapshots&gt;
          &lt;enabled&gt;false&lt;/enabled&gt;
      &lt;/snapshots&gt;
  &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-jcs-core&lt;/artifactId&gt;
              &lt;version&gt;2.2.1&lt;/version&gt;
              &lt;type&gt;jar&lt;/type&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax&lt;/groupId&gt;
              &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
              &lt;version&gt;7.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
           &lt;!-- https://mvnrepository.com/artifact/jcs/jcs --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;jcs&lt;/groupId&gt;
        &lt;artifactId&gt;jcs&lt;/artifactId&gt;
        &lt;version&gt;1.3&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- https://mvnrepository.com/artifact/log4j/log4j --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j&lt;/artifactId&gt;
        &lt;version&gt;1.2.16&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/concurrent/concurrent --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;concurrent&lt;/groupId&gt;
        &lt;artifactId&gt;concurrent&lt;/artifactId&gt;
        &lt;version&gt;1.3.4&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
      &lt;version&gt;2.9.8&lt;/version&gt;
  &lt;/dependency&gt;
  
  
  &lt;dependency&gt;
      &lt;groupId&gt;com.google.guava&lt;/groupId&gt; 
      &lt;artifactId&gt;guava&lt;/artifactId&gt; 
      &lt;version&gt;16.0&lt;/version&gt; 
  &lt;/dependency&gt;
  
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
  
          &lt;resources&gt;
        &lt;resource&gt;
          &lt;directory&gt;src/main/resources&lt;/directory&gt;
          &lt;includes&gt;
            &lt;include&gt;**/*.ccf&lt;/include&gt;
          &lt;/includes&gt;
        &lt;/resource&gt;
          &lt;/resources&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.7&lt;/source&gt;
                      &lt;target&gt;1.7&lt;/target&gt;
                      &lt;compilerArguments&gt;
                          &lt;endorseddirs&gt;${endorsed.dir}&lt;/endorseddirs&gt;
                      &lt;/compilerArguments&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.6&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;validate&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;copy&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;outputDirectory&gt;${endorsed.dir}&lt;/outputDirectory&gt;
                              &lt;silent&gt;true&lt;/silent&gt;
                              &lt;artifactItems&gt;
                                  &lt;artifactItem&gt;
                                      &lt;groupId&gt;javax&lt;/groupId&gt;
                                      &lt;artifactId&gt;javaee-endorsed-api&lt;/artifactId&gt;
                                      &lt;version&gt;7.0&lt;/version&gt;
                                      &lt;type&gt;jar&lt;/type&gt;
                                  &lt;/artifactItem&gt;
                              &lt;/artifactItems&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>My output when i call api <code>http://localhost:8080/api/hello/getAllMusic</code> is:</p>
  
  <pre><code>Putting data in cache 
  Putting data in cache 
  Putting data in cache 
  Putting data in cache 
  </code></pre>
  
  <p>My expected output is:</p>
  
  <pre><code> Putting data in cache 
  Returning data from cache
  Returning data from cache
  Returning data from cache
  Returning data from cache
  </code></pre>
  
  <p>Edited Source code:
  I made the new class MusicClass.java</p>
  
  <pre><code>package com.ashwin.cacheapp.dao;
  
  import com.ashwin.cacheapp.model.Album;
  import com.ashwin.cacheapp.model.Student;
  import java.util.Properties;
  import org.apache.jcs.JCS;
  import org.apache.jcs.access.exception.CacheException;
  import org.apache.jcs.engine.control.CompositeCacheManager;
  import org.apache.log4j.Logger;
  
  
  
  public class MusicCache {
       private JCS cache;
  
  
         // static variable single_instance of type Singleton 
      private static MusicCache single_instance=null; 
  
  
      // private constructor restricted to this class itself 
      private MusicCache() 
      { 
            try {
             CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
              Properties props = new Properties();
  
              props.put("jcs.default","DC");
              props.put("jcs.default.cacheattributes",
                      "org.apache.jcs.engine.CompositeCacheAttributes");
  // lots more props.put - this is basically the contents of cache.ccf
              Logger log
                      = Logger.getLogger(MusicDao.class);
  
              ccm.configure(props);
  
              // Load the cache
              cache = JCS.getInstance("musicCache");
  
              cache.put( "123", new Student( 123, "John", "Swizher", "Weige", "Civil") );
              cache.put( "143", new Student( 143, "Theoder", "", "Sattler", "Computer Science" ) );
              cache.put( "153", new Student( 153, "Martin", "Sam", "Suckhorozka", "Electrical" ) );
              cache.put( "163", new Student( 163, "Russel", "", "Latour", "Mechanical" ) );
  
  
          } catch (CacheException e) {
              System.out.println("here");
              e.printStackTrace();
          }
      } 
  
      // static method to create instance of Singleton class 
      public static MusicCache Singleton() 
      { 
          // To ensure only one instance is created 
          if (single_instance == null) 
          { 
              single_instance = new MusicCache(); 
          } 
          return single_instance; 
      } 
  }
  </code></pre>
  
  <p>Changing in function of service class is:</p>
  
  <pre><code>  public  List&lt;Album&gt; getAllMusic() {
          try
               {
               Properties props = new Properties();
              props.load(new FileInputStream("src/main/resources/log4j.properties"));
              PropertyConfigurator.configure(props); 
  
       List&lt;Album&gt; albumList = (List) cache.get("musicCache");
       System.out.println("Size of album is"+albumList.size());
          if(albumList!=null) {
              System.out.println("Returning data from cache");
            return albumList;
             }
       } catch(Exception e ){
  
               try{
                   System.out.println("Putting data in cache");
                   MusicCache x = MusicCache.Singleton();
               }
               catch(Exception ef){
  
               }
               return musicDao.getAllTopHundredMusic();
       }
  
          return null;
  
      }
  </code></pre>
  
  <p>Still I got the same output</p>