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

The following examples show how to use org.springframework.cache.interceptor.SimpleKey. 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: conf4j   Source File: CachingTypeConverter.java    License: MIT License 6 votes vote down vote up
/**
 * Converts String to the target type.
 * This method checks whether the cache contains the converted value, and if not, obtains it from {@link #typeConverter},
 * stores conversion result in the cache and provides the result.
 *
 * @param type  actual type definition.
 * @param value string representation of the value which is converted to {@code T}.
 *              In case it is {@code null}, converter should return either {@code null} or a value
 *              that is equivalent e.g. an empty list.
 * @return value converted to type {@code T}.
 * @param attributes additional meta-data attributes which may be used by converter. It can be {@code null}.
 * @throws IllegalArgumentException when {@code value} cannot be converted to {@code T}.
 * @throws NullPointerException     when {@code type} is {@code null}.
 */
@SuppressWarnings("unchecked")
@Override
public T fromString(Type type, String value, Map<String, String> attributes) {
    requireNonNull(type, "type cannot be null");

    SimpleKey key = new SimpleKey(type, attributes, value);
    ValueWrapper valueWrapper = cache.get(key);

    if (valueWrapper != null) {
        return (T) valueWrapper.get();
    } else {
        T val = typeConverter.fromString(type, value, attributes);
        valueWrapper = cache.putIfAbsent(key, val);
        if (valueWrapper == null) {
            return val;
        } else {
            return (T) valueWrapper.get();
        }
    }
}
 
Example 2
Source Project: memcached-spring-boot   Source File: MemcachedCacheIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void whenFindAllThenBooksCached() {
    List<Book> books = bookService.findAll();

    assertThat(books).isNotNull();
    assertThat(bookService.getCounterFindAll()).isEqualTo(1);

    bookService.findAll();
    bookService.findAll();
    assertThat(bookService.getCounterFindAll()).isEqualTo(1);

    Cache booksCache = cacheManager.getCache("books");
    Object value = booksCache.get(SimpleKey.EMPTY);

    assertThat(value).isNotNull();
}
 
Example 3
Source Project: memcached-spring-boot   Source File: MemcachedCacheIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void whenTimeoutThenAuthorCacheExpired() throws InterruptedException {
    List<Author> authors = authorService.findAll();

    assertThat(authors).isNotNull();
    assertThat(authorService.getCounterFindAll()).isEqualTo(1); // first time read from DB

    authorService.findAll();
    assertThat(authorService.getCounterFindAll()).isEqualTo(1);

    Thread.sleep(1000 * 5L);

    Object value = cacheManager.getCache("authors").get(SimpleKey.EMPTY);
    assertThat(value).isNull();

    authors = authorService.findAll();

    assertThat(authors).isNotNull();
    assertThat(authorService.getCounterFindAll()).isEqualTo(2); // second time read from DB

    value = cacheManager.getCache("authors").get(SimpleKey.EMPTY);
    assertThat(value).isNotNull();
}
 
Example 4
Source Project: spring-analysis-note   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getSimple() {
	this.keyGenerator.expect(1L);
	Object first = this.simpleService.get(1L);
	Object second = this.simpleService.get(1L);
	assertSame(first, second);

	Object key = new SimpleKey(1L);
	assertEquals(first, cache.get(key).get());
}
 
Example 5
Source Project: spring-analysis-note   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getFlattenVararg() {
	this.keyGenerator.expect(1L, "foo", "bar");
	Object first = this.simpleService.get(1L, "foo", "bar");
	Object second = this.simpleService.get(1L, "foo", "bar");
	assertSame(first, second);

	Object key = new SimpleKey(1L, "foo", "bar");
	assertEquals(first, cache.get(key).get());
}
 
Example 6
Source Project: spring-analysis-note   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getFiltered() {
	this.keyGenerator.expect(1L);
	Object first = this.simpleService.getFiltered(1L, "foo", "bar");
	Object second = this.simpleService.getFiltered(1L, "foo", "bar");
	assertSame(first, second);

	Object key = new SimpleKey(1L);
	assertEquals(first, cache.get(key).get());
}
 
Example 7
Source Project: spring-analysis-note   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Override
public Object generate(Object target, Method method, Object... params) {
	assertTrue("Unexpected parameters: expected: "
					+ Arrays.toString(this.expectedParams) + " but got: " + Arrays.toString(params),
			Arrays.equals(expectedParams, params));
	return new SimpleKey(params);
}
 
Example 8
Source Project: java-technology-stack   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getSimple() {
	this.keyGenerator.expect(1L);
	Object first = this.simpleService.get(1L);
	Object second = this.simpleService.get(1L);
	assertSame(first, second);

	Object key = new SimpleKey(1L);
	assertEquals(first, cache.get(key).get());
}
 
Example 9
Source Project: java-technology-stack   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getFlattenVararg() {
	this.keyGenerator.expect(1L, "foo", "bar");
	Object first = this.simpleService.get(1L, "foo", "bar");
	Object second = this.simpleService.get(1L, "foo", "bar");
	assertSame(first, second);

	Object key = new SimpleKey(1L, "foo", "bar");
	assertEquals(first, cache.get(key).get());
}
 
Example 10
Source Project: java-technology-stack   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void getFiltered() {
	this.keyGenerator.expect(1L);
	Object first = this.simpleService.getFiltered(1L, "foo", "bar");
	Object second = this.simpleService.getFiltered(1L, "foo", "bar");
	assertSame(first, second);

	Object key = new SimpleKey(1L);
	assertEquals(first, cache.get(key).get());
}
 
Example 11
Source Project: java-technology-stack   Source File: JCacheKeyGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Override
public Object generate(Object target, Method method, Object... params) {
	assertTrue("Unexpected parameters: expected: "
					+ Arrays.toString(this.expectedParams) + " but got: " + Arrays.toString(params),
			Arrays.equals(expectedParams, params));
	return new SimpleKey(params);
}
 
Example 12
@Override
@Nullable
public byte[] serialize(Object object) {
	Objects.requireNonNull(object, "redis key is null");
	String key;
	if (object instanceof SimpleKey) {
		key = "";
	} else if (object instanceof String) {
		key = (String) object;
	} else {
		key = converter.convert(object, String.class);
	}
	return key.getBytes(this.charset);
}
 
Example 13
@Test
public void getSimple() {
	this.keyGenerator.expect(1L);
	Object first = this.simpleService.get(1L);
	Object second = this.simpleService.get(1L);
	assertSame(first, second);

	Object key = new SimpleKey(1L);
	assertEquals(first, cache.get(key).get());
}
 
Example 14
@Test
public void getFlattenVararg() {
	this.keyGenerator.expect(1L, "foo", "bar");
	Object first = this.simpleService.get(1L, "foo", "bar");
	Object second = this.simpleService.get(1L, "foo", "bar");
	assertSame(first, second);

	Object key = new SimpleKey(1L, "foo", "bar");
	assertEquals(first, cache.get(key).get());
}
 
Example 15
@Test
public void getFiltered() {
	this.keyGenerator.expect(1L);
	Object first = this.simpleService.getFiltered(1L, "foo", "bar");
	Object second = this.simpleService.getFiltered(1L, "foo", "bar");
	assertSame(first, second);

	Object key = new SimpleKey(1L);
	assertEquals(first, cache.get(key).get());
}
 
Example 16
@Override
public Object generate(Object target, Method method, Object... params) {
	assertTrue("Unexpected parameters: expected: "
					+ Arrays.toString(this.expectedParams) + " but got: " + Arrays.toString(params),
			Arrays.equals(expectedParams, params));
	return new SimpleKey(params);
}
 
Example 17
Source Project: memcached-spring-boot   Source File: MemcachedCacheIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void whenFindAllThenAuthorsCached() {
    List<Author> authors = authorService.findAll();

    assertThat(authors).isNotNull();
    assertThat(authorService.getCounterFindAll()).isEqualTo(1);

    authorService.findAll();
    authorService.findAll();
    assertThat(authorService.getCounterFindAll()).isEqualTo(1);

    Object value = cacheManager.getCache("authors").get(SimpleKey.EMPTY);
    assertThat(value).isNotNull();
}
 
Example 18
Source Project: memcached-spring-boot   Source File: MemcachedCacheIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void whenClearThenOnlyBooksEvicted() {
    bookService.findAll();
    authorService.findAll();

    bookService.clear();

    Object value = cacheManager.getCache("books").get(SimpleKey.EMPTY);
    assertThat(value).isNull();
    value = cacheManager.getCache("authors").get(SimpleKey.EMPTY);
    assertThat(value).isNotNull();
}
 
Example 19
Source Project: cloudbreak   Source File: AwsCachingConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object generateKey(Object target, Method method, Object... params) {
    if (params.length == 1) {
        AwsCredentialView param = (AwsCredentialView) params[0];
        return param.getCredentialCrn() != null ? param.getCredentialCrn() : SimpleKey.EMPTY;
    }
    return SimpleKey.EMPTY;
}
 
Example 20
Source Project: cloudbreak   Source File: AwsCredentialCachingConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object generateKey(Object target, Method method, Object... params) {
    if (params.length == 1) {
        AwsCredentialView param = (AwsCredentialView) params[0];
        if (param.getRoleArn() != null) {
            return param.getRoleArn();
        } else if (param.getAccessKey() != null) {
            return param.getAccessKey();
        }
    }
    return SimpleKey.EMPTY;
}
 
Example 21
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 22
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 23
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);
}