Wednesday, 4 August 2021

Spring cache: specify cache key using method arguments

You can construct a cache key with method parameters and specify it using ‘key’ attribute of @Cacheable annotation.

 

Example1: Method with one parameter

 @Cacheable(key = "#name")
public Organization getByName(String name) {
	// System.out.println("getByName() called");
	for (Organization org : orgs) {
		if (org.getName().equals(name)) {
			return org;
		}
	}

	return null;
}

Above example use ‘name’ as key.

 

Example2: Method with multiple parameters

@Cacheable(key = "{#id, #name }")
public List<Organization> getByNameOrId(int id, String name) {
	List<Organization> myOrgs = new ArrayList<>();

	for (Organization org : orgs) {
		if (org.getName().equals(name)) {
			myOrgs.add(org);
			continue;
		}

		if (org.getId() == id) {
			myOrgs.add(org);
		}
	}

	return myOrgs;
}


Above example use the parameters id and name as key.


 

Find the below working application.

 

Step 1: Create new maven project ‘cache-key-method-params’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>cache-key-method-params</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

		<dependency>
			<groupId>com.github.ben-manes.caffeine</groupId>
			<artifactId>caffeine</artifactId>
		</dependency>

	</dependencies>
</project>

 

Step 3: Define model classes.

 

Organization.java

 

package com.sample.app.model;

public class Organization {
	private int id;
	private String name;
	private String description;

	public Organization(int id, String name, String description) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String toString() {
		return "Organization [id=" + id + ", name=" + name + ", description=" + description + "]";
	}

}

Employee.java

package com.sample.app.model;

public class Employee {
	private int id;
	private String firstName;
	private String lastName;

	public Employee(int id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}


Step 4: Define service classes.

 

OrganizationService.java

package com.sample.app.service;

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

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.sample.app.model.Organization;

@Service
@CacheConfig(cacheNames = "myOrgCache")
public class OrganizationService {
	private static final List<Organization> orgs = new ArrayList<>();

	static {
		Organization org1 = new Organization(1, "ABC Corp", "Hello ABC Corp");
		Organization org2 = new Organization(2, "DEF Corp", "Hello DEF Corp");
		Organization org3 = new Organization(3, "GHI Corp", "Hello GHI Corp");
		Organization org4 = new Organization(4, "JKL Corp", "Hello JKL Corp");
		Organization org5 = new Organization(5, "MNO Corp", "Hello MNO Corp");

		orgs.add(org1);
		orgs.add(org2);
		orgs.add(org3);
		orgs.add(org4);
		orgs.add(org5);
	}

	@Cacheable(key = "#id")
	public Organization getById(int id) {
		// System.out.println("getById() called");
		for (Organization org : orgs) {
			if (id == org.getId()) {
				return org;
			}
		}

		return null;
	}

	@Cacheable(key = "#name")
	public Organization getByName(String name) {
		// System.out.println("getByName() called");
		for (Organization org : orgs) {
			if (org.getName().equals(name)) {
				return org;
			}
		}

		return null;
	}

	@Cacheable(key = "{#id, #name }")
	public List<Organization> getByNameOrId(int id, String name) {
		List<Organization> myOrgs = new ArrayList<>();

		for (Organization org : orgs) {
			if (org.getName().equals(name)) {
				myOrgs.add(org);
				continue;
			}

			if (org.getId() == id) {
				myOrgs.add(org);
			}
		}

		return myOrgs;
	}

}


EmployeeService.java

package com.sample.app.service;

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

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.sample.app.model.Employee;

@Service
@CacheConfig(cacheNames = "myEmployeeCache")
public class EmployeeService {

	private static List<Employee> emps = new ArrayList<>();

	static {
		emps.add(new Employee(1, "Ram", "Kota"));
		emps.add(new Employee(2, "Raj", "Majety"));
		emps.add(new Employee(3, "PTR", "Navakotla"));
		emps.add(new Employee(4, "Krishna", "Boppana"));
	}

	@Cacheable(key = "#id")
	public Employee getEmployeeById(int id) {
		// System.out.println("getEmployeeById() is called");

		for (Employee emp : emps) {
			if (id == emp.getId()) {
				return emp;
			}
		}
		return null;
	}

	@Cacheable(key = "#name")
	public Employee getEmployeeByFirstName(String name) {
		// System.out.println("getEmployeeByFirstName() invoked");

		for (Employee emp : emps) {
			if (name.equals(emp.getFirstName())) {
				return emp;
			}
		}
		return null;
	}
}


Step 5: Define cache configuration.

 

CacheConfig.java

package com.sample.app.config;

import java.util.concurrent.TimeUnit;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.benmanes.caffeine.cache.Caffeine;

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
	@Bean
	@Override
	public CacheManager cacheManager() {
		CaffeineCacheManager cacheManager = new CaffeineCacheManager("myOrgCache", "myEmployeeCache");
		cacheManager.setCaffeine(caffeineCacheBuilder());
		return cacheManager;
	}

	Caffeine caffeineCacheBuilder() {
		return Caffeine.newBuilder().initialCapacity(100).maximumSize(500).expireAfterWrite(10, TimeUnit.SECONDS)
				.recordStats();
	}

}


Step 6: Define main application class.

 

App.java

package com.sample.app;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.context.annotation.Bean;

import com.sample.app.model.Employee;
import com.sample.app.model.Organization;
import com.sample.app.service.EmployeeService;
import com.sample.app.service.OrganizationService;

@SpringBootApplication
public class App {

	@Autowired
	private EmployeeService empService;

	@Autowired
	private OrganizationService orgService;

	@Autowired
	private CacheManager cacheManager;

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

	public void getEmployeeAndOrgDetails() {

		Employee emp = empService.getEmployeeById(1);
		// System.out.println(emp);
		emp = empService.getEmployeeByFirstName("Krishna");
		// System.out.println(emp);

		Organization org = orgService.getById(3);
		// System.out.println(org);
		org = orgService.getByName("GHI Corp");
		// System.out.println(org);

		List<Organization> orgs = orgService.getByNameOrId(3, "JKL Corp");
		orgs.forEach(System.out::println);
	}

	@Bean
	public CommandLineRunner demo() {
		return (args) -> {
			printNativeCache();
			getEmployeeAndOrgDetails();

			printNativeCache();
			// getEmployeeAndOrgDetails();

			System.out.println("\nSleeping for 5 seconds");
			TimeUnit.SECONDS.sleep(5);
			printNativeCache();

			System.out.println("\nSleeping for 5 seconds");
			TimeUnit.SECONDS.sleep(5);
			printNativeCache();

		};
	}

	public void printNativeCache() {
		System.out.println("\n**************************************");
		System.out.println("-- native cache --");
		Collection<String> cacheNames = cacheManager.getCacheNames();

		for (String cacheName : cacheNames) {
			System.out.println("\nFor the cache : " + cacheName);
			CaffeineCache cache = (CaffeineCache) cacheManager.getCache(cacheName);
			com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCache = cache.getNativeCache();
			Map<Object, Object> map = nativeCache.asMap();

			for (Object key : map.keySet()) {
				System.out.println(key + " -> " + map.get(key));
			}

		}

		System.out.println("**************************************\n");
	}
}


Total project structure looks like below.






Run App.java, you will see below messages in console.

**************************************
-- native cache --

For the cache : myOrgCache

For the cache : myEmployeeCache
**************************************

Organization [id=3, name=GHI Corp, description=Hello GHI Corp]
Organization [id=4, name=JKL Corp, description=Hello JKL Corp]

**************************************
-- native cache --

For the cache : myOrgCache
3 -> Organization [id=3, name=GHI Corp, description=Hello GHI Corp]
[3, JKL Corp] -> [Organization [id=3, name=GHI Corp, description=Hello GHI Corp], Organization [id=4, name=JKL Corp, description=Hello JKL Corp]]
GHI Corp -> Organization [id=3, name=GHI Corp, description=Hello GHI Corp]

For the cache : myEmployeeCache
1 -> Employee [id=1, firstName=Ram, lastName=Kota]
Krishna -> Employee [id=4, firstName=Krishna, lastName=Boppana]
**************************************


Sleeping for 5 seconds

**************************************
-- native cache --

For the cache : myOrgCache
3 -> Organization [id=3, name=GHI Corp, description=Hello GHI Corp]
[3, JKL Corp] -> [Organization [id=3, name=GHI Corp, description=Hello GHI Corp], Organization [id=4, name=JKL Corp, description=Hello JKL Corp]]
GHI Corp -> Organization [id=3, name=GHI Corp, description=Hello GHI Corp]

For the cache : myEmployeeCache
1 -> Employee [id=1, firstName=Ram, lastName=Kota]
Krishna -> Employee [id=4, firstName=Krishna, lastName=Boppana]
**************************************


Sleeping for 5 seconds

**************************************
-- native cache --

For the cache : myOrgCache

For the cache : myEmployeeCache
**************************************


You can download complete working application from this link.



 

Previous                                                    Next                                                    Home

ArangoDB: Traverse a cursor using next and hasNext methods

Using next(), hasNext() methods you can access the cursor elements.

 

Example

usersCursor = userCollection.all()

while(usersCursor.hasNext()) print(usersCursor.next())

127.0.0.1:8529@abc_org> usersCursor = userCollection.all()
SimpleQueryAll(user)

127.0.0.1:8529@abc_org> while(usersCursor.hasNext()) print(usersCursor.next())
{ 
  "_key" : "12977", 
  "_id" : "user/12977", 
  "_rev" : "_cRdSijO---", 
  "id" : 1, 
  "firstName" : "Ram" 
}
{ 
  "_key" : "13028", 
  "_id" : "user/13028", 
  "_rev" : "_cRdT1TK---", 
  "id" : 514, 
  "firstName" : "Sailu" 
}
{ 
  "_key" : "14390", 
  "_id" : "user/14390", 
  "_rev" : "_cRd12pm---", 
  "id" : 1, 
  "firstName" : "Ram" 
}
{ 
  "_key" : "14434", 
  "_id" : "user/14434", 
  "_rev" : "_cRd5IAm---", 
  "lastName" : "Battu", 
  "firstName" : "Ram" 
}

127.0.0.1:8529@abc_org>

 

 

 

Previous                                                    Next                                                    Home

Neo4j: Exercise 2: Nodes with multiple labels

Create three nodes Rama, Murali and Nayana.

Label Rama with Person, Cricketer, Singer

Label Murali with Person, Engineer, Blogger.

Label Nayana with Person, Engineer and Singer

 

Step 1: Open neo4j browser by hitting following url in browser.

http://localhost:7474/browser/

 

 


Step 2: Create nodes by executing below queries.

CREATE (r:Person:Cricketer:Singer{name: "Rama", age: 32, city: "Bangalore", male: true}),

(m:Person:Engineer:Blogger{name: "Murali", age: 34, city: "Hyderabad", male: true}),

(n:Person:Engineer:Singer{name: "Nayana", age: 34, city: "Hyderabad", female: true}) RETURN r, m, n

 

 


Query all nodes which are labelled with Singer

MATCH (node:Singer) RETURN node

 


Query all the nodes which are labelled with both Singer and Engineer

MATCH (node:Singer:Engineer) RETURN node

 

Previous                                                    Next                                                    Home

Junit5: ParameterizedTest: Custom Aggregators

Follow below procedure to implement custom aggregators.

 

Step 1: Implement ArgumentsAggregator interface.

public class PersonAggregator implements ArgumentsAggregator {

	@Override
	public Person aggregateArguments(ArgumentsAccessor arguments, ParameterContext context)
			throws ArgumentsAggregationException {
		return new Person(arguments.getString(0), arguments.getString(1), arguments.get(2, Gender.class),
				arguments.get(3, LocalDate.class));
	}

}

Step 2: Use the aggregator defined in step 1.

void testWithArgumentsAggregator(@AggregateWith(PersonAggregator.class) Person person) {
	if (person.getFirstName().equals("Sailu")) {
		assertEquals(Gender.FEMALE, person.getGender());
		assertEquals("PTR", person.getLastName());
		assertEquals(1989, person.getDateOfBirth().getYear());
	} else {
		assertEquals(Gender.MALE, person.getGender());
		assertEquals("Ram", person.getFirstName());
		assertEquals("Gurram", person.getLastName());
		assertEquals(1990, person.getDateOfBirth().getYear());
	}
}


Find the below working application.

 

Gender.java

package com.sample.app.model;

public enum Gender {
	FEMALE, MALE

}


Person.java

package com.sample.app.model;

import java.time.LocalDate;

public class Person {
	private String firstName;
	private String lastName;
	private Gender gender;
	private LocalDate dateOfBirth;

	public Person(String firstName, String lastName, Gender gender, LocalDate dateOfBirth) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.gender = gender;
		this.dateOfBirth = dateOfBirth;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public LocalDate getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(LocalDate dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

}


PersonAggregator.java

package com.sample.app.aggregators;

import java.time.LocalDate;

import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.aggregator.ArgumentsAggregationException;
import org.junit.jupiter.params.aggregator.ArgumentsAggregator;

import com.sample.app.model.Gender;
import com.sample.app.model.Person;

public class PersonAggregator implements ArgumentsAggregator {

	@Override
	public Person aggregateArguments(ArgumentsAccessor arguments, ParameterContext context)
			throws ArgumentsAggregationException {
		return new Person(arguments.getString(0), arguments.getString(1), arguments.get(2, Gender.class),
				arguments.get(3, LocalDate.class));
	}

}


CustomAggregatorTest.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.AggregateWith;
import org.junit.jupiter.params.provider.CsvSource;

import com.sample.app.aggregators.PersonAggregator;
import com.sample.app.model.Gender;
import com.sample.app.model.Person;

public class CustomAggregatorTest {
	
	@ParameterizedTest
	@CsvSource({ 
				"Sailu, PTR, FEMALE, 1989-08-20", 
				"Ram, Gurram, MALE, 1990-01-22"
			})
	void testWithArgumentsAggregator(@AggregateWith(PersonAggregator.class) Person person) {
		if (person.getFirstName().equals("Sailu")) {
			assertEquals(Gender.FEMALE, person.getGender());
			assertEquals("PTR", person.getLastName());
			assertEquals(1989, person.getDateOfBirth().getYear());
		} else {
			assertEquals(Gender.MALE, person.getGender());
			assertEquals("Ram", person.getFirstName());
			assertEquals("Gurram", person.getLastName());
			assertEquals(1990, person.getDateOfBirth().getYear());
		}
	}
}


Run above test class, you will see the test result in junit window.





You can even create a custom annotation that is composed of multiple annotations use.


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AggregateWith(PersonAggregator.class)
public @interface CsvToPerson {
	
}


@ParameterizedTest
@CsvSource({ 
			"Sailu, PTR, FEMALE, 1989-08-20", 
			"Ram, Gurram, MALE, 1990-01-22"
		})
void testWithArgumentsAggregator(@CsvToPerson Person person) {
	.......
	.......
}


Find the below working application.

 

CsvToPerson.java

package com.sample.app.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.params.aggregator.AggregateWith;

import com.sample.app.aggregators.PersonAggregator;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AggregateWith(PersonAggregator.class)
public @interface CsvToPerson {
	
}


CustomAggregatorTest2.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import com.sample.app.annotations.CsvToPerson;
import com.sample.app.model.Gender;
import com.sample.app.model.Person;

public class CustomAggregatorTest2 {
	@ParameterizedTest
	@CsvSource({ 
				"Sailu, PTR, FEMALE, 1989-08-20", 
				"Ram, Gurram, MALE, 1990-01-22"
			})
	void testWithArgumentsAggregator(@CsvToPerson Person person) {
		if (person.getFirstName().equals("Sailu")) {
			assertEquals(Gender.FEMALE, person.getGender());
			assertEquals("PTR", person.getLastName());
			assertEquals(1989, person.getDateOfBirth().getYear());
		} else {
			assertEquals(Gender.MALE, person.getGender());
			assertEquals("Ram", person.getFirstName());
			assertEquals("Gurram", person.getLastName());
			assertEquals(1990, person.getDateOfBirth().getYear());
		}
	}
}

 

 

 





 

 

Previous                                                    Next                                                    Home