Hi all.

I’m experiencing a bad behavior using SAFE_AND_CACHEABLE actions implemented on 
Domain Entities.

As current QueryResultsCache implementation has as its Key the calling class, 
instead of the concrete domain entity that has invoked it, it will return the 
cached result when invoking the same action on different domain objects.

For example, I have the following action:

public class Product {

    // {{ findStockForSupplier (action)
    @Action(semantics = SemanticsOf.SAFE_AND_REQUEST_CACHEABLE)
    @ActionLayout(hidden = Where.ANYWHERE)
    @MemberOrder(name = "supplierTerms", sequence = "3")
    public BigDecimal findStockForSupplier(
            final Supplier supplier) {
        final ProductSupplierTerms productSupplierTerms = 
this.wrapSkipRules(this).findTermsForSupplier(supplier);
        return productSupplierTerms != null ? productSupplierTerms.getStock() : 
BigDecimal.ZERO;
    }

}


If I invoke that action over 2 different Products (ie, Product instances) as 
per:

final ProductSupplierTerms productSupplierTerms1 = 
this.wrapSkipRules(product1).findTermsForSupplier(supplier);

final ProductSupplierTerms productSupplierTerms2 = 
this.wrapSkipRules(product2).findTermsForSupplier(supplier);


For the second invocation it will return the cached result, as only the class 
is considered on the QueryResultsCache and not the instance “identity”, and 
both “product1” and “product2” are Product instances.


As all Domain Entities implement the Comparable interface, perhaps it might be 
considered.

Until now, I didn’t noticed this because I always used the QueryResultsCache 
for Domain Services, and they have only 1 instance in production.

But it’s not the same when the SAFE_AND_CACHEABLE actions are implemented on 
Domain Entities.


Perhaps the solution would be to simply change the QueryResultsCache.Key 
implementation to accept an Object instead of a Class, using the “Comparable” 
interface if implemented to differentiate between objects (for Domain Objects), 
and the “equal” operator when not present (for Services)?

Thanks,

Oscar







 

Reply via email to