Java Code Examples for org.springframework.cache.interceptor.SimpleKeyGenerator

The following examples show how to use org.springframework.cache.interceptor.SimpleKeyGenerator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: JCacheErrorHandlerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void getFailPutExceptionFail() {
	UnsupportedOperationException exceptionOnPut = new UnsupportedOperationException("Test exception on put");
	Object key = SimpleKeyGenerator.generateKey(0L);
	given(this.cache.get(key)).willReturn(null);
	willThrow(exceptionOnPut).given(this.errorCache).put(key, SimpleService.TEST_EXCEPTION);

	try {
		this.simpleService.getFail(0L);
	}
	catch (IllegalStateException ex) {
		assertEquals("Test exception", ex.getMessage());
	}
	verify(this.errorHandler).handleCachePutError(
			exceptionOnPut, this.errorCache, key, SimpleService.TEST_EXCEPTION);
}
 
Example 2
Source Project: java-technology-stack   Source File: JCacheErrorHandlerTests.java    License: MIT License 6 votes vote down vote up
@Test
public void getFailPutExceptionFail() {
	UnsupportedOperationException exceptionOnPut = new UnsupportedOperationException("Test exception on put");
	Object key = SimpleKeyGenerator.generateKey(0L);
	given(this.cache.get(key)).willReturn(null);
	willThrow(exceptionOnPut).given(this.errorCache).put(key, SimpleService.TEST_EXCEPTION);

	try {
		this.simpleService.getFail(0L);
	}
	catch (IllegalStateException ex) {
		assertEquals("Test exception", ex.getMessage());
	}
	verify(this.errorHandler).handleCachePutError(
			exceptionOnPut, this.errorCache, key, SimpleService.TEST_EXCEPTION);
}
 
Example 3
/**
 * Construct a new {@code DefaultJCacheOperationSource} with the given cache manager,
 * cache resolver and key generator suppliers, applying the corresponding default
 * if a supplier is not resolvable.
 * @since 5.1
 */
public DefaultJCacheOperationSource(
		@Nullable Supplier<CacheManager> cacheManager, @Nullable Supplier<CacheResolver> cacheResolver,
		@Nullable Supplier<CacheResolver> exceptionCacheResolver, @Nullable Supplier<KeyGenerator> keyGenerator) {

	this.cacheManager = SingletonSupplier.ofNullable(cacheManager);
	this.cacheResolver = SingletonSupplier.ofNullable(cacheResolver);
	this.exceptionCacheResolver = SingletonSupplier.ofNullable(exceptionCacheResolver);
	this.keyGenerator = new SingletonSupplier<>(keyGenerator, SimpleKeyGenerator::new);
}
 
Example 4
Source Project: spring-analysis-note   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(this.cache).get(key);

	this.simpleService.get(0L);
	verify(this.errorHandler).handleCacheGetError(exception, this.cache, key);
}
 
Example 5
Source Project: spring-analysis-note   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getPutNewElementFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
	Object key = SimpleKeyGenerator.generateKey(0L);
	given(this.cache.get(key)).willReturn(null);
	willThrow(exception).given(this.cache).put(key, 0L);

	this.simpleService.get(0L);
	verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 0L);
}
 
Example 6
Source Project: spring-analysis-note   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void putFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(this.cache).put(key, 234L);

	this.simpleService.put(0L, 234L);
	verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 234L);
}
 
Example 7
Source Project: spring-analysis-note   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void evictFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(this.cache).evict(key);

	this.simpleService.evict(0L);
	verify(this.errorHandler).handleCacheEvictError(exception, this.cache, key);
}
 
Example 8
Source Project: spring-analysis-note   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper == null) {
		throw new AssertionError("Excepted value to be put in cache with key " + key);
	}
	Object actual = valueWrapper.get();
	if (value != actual) { // instance check on purpose
		throw new AssertionError("Wrong value set in cache with key " + key + ". " +
				"Expected=" + value + ", but got=" + actual);
	}
}
 
Example 9
Source Project: spring-analysis-note   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper != null) {
		throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
	}
}
 
Example 10
Source Project: spring-analysis-note   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper == null) {
		throw new AssertionError("Excepted value to be put in cache with key " + key);
	}
	Object actual = valueWrapper.get();
	if (value != actual) { // instance check on purpose
		throw new AssertionError("Wrong value set in cache with key " + key + ". " +
				"Expected=" + value + ", but got=" + actual);
	}
}
 
Example 11
Source Project: spring-analysis-note   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper != null) {
		throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
	}
}
 
Example 12
/**
 * Construct a new {@code DefaultJCacheOperationSource} with the given cache manager,
 * cache resolver and key generator suppliers, applying the corresponding default
 * if a supplier is not resolvable.
 * @since 5.1
 */
public DefaultJCacheOperationSource(
		@Nullable Supplier<CacheManager> cacheManager, @Nullable Supplier<CacheResolver> cacheResolver,
		@Nullable Supplier<CacheResolver> exceptionCacheResolver, @Nullable Supplier<KeyGenerator> keyGenerator) {

	this.cacheManager = SingletonSupplier.ofNullable(cacheManager);
	this.cacheResolver = SingletonSupplier.ofNullable(cacheResolver);
	this.exceptionCacheResolver = SingletonSupplier.ofNullable(exceptionCacheResolver);
	this.keyGenerator = new SingletonSupplier<>(keyGenerator, SimpleKeyGenerator::new);
}
 
Example 13
Source Project: java-technology-stack   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(this.cache).get(key);

	this.simpleService.get(0L);
	verify(this.errorHandler).handleCacheGetError(exception, this.cache, key);
}
 
Example 14
Source Project: java-technology-stack   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getPutNewElementFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
	Object key = SimpleKeyGenerator.generateKey(0L);
	given(this.cache.get(key)).willReturn(null);
	willThrow(exception).given(this.cache).put(key, 0L);

	this.simpleService.get(0L);
	verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 0L);
}
 
Example 15
Source Project: java-technology-stack   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void putFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(this.cache).put(key, 234L);

	this.simpleService.put(0L, 234L);
	verify(this.errorHandler).handleCachePutError(exception, this.cache, key, 234L);
}
 
Example 16
Source Project: java-technology-stack   Source File: JCacheErrorHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void evictFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(this.cache).evict(key);

	this.simpleService.evict(0L);
	verify(this.errorHandler).handleCacheEvictError(exception, this.cache, key);
}
 
Example 17
Source Project: java-technology-stack   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper == null) {
		throw new AssertionError("Excepted value to be put in cache with key " + key);
	}
	Object actual = valueWrapper.get();
	if (value != actual) { // instance check on purpose
		throw new AssertionError("Wrong value set in cache with key " + key + ". " +
				"Expected=" + value + ", but got=" + actual);
	}
}
 
Example 18
Source Project: java-technology-stack   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper != null) {
		throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
	}
}
 
Example 19
Source Project: java-technology-stack   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper == null) {
		throw new AssertionError("Excepted value to be put in cache with key " + key);
	}
	Object actual = valueWrapper.get();
	if (value != actual) { // instance check on purpose
		throw new AssertionError("Wrong value set in cache with key " + key + ". " +
				"Expected=" + value + ", but got=" + actual);
	}
}
 
Example 20
Source Project: java-technology-stack   Source File: AnnotatedJCacheableService.java    License: MIT License 5 votes vote down vote up
@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper != null) {
		throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
	}
}
 
Example 21
@Test
public void getFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(cache).get(key);

	this.simpleService.get(0L);
	verify(errorHandler).handleCacheGetError(exception, cache, key);
}
 
Example 22
@Test
public void putFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(cache).put(key, 234L);

	this.simpleService.put(0L, 234L);
	verify(errorHandler).handleCachePutError(exception, cache, key, 234L);
}
 
Example 23
@Test
public void evictFail() {
	UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
	Object key = SimpleKeyGenerator.generateKey(0L);
	willThrow(exception).given(cache).evict(key);

	this.simpleService.evict(0L);
	verify(errorHandler).handleCacheEvictError(exception, cache, key);
}
 
Example 24
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper == null) {
		throw new AssertionError("Excepted value to be put in cache with key " + key);
	}
	Object actual = valueWrapper.get();
	if (value != actual) { // instance check on purpose
		throw new AssertionError("Wrong value set in cache with key " + key + ". " +
				"Expected=" + value + ", but got=" + actual);
	}
}
 
Example 25
@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper != null) {
		throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
	}
}
 
Example 26
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper == null) {
		throw new AssertionError("Excepted value to be put in cache with key " + key);
	}
	Object actual = valueWrapper.get();
	if (value != actual) { // instance check on purpose
		throw new AssertionError("Wrong value set in cache with key " + key + ". " +
				"Expected=" + value + ", but got=" + actual);
	}
}
 
Example 27
@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
	Object key = SimpleKeyGenerator.generateKey(id);
	Cache.ValueWrapper valueWrapper = defaultCache.get(key);
	if (valueWrapper != null) {
		throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
	}
}
 
Example 28
@Override
// Exception squid:S923 - override
@SuppressWarnings({ "squid:S923" })
public Object generate(final Object target, final Method method, final Object... params) {
    final String initialTenantCreation = createInitialTenant.get();
    if (initialTenantCreation == null) {
        return SimpleKeyGenerator.generateKey(tenantAware.getCurrentTenant().toUpperCase(),
                tenantAware.getCurrentTenant().toUpperCase());
    }
    return SimpleKeyGenerator.generateKey(initialTenantCreation.toUpperCase(),
            initialTenantCreation.toUpperCase());
}
 
Example 29
Source Project: cloudbreak   Source File: CachingConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object generate(Object target, Method method, Object... params) {
    if (params.length == 1) {
        if (params[0] == null) {
            return SimpleKey.EMPTY;
        }
        CacheDefinition cacheDefinition = classCacheDefinitionMap.get(params[0].getClass());
        if (cacheDefinition != null) {
            return cacheDefinition.generateKey(target, method, params);
        }
    }
    return SimpleKeyGenerator.generateKey(params);
}
 
Example 30
Source Project: cloudbreak   Source File: CachingConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object generate(Object target, Method method, Object... params) {
    if (params.length == 1) {
        if (params[0] == null) {
            return SimpleKey.EMPTY;
        }
        CacheDefinition cacheDefinition = classCacheDefinitionMap.get(params[0].getClass());
        if (cacheDefinition != null) {
            return cacheDefinition.generateKey(target, method, params);
        }
    }
    return SimpleKeyGenerator.generateKey(params);
}