Spring caching eviction - spring

I am having trouble with understanding spring #cacheEvict annotation. Does it a method to trigger cache eviction? Please see below.
#CacheEvict
public void clearEmployeeById(int id) {
//Do we have to add a method as trigger here in order to trigger the cache eviction? or can we leave this method without implementation
}
``

You need to specify the cache name in #CacheEvict
#CacheEvict(value = {"employee"}, allEntries = true)
public void clearEmployeeById(int id) {
//Logic
}
Cache employee will get evicted, Whenever clearEmplyoeeByID() method gets invoked.
To add cache into employee
#Cacheable(value = {"employee"})
public Employee addEmployeeById(int id) {
//Logic
return employee;
}
To clear all cache, you need to use CacheManager
#Autowired
private CacheManager cacheManager;
#Scheduled(fixedDelay = 86400000)
public boolean evictAllCaches() {
cacheManager.getCacheNames().stream().forEach(c -> cacheManager.getCache(c).clear());
return true;
}

Related

Spring #Cacheable for method no parameter

I want to cache some db data. for example Cache Customer and use customer.id as the key.
How could I set the key if I want to load all customers (allCustomer() in the code) ?
#Cacheable(value = "customer", key = "#customerID")
public Customer getCustomer(Long customerID) {
return getCustomerData(customerID);
}
// How to setup this key?
#Cacheable(value = "customer", key = "?")
public List<Customer> allCustomer(){
return db.values().stream().collect(Collectors.toList());
}
#CachePut(value = "customer", key = "#customer.id")
public void updateCustomer(Customer customer){
db.put(customer.getId(), customer);
}
#CacheEvict(value = "customer", key = "#customerID")
public void deleteCustomer(Long customerID){
db.remove(customerID);
}
I would recommend using #CachePut instead of #Cacheable. In the case that a new entry is added to the DB from outside of this application instance, the cache would not contain that new value.
You can use #result.id to tell Spring which value to use as a key and I've included a conditional so that you don't get strange errors in case of a null value.
#CachePut(value = "customer", key = "#result.id", condition = "#result != null")
It's impossible to do it for collections with the Spring's annotations - with #Cacheable you'd have just one element in a cache with a computed key and a value with the whole list inside.
If performance is not that important in your app, use getCustomer(...) in a loop.
Otherwise, you'll need to update your cache manually. Unfortunately, Cache interface doesn't provide a method to retrieve all keys/values/key-value pairs from a cache, so a bit of casting is required.
The example for the default in-memory cache (spring.cache.type=simple):
#Autowired
private org.springframework.CacheManager cacheManager;
public List<Customer> allCustomers() {
ConcurrentMap<Long, Customer> customerCache = (ConcurrentMap<Long, Customer>)
cacheManager.getCache("customer").getNativeCache();
if (!customerCache.isEmpty()) {
return new ArrayList<>(customerCache.values());
}
List<Customer> customers = db.values().stream().collect(Collectors.toList());
customers.forEach(customer -> customerCache.put(customer.getId(), customer));
return customers;
}
Or for spring.cache.type=jcache with backed EhCache 3:
#Autowired
private org.springframework.CacheManager cacheManager;
public List<Customer> allCustomers() {
javax.cache.Cache<Long, Customer> customerCache = (javax.cache.Cache<Long, Customer>)
cacheManager.getCache("customer").getNativeCache();
Iterator<Cache.Entry<Long, Customer>> iterator = customerCache.iterator();
List<Customer> cachedCustomers = new ArrayList<>();
while (iterator.hasNext()) {
Cache.Entry<Long, Customer> entry = iterator.next();
cachedCustomers.add(entry.getValue());
}
if (!cachedCustomers.isEmpty()) {
return cachedCustomers;
}
List<Customer> customers = db.values().stream().collect(Collectors.toList());
customers.forEach(customer -> customerCache.put(customer.getId(), customer));
return customers;
}
The same can be done similarly for any other cache type (redis, hazelcast, caffeine etc.).
The corresponding eviction method can be written much easier:
#CacheEvict(value = "customer", allEntries = true)
public void deleteAllCustomers(){
db.removeAll(); //pseudocode
}

Spring-data JdbcTemplate does not commit

I need to update thousands of records in the database but i would like to commit after a batch of 5000 records.
#Service
#Transactional (rollbackFor=Throwable.class)
public class AttributeProcessorServiceImpl extends DataLoader implements
AttributeProcessorService
{
.....
private final TransactionTemplate transTemplate;
private final JdbcTemplate jdbcTemplate;
#Autowired private PlatformTransactionManager platformTransactionManager;
#Autowired
public BlockAttributeProcessorServiceImpl(
TransactionTemplate transTemplate,
JdbcTemplate jdbcTemplate,
.....)
{
super();
this.transTemplate = transTemplate;
this.jdbcTemplate=jdbcTemplate;
.....
}
#Async
#Transactional (propagation=Propagation.NOT_SUPPORTED)
public void reloadAttrs()
{
loadAttrs();
updateAttrs();
}
private void loadAttrs()
{
...some data fetching and processing, finally call db update.
updateDbInBatches(rowcount, sql);
}
private void updateAttrs()
{
...some data fetching and processing, finally call db update.
updateDbInBatches(rowcount, sql);
}
private void updateDbInBatches(long rowcount, String sql)
{
DefaultTransactionDefinition def;
boolean hasMore=true;
Integer from;
Integer to = 0;
int batchSize=5000; //gets from property
while (hasMore)
{
from = to+1;
to = batchSize;
def = new DefaultTransactionDefinition();
def.setName("backCommitTx");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = platformTransactionManager.getTransaction(def);
int rows = jdbcTemplate.update(sql,paramValues,paramTypes);
logger.debug("Loaded ["+rows+"] records.");
platformTransactionManager.commit(status);
if (to > rowcount)
{
hasMore=false;
logger.debug("All records ["+rowcount+"] updated.");
}
}
}
}
If I put a breakpoint after loadAttrs(), it shows it loaded bunch of records to the database and issued a commit(), but database does not reflect that commit, until after entire public method completes. How do i ensure data is indeed written to the database after each commit. commit neither gives any error as well.
I missed an important piece of information that solved the problem.
I had another public method which is what was called from outside.
public void reloadAttrs(TransDetail trans)
{
reloadAttrs();
}
Above method was infact using default Transaction Propagation as i did not mention it specifically. Since this was the first public method that was called, spring was ignoring transaction demarcation on next public (async) method that was called. I changed above signature to:
#Transactional (propagation=Propagation.NOT_SUPPORTED)
public void reloadAttrs(TransDetail trans)
{
reloadAttrs();
}
It then worked. I was able to see changes in the database after every commit.

Caffeine cache refresh / reload cache manually or on demand

I have implemented caffeine cache in my application. I am caching data from few static tables. But i want to know if i can refresh / clear / reload cache manually or on demand using a REST API or any other way.
Can any one please suggest a way to implement such a requirement.
I want something like :-
an endpoint url like :- http://localhost:8080/refreshCache
this will trigger some method internally and clear the cache or reload new values in cache manually.
Below is the cache configuration:
#Configuration
public class CacheConfig{
private com.github.benmanes.caffeine.cache.Cache<Object, Object> cache;
#Bean
Caffeine<Object,Object> cacheBuilder(){
return Caffeine.newBuilder()
.initialCapacity(300)
.maximumSize(50000)
.expireAfterAccess(1, TimeUnit.DAYS)
.removalListener(new CacheRemovalListener())
.recordStats();
}
class CacheRemovalListener implements RemovalListener<Object, Object> {
#Override
public void onRemoval(Object key, Object value, RemovalCause cause) {
System.out.format("Removal listener called with key [%s], cause[%s], evicted [%s] %n",
key , cause.toString(), cause.wasEvicted());
}
}
}
You can use Spring's CacheManager to create CaffeineCache instances and then you can perform CRUD operations on any cache using CacheManager.
See Below code.
Bean Configuration:
public class CacheBeansConfig {
#Bean
public CacheManager cacheManager() {
// create multiple instances of cache
CaffeineCacheManager cacheManager = new CaffeineCacheManager("UserCache","InventoryCache");
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}
private Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.initialCapacity(<initial capacity>)
.maximumSize(<max size>)
.expireAfterAccess(<expire after hrs>, TimeUnit.HOURS)
.recordStats();
}
This will initialize your CacheManager with two Caffeeine Cache instances.
Use below Rest Controller Class to access these class.
#RestController
#RequestMapping(path = "/v1/admin/cache")
public class ACSCacheAdminController {
#Autowired
private CacheManager cacheManager;
/**
* call this to invalidate all cache instances
*/
#DeleteMapping(
path = "/",
produces = {"application/json"})
public void invalidateAll() {
Collection<String> cacheNames = cacheManager.getCacheNames();
cacheNames.forEach(this::getCacheAndClear);
}
/**
* call this to invalidate a given cache name
*/
#DeleteMapping(
path = "/{cacheName}",
produces = {"application/json"})
public void invalidateCache(#PathVariable("cacheName") final String cacheName) {
getCacheAndClear(cacheName);
}
/**
* Use this to refresh a cache instance
*/
#PostMapping(
path = "/{cacheName}",
produces = {"application/json"})
public void invalidateCache(#PathVariable("cacheName") final String cacheName) {
getCacheAndClear(cacheName);
Cache cache = cacheManager.getCache(cacheName);
// your logic to put in above cache instance
// use cache.put(key,value)
}
/**
* call this to invalidate cache entry by given cache name and cache key
*/
#DeleteMapping(
path = "/{cacheName}/{key}/",
produces = {"application/json"})
public void invalidateCacheKey(
#PathVariable("cacheName") final String cacheName, #PathVariable("key") Object key) {
final Cache cache = cacheManager.getCache(cacheName);
if (cache == null) {
throw new IllegalArgumentException("invalid cache name for key invalidation: " + cacheName);
}
cache.evict(key);
}
#GetMapping(
path = "/{cacheName}/{key}",
produces = {"application/json"})
public ResponseEntity<Object> getByCacheNameAndKey(
#PathVariable("cacheName") final String cacheName, #PathVariable("key") final int key) {
final Cache cache = cacheManager.getCache(cacheName);
if (cache == null) {
throw new IllegalArgumentException("invalid cache name: " + cacheName);
}
return ResponseEntity.ok().body(cache.get(key));
}
private void getCacheAndClear(final String cacheName) {
final Cache cache = cacheManager.getCache(cacheName);
if (cache == null) {
throw new IllegalArgumentException("invalid cache name: " + cacheName);
}
cache.clear();
}
Just change the code as per your need :)

Spring Redis Cache not evicting

The following works (results in the evict being performed):
fooController {
#ApiEndpoint
public delete(id) {
fooService.deleteFoo(id)
}
}
fooService {
#CacheEvict(value = "cache1", key = "#id")
public void deleteFoo(Long id) {
//delete logic here
}
}
But this does not work (nothing is evicted from the cache):
fooController {
#ApiEndpoint
public delete(name) {
fooService.deleteFoo2(name)
}
}
fooService {
public void deleteFoo2(String name) {
//delete logic here
deleteFoo(name.getId())
}
#CacheEvict(value = "cache1", key = "#id")
public void deleteFoo(Long id) {
//delete logic here
}
}
Why are my #CacheEvict annotations only called when the method is called straight from the controller?
I'm using Redis as the caching mechanism.
Aop is not worinking when your method is called inside the class.
It is working when method is called by another class.
So you can define deleteFoo in another service.
To make spring aspect intercept #Cache* annotations you have to make external call. If you don't like to call this method from another object, use bean self-invocation approach. In this case your class is presented as two objects and one calls the other:
#Resource private FooController thisBean;
public delete(id) {
thisBean.deleteFoo(id)
}
#CacheEvict(value = "cache1", key = "#id")
public void deleteFoo(Long id) {}

Spring Cache with Ehcache #CacheEvict doesn't work

I've got next method in UserService:
#Cacheable(value = "user", key="#p0")
public User find(String user) {
return userRepository.findByUser(User);
}
It caches well. In other service I have:
#Transactional
public void updateToken(int id, String token) {
Group group = groupRepository.findOne(id);
group.getMembers().forEach(member -> {
member.getUser().setToken(token);
removeUserCacheByName(member.getUser().getName());
});
groupRepository.save(group);
}
#CacheEvict(value = "user", key="#p0")
public void removeUserCacheByName(String name) {
log.debug("Removing user cache by name {}.", name);
}
After updateToken method, cache does not clear.
What you're seeing is normal. You're calling removeUserCacheByName() from within the Proxy object so the catching aspect doesn't execute. You have this behaviour explained in the documentation.
You can do some things to work around this:
1) Take the evict method (removeUserCacheByName) to another bean, autowire it in updateToken()'s class, and call it.
2) An ugly but useful one, autowire the ApplicationContext, get the same object from it and call the method, e.g.:
public class UserService{
#Autowired
private ApplicationContext ac;
#Transactional
public void updateToken(int id, String token) {
Group group = groupRepository.findOne(id);
group.getMembers().forEach(member -> {
member.getUser().setToken(token);
UserService sameBean = ac.getBean(UserService.class);
sameBean.removeUserCacheByName(member.getUser().getName());
});
groupRepository.save(group);
}
#CacheEvict(value = "user", key="#p0")
public void removeUserCacheByName(String name) {
log.debug("Removing user cache by name {}.", name);
}
}

Resources