<!--Infinispan-->
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.jbossts</groupId>
<artifactId>jbossjta</artifactId>
<version>4.16.4.Final</version>
</dependency>
<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:6.0 http://www.infinispan.org/schemas/infinispan-config-6.0.xsd"
xmlns="urn:infinispan:config:6.0">
<global>
<transport nodeName="caching" clusterName="caching_cluster" transportClass="org.infinispan.remoting.transport.jgroups.JGroupsTransport">
<properties> <property name="configurationFile" value="tcp.xml"/>
</properties>
</transport>
</global>
<default>
<clustering mode="dist"><async/>
</clustering>
<eviction maxEntries="1000" strategy="LRU"/>
</default>
<namedCache name="productBrand.region">
<clustering mode="dist"/>
<eviction strategy="LRU" maxEntries="5000" threadPolicy="DEFAULT"/>
<expiration maxIdle="10"/>
</namedCache>
</infinispan>
@Entity
@Table(name = "PRODUCT_CATEGORY")
@Cache(region = "productCategory.region", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class ProductCategory {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "CATEGORY_ID")
private Long id;
@Column(name = "NAME")
private String name;
@Cache(region = "productCategory.brands.region", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "BRAND_CATEGORY", joinColumns = @JoinColumn(name = "CATEGORY_ID"), inverseJoinColumns = @JoinColumn(name = "BRAND_ID"))
private List<ProductBrand> brands;
...
}
Cache<Object, Object> cache = new DefaultCacheManager().getCache();
// Add a entry cache.put("key", "value"); // Remove the entry from the cache Object v = cache.remove("key"); // And replace it if missing cache.putIfAbsent("key", "newValue"); cache.clear(); //By default entries are immortal but we can override this on a per-key basis and provide lifespans. cache.put("key", "value", 5, SECONDS);
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-hibernate4</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>3.2.1</version>
</dependency>
<hazelcast>
<group name="dev" password="dev"/>
<network>
<port auto-increment="true">5701</port>
<outbound-ports><ports>0</ports></outbound-ports>
<join>
<multicast enabled="false">
<multicast-group>231.12.21.135</multicast-group>
<multicast-port>45564</multicast-port>
<multicast-timeout-seconds>2</multicast-timeout-seconds>
</multicast>
<tcp-ip enabled="true"><interface>127.0.0.1</interface></tcp-ip>
</join>
<interfaces enabled="true"><interface>127.0.0.*</interface></interfaces>
<ssl enabled="false"/>
<socket-interceptor enabled="false"/>
<symmetric-encryption enabled="false"/>
</network>
<partition-group enabled="false"/>
<executor-service name="default">
<pool-size>16</pool-size>
<queue-capacity>0</queue-capacity>
</executor-service>
<map name="default"/>
<map name="productCategory.brands.region">
<in-memory-format>OBJECT</in-memory-format>
<backup-count>2</backup-count>
<async-backup-count>0</async-backup-count>
<time-to-live-seconds>90</time-to-live-seconds>
<max-idle-seconds>0</max-idle-seconds>
<eviction-policy>LRU</eviction-policy>
<max-size policy="PER_NODE">5000</max-size>
<eviction-percentage>30</eviction-percentage>
<merge-policy>com.hazelcast.map.merge.PassThroughMergePolicy</merge-policy>
<read-backup-data>true</read-backup-data>
</map>
</hazelcast>
<!-- SessionFactory bean with Hazelcast Hibernate properties -->
<prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastCacheRegionFactory</prop>
<prop key="hibernate.cache.hazelcast.configuration_file_path">hazelcast.xml</prop>
<prop key="hibernate.cache.hazelcast.use_lite_member">true</prop>
<prop key="hibernate.cache.hazelcast.shutdown_on_session_factory_close">false</prop>
@Entity
@Table(name = "PRODUCT_CATEGORY")
@Cache(region = "productCategory.region", usage = CacheConcurrencyStrategy.READ_WRITE)
public class ProductCategory {
...
@Cache(region = "productCategory.brands.region", usage = CacheConcurrencyStrategy.READ_WRITE)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "BRAND_CATEGORY", joinColumns = @JoinColumn(name = "CATEGORY_ID"), inverseJoinColumns = @JoinColumn(name = "BRAND_ID"))
private List<ProductBrand> brands;
...
}
Config cfg = new Config();
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
Map<Integer, String> mapCustomers = instance.getMap("customers");
mapCustomers.put(1, "Joe");
mapCustomers.put(2, "Ali");
mapCustomers.put(3, "Avi");