protected KeyGenerator determineKeyGenerator( @Nullable CacheDefaults defaults, Class<? extends CacheKeyGenerator> candidate) { if (candidate != CacheKeyGenerator.class) { return new KeyGeneratorAdapter(this, getBean(candidate)); } else if (defaults != null && CacheKeyGenerator.class != defaults.cacheKeyGenerator()) { return new KeyGeneratorAdapter(this, getBean(defaults.cacheKeyGenerator())); } else { return getDefaultKeyGenerator(); } }
@Override public Object generate(Object target, Method method, Object... params) { JCacheOperation<?> operation = this.cacheOperationSource.getCacheOperation(method, target.getClass()); if (!(AbstractJCacheKeyOperation.class.isInstance(operation))) { throw new IllegalStateException("Invalid operation, should be a key-based operation " + operation); } CacheKeyInvocationContext<?> invocationContext = createCacheKeyInvocationContext(target, operation, params); if (this.cacheKeyGenerator != null) { return this.cacheKeyGenerator.generateCacheKey(invocationContext); } else { Assert.state(this.keyGenerator != null, "No key generator"); return doGenerate(this.keyGenerator, invocationContext); } }
private void assertCacheKeyGenerator(KeyGenerator actual, Class<? extends CacheKeyGenerator> expectedTargetType) { assertEquals("Wrong cache resolver implementation", KeyGeneratorAdapter.class, actual.getClass()); KeyGeneratorAdapter adapter = (KeyGeneratorAdapter) actual; assertEquals("Wrong target CacheKeyGenerator implementation", expectedTargetType, adapter.getTarget().getClass()); }
@Override public Object generate(Object target, Method method, Object... params) { JCacheOperation<?> operation = this.cacheOperationSource.getCacheOperation(method, target.getClass()); if (!(AbstractJCacheKeyOperation.class.isInstance(operation))) { throw new IllegalStateException("Invalid operation, should be a key-based operation " + operation); } CacheKeyInvocationContext<?> invocationContext = createCacheKeyInvocationContext(target, operation, params); if (this.cacheKeyGenerator != null) { return this.cacheKeyGenerator.generateCacheKey(invocationContext); } else { Assert.state(this.keyGenerator != null, "No key generator"); return doGenerate(this.keyGenerator, invocationContext); } }
protected KeyGenerator determineKeyGenerator( @Nullable CacheDefaults defaults, Class<? extends CacheKeyGenerator> candidate) { if (candidate != CacheKeyGenerator.class) { return new KeyGeneratorAdapter(this, getBean(candidate)); } else if (defaults != null && CacheKeyGenerator.class != defaults.cacheKeyGenerator()) { return new KeyGeneratorAdapter(this, getBean(defaults.cacheKeyGenerator())); } else { return getDefaultKeyGenerator(); } }
private void assertDefaults(AbstractJCacheKeyOperation<?> operation) { assertEquals(defaultCacheResolver, operation.getCacheResolver()); assertEquals(KeyGeneratorAdapter.class, operation.getKeyGenerator().getClass()); assertEquals(defaultKeyGenerator, ((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget()); }
@Override public Object generate(Object target, Method method, Object... params) { JCacheOperation<?> operation = this.cacheOperationSource.getCacheOperation(method, target.getClass()); if (!(AbstractJCacheKeyOperation.class.isInstance(operation))) { throw new IllegalStateException("Invalid operation, should be a key-based operation " + operation); } CacheKeyInvocationContext<?> invocationContext = createCacheKeyInvocationContext(target, operation, params); if (this.cacheKeyGenerator != null) { return this.cacheKeyGenerator.generateCacheKey(invocationContext); } else { return doGenerate(this.keyGenerator, invocationContext); } }
this.adaptedKeyGenerator = new KeyGeneratorAdapter(this, this.keyGenerator);
@Test public void customCacheResolver() { CacheResultOperation operation = getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class); assertJCacheResolver(operation.getCacheResolver(), TestableCacheResolver.class); assertJCacheResolver(operation.getExceptionCacheResolver(), null); assertEquals(KeyGeneratorAdapter.class, operation.getKeyGenerator().getClass()); assertEquals(defaultKeyGenerator, ((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget()); }
protected KeyGenerator determineKeyGenerator(CacheDefaults defaults, Class<? extends CacheKeyGenerator> candidate) { if (CacheKeyGenerator.class != candidate) { return new KeyGeneratorAdapter(this, getBean(candidate)); } else if (defaults != null && CacheKeyGenerator.class != defaults.cacheKeyGenerator()) { return new KeyGeneratorAdapter(this, getBean(defaults.cacheKeyGenerator())); } else { return getDefaultKeyGenerator(); } }
@Test public void customKeyGeneratorSpringBean() { TestableCacheKeyGenerator bean = new TestableCacheKeyGenerator(); beanFactory.registerSingleton("fooBar", bean); CacheResultOperation operation = getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class); assertEquals(defaultCacheResolver, operation.getCacheResolver()); assertNull(operation.getExceptionCacheResolver()); KeyGeneratorAdapter adapter = (KeyGeneratorAdapter) operation.getKeyGenerator(); assertSame(bean, adapter.getTarget()); // take bean from context }