5

I need to initialize the Cache which needs to contain

String in key
List<Object> in value

So i have CacheHelper class which has

public class CacheHelper {

    private CacheManager cacheManager;

    private Cache<String,List<Person>> cacheDataList;

    private static final String CACHE_PERSON="cache_key_person";


    public CacheHelper() {


    }

    public void putInCacheFromDb(){
        System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
        cacheManager= CacheManagerBuilder
                .newCacheManagerBuilder().build();
        cacheManager.init();


        cacheDataList = cacheManager
                .createCache("cacheOfPersonList", CacheConfigurationBuilder
                        .newCacheConfigurationBuilder(
                                String.class,Person.class,
                                ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
                                TimeUnit.SECONDS))));


    }


    public void putInList(List<Person> personList){
        System.out.println("putting list in cache");
        cacheDataList.put(CACHE_PERSON,personList);
    }


}

But at this line of code,I am not able to conver object into list String.class,Person.class ,which needs to be String.class,List:

 cacheDataList = cacheManager
                    .createCache("cacheOfPersonList", CacheConfigurationBuilder
                            .newCacheConfigurationBuilder(
                                    String.class,Person.class,
                                    ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
                                    TimeUnit.SECONDS))));

But I am getting error as:

Incompatible types. Required Cache<String, List<Person>> but 'createCache' was inferred to Cache<K, V>: Incompatible equality constraint: List<Person> and Person

I need to store List in a single key.how can I initialize it?

Ashwin Karki
  • 249
  • 4
  • 18

2 Answers2

4

What you have won't work because you're defining the configuration builder to have a key of String and a value of Person, not a List of Person. Generics doesn't let you get the class of List so you need to implement a wrapper:

import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.expiry.Expirations;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class CacheHelper {


    private CacheManager cacheManager;

    private Cache<String, PersonList> cacheDataList;

    private static final String CACHE_PERSON = "cache_key_person";

    private static class Person {
        final String name;

        public Person(final String name) {
            this.name = name;
        }
    }

    private class PersonList extends ArrayList<Person> {
        public PersonList(final Collection<? extends Person> c) {
            super(c);
        }
    }

    public static void main(String[] args) {


        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Bob"));
        persons.add(new Person("Sally"));

        CacheHelper helper = new CacheHelper();
        helper.putInList(persons);
        PersonList personList = helper.cacheDataList.get(CACHE_PERSON);
        for (Person p : personList) {
            System.out.println("Found " + p.name);
        }

    }

    public CacheHelper() {
        System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
        cacheManager = CacheManagerBuilder
                .newCacheManagerBuilder().build();
        cacheManager.init();
        cacheDataList = cacheManager
                .createCache("cacheOfPersonList", CacheConfigurationBuilder
                        .newCacheConfigurationBuilder(String.class, PersonList.class, ResourcePoolsBuilder.heap(10))
                        .withExpiry(Expirations.timeToLiveExpiration(org.ehcache.expiry.Duration.of(20, TimeUnit.SECONDS))));
    }


    public void putInList(List<Person> personList) {
        System.out.println("putting list in cache");
        cacheDataList.put(CACHE_PERSON, new PersonList(personList));
    }


}
Domenic D.
  • 5,276
  • 4
  • 30
  • 41
  • can u help me to elaborate the line extends Person> – Ashwin Karki Sep 05 '19 at 04:15
  • The PersonList class extends the `ArrayList` class, but qualifies it by requiring values of type Person. The constructor overrides a constructor of ArrayList by allowing you to provide it an initial list. the ` extends Person>` means that you can provide actual Person objects or a class that extends Person (e.g. `Employee extends Person`). – Domenic D. Sep 05 '19 at 04:23
  • cant i just put (final Collection c) in constructor? – Ashwin Karki Sep 06 '19 at 08:21
  • You won't get any compilation errors if you do that, but you'll get runtime errors if you were to ever put anything other than a Person in that list via the constructor. For example, this will cause a ClassCastException `PersonList personList1 = new PersonList(Arrays.asList(new Integer(7)));` `Person x = personList1.get(0);` – Domenic D. Sep 06 '19 at 13:14
0

There is an easier way to store List of objects as a value.

You may just create a simple wrapper which will store List.

public class VoyageList implements Serializable {

  private static final long serialVersionUID = 1L;
  private final List<Person> persons;

  public PersonList(final List<Person> persons) {
    this.persons = Collections.unmodifiableList(persons);
  }

  public List<Person> getPersons() {
    return persons;
  }                                                                        
}

and then it can be used during creating cache

cacheManager.createCache("cacheOfPersonList", CacheConfigurationBuilder
                        .newCacheConfigurationBuilder(String.class, PersonList.class, ResourcePoolsBuilder.heap(10)));
Robert Głowacki
  • 292
  • 5
  • 22