Ehcache 3.3 with value as collection of object

955 views
Skip to first unread message

vaibhav srivastava

unread,
May 31, 2017, 8:54:13 PM5/31/17
to ehcache-users
  1. What version of Ehcache you are currently using; 3.3
  2. Paste the configuration for the Cache/CacheManager you have an issue with;
  3. CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().
            withCache("testCache", 
                        CacheConfigurationBuilder.newCacheConfigurationBuilder
                        (String.class,**customObject.class**,ResourcePoolsBuilder.heap(10))
                        .withExpiry(Expirations.timeToLiveExpiration(Duration.of(200, TimeUnit.SECONDS)))
                      ).build();
  4. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate);
  5. Providing JDK and OS versions maybe useful as well.



I want to replace that customobject.class with List<customObject>
How to initialize cache with value collection of custom object ?

Can we do it ?



Louis Jacomet

unread,
Jun 1, 2017, 5:36:15 AM6/1/17
to ehcache-users
Hi,

Yes you can do it and it will work, but the type system will not help you ... and even stand in your way in places.

In short, you cannot express the class of List<CustomObject> so you have to create a String.class, List.class cache.
Then when getting the cache, you have to cast it from Cache<String, List> to Cache<String, List<CustomObject>>.

Also, whenever you cache a mutable object make sure to understand that mutating it will be reflected in the cache unless you use copiers or move beyond heap.

Regards,
Louis

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/a22c4d21-515a-4947-ab25-8d5b6da3338e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages