Hello world
compiled-notes
Sunday, June 20, 2021
SPRING CACHE :
1. Dependency :
Contains core
cache implementation
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
Provides more
cache managers like EHCache, etc.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
For spring boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
@EnableCaching
@Configuration
@EnableCaching
public class CachingConfig
{
@Bean
public
CacheManager cacheManager() {
return
new ConcurrentMapCacheManager("addresses");
}
}
XML based caching
<beans>
<cache:annotation-driven
/>
<bean
id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property
name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
name="addresses"/>
</set>
</property>
</bean>
</beans>
2.
Spring
boot – By default ConcurrentMapCacheManager is registered.
3.
Customization
:
@Component
public class SimpleCacheCustomizer
implements CacheManagerCustomizer<ConcurrentMapCacheManager>
{
@Override
public
void customize(ConcurrentMapCacheManager cacheManager) {
cacheManager.setCacheNames(asList("users",
"transactions"));
}
}
4.
Single
cache :
5.
@Cacheable("addresses")
public String
getAddress(Customer customer) {...}
Call is optional. First
checks in “addresses” cache. If not present, then method is called and result
is put into cache.
6.
Multiple
cache :
Call is optional. First
checks in “addresses” and “directory” caches. If not present, then method is called
and result is put into cache.
@Cacheable({"addresses", "directory"})
public String
getAddress(Customer customer) {...}
7.
Clears
all entries in cache “addresses”.
@CacheEvict(value="addresses",
allEntries=true)
public String
getAddress(Customer customer) {...}
8.
Method
is always executed. Results are updated in cache
@CachePut(value="addresses")
public String
getAddress(Customer customer) {...}
9.
Using
multiple annotations :
@Caching(evict = {
@CacheEvict("addresses"),
@CacheEvict(value="directory",
key="#customer.name") })
public String
getAddress(Customer customer) {...}
10.
Class-level
annotation :
Will be applied to all
methods in the class.
@CacheConfig(cacheNames={"addresses"})
public class CustomerDataService
{
@Cacheable
public
String getAddress(Customer customer) {...}
11.
Conditional
cache :
Caching will be done if
customer’s name is Tom (argument)
@CachePut(value="addresses",
condition="#customer.name=='Tom'")
public String
getAddress(Customer customer) {...}
12.
unless
parameter :
Caching will be done if
length of address is less than 64 (return-type).
@CachePut(value="addresses",
unless="#result.length()<64")
public String
getAddress(Customer customer) {...}
13.
Java
based configuration :
@Configuration
@EnableCaching
public class CachingConfig
{
@Bean
public
CacheManager cacheManager() {
SimpleCacheManager
cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(
new
ConcurrentMapCache("directory"),
new
ConcurrentMapCache("addresses")));
return
cacheManager;
}
}
@Component
public class CustomerDataService
{
@Cacheable(value
= "addresses", key = "#customer.name")
public
String getAddress(Customer customer) {
return
customer.getAddress();
}
}
Some more examples :
14.
If
key is not specified, then it uses hashcode of the class.
@Cacheable(value="books",
key="#isbn")
public Book
findStoryBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
@Cacheable(value="books",
key="#isbn.rawNumber")
public Book
findStoryBook (ISBN isbn, boolean checkWarehouse, boolean includeUsed)
@Cacheable(value="books",
key="T(classType).hash(#isbn)")
public Book
findStoryBook (ISBN isbn, boolean checkWarehouse, boolean includeUsed)
15.
Disable
cache –
spring.cache.type=none
16.
Two caches –
spring.cache.cache-names=cache1,cache2
Subscribe to:
Posts (Atom)
Hello world
Hello world