Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

ReturnCachedValueConcern is not updating cache #10

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
*/
@AppliesTo( Cached.class )
public class ReturnCachedValueConcern
extends ConcernOf<InvocationHandler>
implements InvocationHandler
extends ConcernOf<InvocationHandler>
implements InvocationHandler
{
@This @Optional
private InvocationCache cache;

@Override
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
throws Throwable
{
boolean voidReturnType = method.getReturnType().equals( Void.TYPE );
if( cache != null || voidReturnType )
Expand All @@ -52,12 +52,16 @@ public Object invoke( Object proxy, Method method, Object[] args )
cacheName += Arrays.asList( args );
}
Object result = cache.cachedValue( cacheName );
if( result != null )
if (result == null)
{
return result;
// No cached value found
result = next.invoke(proxy, method, args);
// Update cache
cache.setCachedValue(cacheName, result);
}
return result;
}
// No cached value found or no InvocationCache defined - call method
// No InvocationCache defined - call method
return next.invoke( proxy, method, args );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.apache.polygene.library.invocationcache;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.polygene.api.injection.scope.Service;
import org.apache.polygene.api.mixin.Mixins;
import org.apache.polygene.bootstrap.AssemblyException;
import org.apache.polygene.bootstrap.ModuleAssembly;
import org.apache.polygene.test.AbstractPolygeneTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

public class ReturnCachedValueTest extends AbstractPolygeneTest {

@Service
ExpensiveOperation srv;

@BeforeEach
void resetInvoicationCounter() {
ExpensiveOperation.invoicationCount.set(0);
}

@Test
public void methodResultIsCachedForEqualParameters() {

String parameter = "cachedHash";
assertThat(srv.compute(parameter), equalTo(parameter.hashCode()));
assertThat(srv.compute(parameter), equalTo(parameter.hashCode()));

assertThat(ExpensiveOperation.invoicationCount.intValue(), equalTo(1));
}

//@Test
//Ignored: cache key builder has to be fixed to support this case
public void cacheKeyNotWorkWithVarargsParameter() {
ExpensiveOperation.invoicationCount.set(0);
srv.compute(7, 13);
srv.compute(7, 13);

assertThat(ExpensiveOperation.invoicationCount.intValue(), equalTo(1));
}

@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
module.services(ExpensiveOperation.class)
.withMixins(SimpleInvocationCacheMixin.class)
.withConcerns(ReturnCachedValueConcern.class);
}


@Mixins(Impl.class)
public interface ExpensiveOperation {

AtomicInteger invoicationCount = new AtomicInteger(0);

@Cached
int compute(String param);

int compute(int... arguments);
}

public abstract static class Impl implements ExpensiveOperation {

@Override
public int compute(String param) {
invoicationCount.incrementAndGet();
return param.hashCode();
}

@Override
public int compute(int... arguments) {
invoicationCount.incrementAndGet();
return Arrays.stream(arguments).sum();
}
}
}