Skip to content

Commit

Permalink
Reformat code & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
enozcan committed Nov 8, 2019
1 parent e2065eb commit 28350fe
Show file tree
Hide file tree
Showing 22 changed files with 255 additions and 359 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ L2C can be used for SessionFactory objects in Hibernate.

## Dependencies

- Include `hibernate-jcache` dependency in your program. The version must be the same as
the `hibernate-core` version.
- Include `hibernate-jcache` dependency in your program. The version must be the same as
`hibernate-core` version.

In `pom.xml`:
```xml
Expand Down
43 changes: 21 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.self="override">
<parallel>none</parallel>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<printSummary>false</printSummary>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<parallel>none</parallel>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<printSummary>false</printSummary>
<skipAfterFailureCount>1</skipAfterFailureCount>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
Expand All @@ -44,39 +48,34 @@
<artifactId>hibernate-jcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>${hazelcast.version}</version>
</dependency>

<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-hibernate53</artifactId>
<version>1.3.2</version>
</dependency>


<!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
</dependency>

<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
79 changes: 35 additions & 44 deletions src/main/java/jcache/L2C/CollectionCache.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,69 @@
package jcache.L2C;

import com.hazelcast.core.Hazelcast;
import jcache.L2C.entity.Item;
import jcache.L2C.entity.SubItem;
import jcache.L2C.util.HibernateUtil;
import jcache.L2C.entity.Department;
import jcache.L2C.entity.Employee;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.ArrayList;
import java.util.List;

public class CollectionCache {

public static void main(String[] args){

initializeDatabase();

Session session1, session2;
Transaction tx1, tx2;
Item item;


// Get Department from DB.
// This call will not get employees list.
// It has to be called explicitly.
// Get Item from DB.
// Collection cache miss & put are expected
session1 = HibernateUtil.getSession();
tx1 = session1.getTransaction();
tx1.begin();
Department department = session1.get(Department.class, 1);
System.out.println("Department :" + department.getName());
// Call collection for cached department. Cache miss is expected.
List<Employee> employees=department.getEmployees();
for (Employee employee : employees) {
System.out.println("\tEmployee Name : " + employee.getName());
}
session1.beginTransaction();
item = session1.get(Item.class, 1);

// Since fetch type for the collection is Lazy, force it to be fetched.
for(SubItem i : item.getSubItems()){
System.out.println(i);
};
session1.close();
HibernateUtil.printCollectionCacheStats();

//Get Department again. Cache hit is expected.
//Get Department again. Collection cache hit is expected.
session2 = HibernateUtil.getSession();
tx2 = session2.getTransaction();
tx2.begin();
department = session2.get(Department.class, 1);
System.out.println("Department :" + department.getName());
// Call collection for cached department. Cache hit is expected since
// it has already cached in session1.
// No SQL queries must be shown on the log.
employees = department.getEmployees();
for (Employee employee : employees) {
System.out.println("\tEmployee Name : " + employee.getName());
}
session2.beginTransaction();
item = session2.get(Item.class, 1);

// Since fetch type for the collection is Lazy, force it to be fetched.
for(SubItem i : item.getSubItems()){
System.out.println(i);
};
session2.close();
HibernateUtil.printCollectionCacheStats();

HibernateUtil.closeFactory();
Hazelcast.shutdownAll();

}

private static void initializeDatabase(){

Session session;
Transaction transaction;
Session session = HibernateUtil.getSession();

Item item1 = new Item("item-1",1);


SubItem subItem1 = new SubItem(1,"subitem-1",item1);
SubItem subItem2 = new SubItem(2,"subitem-2",item1);

Department dpt = new Department("Department 1", null);
item1.addSubItem(subItem1).addSubItem(subItem2);

Employee employee1 = new Employee("Employee 1", dpt);
Employee employee2 = new Employee("Employee 2", dpt);
Transaction tx = session.beginTransaction();

List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(employee1);
employeeList.add(employee2);
dpt.setEmployees(employeeList);
session.save(item1);

session = HibernateUtil.getSession();
transaction = session.getTransaction();
transaction.begin();
session.save(dpt);
transaction.commit();
tx.commit();
session.close();
HibernateUtil.evictAllRegions();
}
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/jcache/L2C/EntityCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,48 @@ public class EntityCache {
public static void main(String[] args){

Session session1, session2, session3, session4;
Transaction tx1, tx2, tx3, tx4;
Item item;
Transaction tx1;


// Store an hazelcast.hibernate.jcache.Entity.Item with id = 1
// Store an hazelcast.hibernate.jcache.L2C.Entity.Item with id = 1
// L2C put is expected
session1 = HibernateUtil.getSession();
tx1 = session1.getTransaction();
tx1.begin();
session1.save(new Item(1,"New Hazelcast Item",12345L));
session1.save(new Item("item-1",1));
tx1.commit();
session1.close();

// Evict cache and clear statistics
HibernateUtil.evictAllRegions();

// Get recently stored value
// Data is expected to be fetched from L2 Cache, not from database.
// L2C hit is expected
// Get recently stored value. Since the cache is evicted,
// data is expected to be fetched from DB, not from the cache.
// L2C miss & put are expected
session2 = HibernateUtil.getSession();
tx2 = session2.getTransaction();
tx2.begin();
item = session2.get(Item.class,1);
session2.beginTransaction();
session2.get(Item.class,1);
session2.close();
System.out.println("Item fetched: "+ item);
HibernateUtil.printStats();

// Get last accessed data from another session.
// Data is expected to be fetched from L2 Cache, not from database.
// L2C hit is expected
session3 = HibernateUtil.getSession();
tx3 = session3.getTransaction();
tx3.begin();
item = session3.get(Item.class,1);
System.out.println("Item fetched: "+ item);
tx3.commit();
session3.beginTransaction();
session3.get(Item.class,1);
session3.close();
HibernateUtil.printStats();

// Evict the cache
// Evict the cache and clear statistics
HibernateUtil.evictAllRegions();

// Data is expected to be fetched from database, not from L2C.
// L2C miss & put are expected
session4 = HibernateUtil.getSession();
tx4 = session4.getTransaction();
tx4.begin();
item = session4.get(Item.class,1);
System.out.println("Item fetched: "+ item);
tx4.commit();
session4.beginTransaction();;
session4.get(Item.class,1);
session4.close();
HibernateUtil.printStats();

HibernateUtil.closeFactory();
Hazelcast.shutdownAll();
Expand Down
39 changes: 16 additions & 23 deletions src/main/java/jcache/L2C/QueryCache.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package jcache.L2C;

import com.hazelcast.core.Hazelcast;
import jcache.L2C.util.HibernateUtil;
import jcache.L2C.entity.Item;
import jcache.L2C.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
Expand All @@ -18,55 +18,48 @@ public static void main(String[] args){

Session session1, session2, session3, session4;
Transaction tx1;
List<Item> queryResults;

// Store Items with id = 100,101 and 102
// L2C miss and put are expected
session1 = HibernateUtil.getSession();
tx1 = session1.getTransaction();
tx1.begin();
session1.save(new Item(100,"New Hazelcast Item-0",12345L));
session1.save(new Item(101,"New Hazelcast Item-1",12345L));
session1.save(new Item(102,"New Hazelcast Item-2",12345L));
session1.save(new Item("New Hazelcast Item-0",100));
session1.save(new Item("New Hazelcast Item-1",101));
session1.save(new Item("New Hazelcast Item-2",102));
tx1.commit();
session1.close();

// Evict cache and clear statistics
HibernateUtil.evictAllRegions();

// Get recently stored values via query.
// Data is expected to be fetched from database, not from L2C since evicted.
// QueryCache miss is expected
// QueryCache miss & put are expected
session2 = HibernateUtil.getSession();
queryResults = executeQuery(QUERY_STRING,session2);
printList(queryResults);
executeQuery(QUERY_STRING,session2);
session2.close();
HibernateUtil.printQueryCacheStats();

// Execute last executed query again from another session.
// Data is expected to be fetched from query Cache, not from database.
// QueryCache hit is expected
session3 = HibernateUtil.getSession();
queryResults = executeQuery(QUERY_STRING,session3);
printList(queryResults);
executeQuery(QUERY_STRING,session3);
session3.close();
HibernateUtil.printQueryCacheStats();

// Execute last executed query again from another session.
// Data is expected to be fetched from query Cache, not from database.
// QueryCache hit is expected
// Execute last different query.
// QueryCache miss & put are expected
session4 = HibernateUtil.getSession();
queryResults = executeQuery(QUERY_STRING2,session4);
printList(queryResults);
executeQuery(QUERY_STRING2,session4);
session4.close();
HibernateUtil.printQueryCacheStats();

// Tear down
HibernateUtil.closeFactory();
Hazelcast.shutdownAll();
}

private static void printList(List<Item> list){
System.out.println("Items fetched: ");
for(Item i : list) {
System.out.println(i);
}
}

private static List<Item> executeQuery(String query, Session session){
Transaction tx;
tx = session.getTransaction();
Expand Down
Loading

0 comments on commit 28350fe

Please sign in to comment.