I am using @Cacheable
annotation of com.jcabi.aspects
for caching mechanism in my project and I have a scenario in which I need to flush a particular data from the cache instead of flushing the whole cache. How is it possible?
For example,
import com.jcabi.aspects.Cacheable;
public class Employees {
@Cacheable(lifetime = 1, unit = TimeUnit.HOURS)
static int size(Organization org) {
// calculate their amount in MySQL
}
@Cacheable.FlushBefore
static void add(Employee employee, Organization org) {
// add a new one to MySQL
}
}
If I have a class Employees that is used by two organizations Org1 and Org2, now if a new employee is added to Org1, then only Org1's data should be flushed from the cache and Org2's data should remain in the cache.
Reference for com.jcabi.aspects.Cacheable @Cacheable : http://www.yegor256.com/2014/08/03/cacheable-java-annotation.html
It's not possible with jcabi-aspects. And I believe that your design should be improved, in order to make it possible. At the moment your class Employees
is not really a proper object, but a collection of procedures (a utility class). That's why caching can't be done right. Instead, you need a new class/decorator MySqlOrganization
:
class MySqlOrganization {
private final Organization;
@Cacheable
public int size() {
// count in MySQL
}
@Cacheable.FlushBefore
public void add(Employee emp) {
// save it to MySQL
}
}
See the benefits of a proper OOP now? :)