【问题标题】:How to initialize List<> of object for ehcache?如何为ehcache初始化对象列表<>?
【发布时间】:2019-09-05 03:24:26
【问题描述】:

我需要初始化需要包含的Cache

String in key
List<Object> in value

所以我有 CacheHelper 类,它有

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);
    }


}

但是在这行代码中,我无法将对象转换为列表 String.class,Person.class ,它需要是 String.class,List:

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

但我得到的错误是:

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

我需要将 List 存储在一个键中。如何初始化它?

【问题讨论】:

  • 你试过 Person.class=> List.class 吗?

标签: java ehcache-3


【解决方案1】:

您将无法工作,因为您将配置构建器定义为具有字符串键和人员值,而不是人员列表。泛型不允许您获取 List 类,因此您需要实现一个包装器:

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));
    }


}

【讨论】:

  • 你能帮我详细说明一下
  • PersonList 类扩展了 ArrayList 类,但通过要求 Person 类型的值来限定它。构造函数通过允许您为其提供初始列表来覆盖 ArrayList 的构造函数。 &lt;? extends Person&gt; 表示您可以提供实际的 Person 对象或扩展 Person 的类(例如 Employee extends Person)。
  • 我不能把 (final Collection c) 放在构造函数中吗?
  • 如果你这样做,你不会得到任何编译错误,但如果你通过构造函数将除 Person 以外的任何东西放在该列表中,你会得到运行时错误。例如,这将导致 ClassCastException PersonList personList1 = new PersonList(Arrays.asList(new Integer(7))); Person x = personList1.get(0);
猜你喜欢
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
相关资源
最近更新 更多