Search in sources :

Example 1 with CacheResult

use of com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult in project Hystrix by Netflix.

the class CacheInvocationContextFactory method createCacheResultInvocationContext.

/**
     * Create {@link CacheInvocationContext} parametrized with {@link CacheResult} annotation.
     *
     * @param metaHolder the meta holder, see {@link com.netflix.hystrix.contrib.javanica.command.MetaHolder}
     * @return initialized and configured {@link CacheInvocationContext}
     */
public static CacheInvocationContext<CacheResult> createCacheResultInvocationContext(MetaHolder metaHolder) {
    Method method = metaHolder.getMethod();
    if (method.isAnnotationPresent(CacheResult.class)) {
        CacheResult cacheResult = method.getAnnotation(CacheResult.class);
        MethodExecutionAction cacheKeyMethod = createCacheKeyAction(cacheResult.cacheKeyMethod(), metaHolder);
        return new CacheInvocationContext<CacheResult>(cacheResult, cacheKeyMethod, metaHolder.getObj(), method, metaHolder.getArgs());
    }
    return null;
}
Also used : CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) AopUtils.getDeclaredMethod(com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod) Method(java.lang.reflect.Method) MethodExecutionAction(com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction)

Example 2 with CacheResult

use of com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult in project Hystrix by Netflix.

the class CacheInvocationContextFactoryTest method testCacheResultMethodWithCacheKeyMethodWithWrongReturnType_givenCacheKeyMethodWithWrongReturnType_shouldThrowException.

@Test(expected = HystrixCachingException.class)
public void testCacheResultMethodWithCacheKeyMethodWithWrongReturnType_givenCacheKeyMethodWithWrongReturnType_shouldThrowException() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String param1 = "val_1";
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethodWithCacheKeyMethodWithWrongReturnType", String.class, String.class)).args(new Object[] { param1 }).obj(testCacheClass).build();
    // when
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    System.out.println(context);
// then expected HystrixCachingException
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Example 3 with CacheResult

use of com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult in project Hystrix by Netflix.

the class HystrixCacheKeyGeneratorTest method testGenerateCacheKey_givenUserWithNullProfile_shouldReturnCorrectCacheKey.

@Test
public void testGenerateCacheKey_givenUserWithNullProfile_shouldReturnCorrectCacheKey() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String id = "1";
    User user = new User();
    user.setId(id);
    user.setProfile(null);
    String expectedKey = id;
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethod", String.class, User.class)).args(new Object[] { id, user }).obj(testCacheClass).build();
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    HystrixCacheKeyGenerator keyGenerator = HystrixCacheKeyGenerator.getInstance();
    // when
    String actual = keyGenerator.generateCacheKey(context).getCacheKey();
    // then
    assertEquals(expectedKey, actual);
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Example 4 with CacheResult

use of com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult in project Hystrix by Netflix.

the class CacheInvocationContextFactoryTest method testCreateCacheResultInvocationContext_givenMethodAnnotatedWithCacheResult_shouldCreateCorrectCacheKeyInvocationContext.

@Test
public void testCreateCacheResultInvocationContext_givenMethodAnnotatedWithCacheResult_shouldCreateCorrectCacheKeyInvocationContext() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String param1 = "val_1";
    String param2 = "val_2";
    Integer param3 = 3;
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethod", String.class, String.class, Integer.class)).args(new Object[] { param1, param2, param3 }).obj(testCacheClass).build();
    // when
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    // then
    assertNotNull(context.getKeyParameters());
    assertEquals(2, context.getKeyParameters().size());
    assertEquals(String.class, context.getKeyParameters().get(0).getRawType());
    assertEquals(0, context.getKeyParameters().get(0).getPosition());
    assertEquals(param1, context.getKeyParameters().get(0).getValue());
    assertTrue(isAnnotationPresent(context.getKeyParameters().get(0), CacheKey.class));
    assertEquals(Integer.class, context.getKeyParameters().get(1).getRawType());
    assertEquals(2, context.getKeyParameters().get(1).getPosition());
    assertEquals(param3, context.getKeyParameters().get(1).getValue());
    assertTrue(isAnnotationPresent(context.getKeyParameters().get(1), CacheKey.class));
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) CacheKey(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey) Test(org.junit.Test)

Example 5 with CacheResult

use of com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult in project Hystrix by Netflix.

the class CacheInvocationContextFactoryTest method testCacheResultMethodWithWrongCacheKeyMethodSignature_givenWrongCacheKeyMethod_shouldThrowException.

@Test(expected = HystrixCachingException.class)
public void testCacheResultMethodWithWrongCacheKeyMethodSignature_givenWrongCacheKeyMethod_shouldThrowException() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String param1 = "val_1";
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethodWithWrongCacheKeyMethodSignature", String.class)).args(new Object[] { param1 }).obj(testCacheClass).build();
    // when
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
// then expected HystrixCachingException
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Aggregations

CacheResult (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult)7 MetaHolder (com.netflix.hystrix.contrib.javanica.command.MetaHolder)6 Test (org.junit.Test)6 CacheKey (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey)1 MethodExecutionAction (com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction)1 AopUtils.getDeclaredMethod (com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod)1 Method (java.lang.reflect.Method)1