org.springframework.cache.interceptor.SimpleKey Java Examples

The following examples show how to use org.springframework.cache.interceptor.SimpleKey. 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 File: MemcachedCacheIT.java    From memcached-spring-boot with 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 #2
Source File: CachingTypeConverter.java    From conf4j with 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 #3
Source File: MemcachedCacheIT.java    From memcached-spring-boot with 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 #4
Source File: JCacheKeyGeneratorTests.java    From spring4-understanding with Apache License 2.0 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 #5
Source File: CachingConfig.java    From cloudbreak with 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 #6
Source File: CachingConfig.java    From cloudbreak with 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 #7
Source File: CachingConfig.java    From cloudbreak with 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 #8
Source File: AwsCredentialCachingConfig.java    From cloudbreak with 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 #9
Source File: AwsCachingConfig.java    From cloudbreak with 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 #10
Source File: MemcachedCacheIT.java    From memcached-spring-boot with 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 #11
Source File: MemcachedCacheIT.java    From memcached-spring-boot with 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 #12
Source File: JCacheKeyGeneratorTests.java    From spring4-understanding with Apache License 2.0 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 #13
Source File: JCacheKeyGeneratorTests.java    From spring4-understanding with Apache License 2.0 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 #14
Source File: JCacheKeyGeneratorTests.java    From spring-analysis-note with 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 #15
Source File: JCacheKeyGeneratorTests.java    From spring4-understanding with Apache License 2.0 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 #16
Source File: RedisKeySerializer.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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 #17
Source File: JCacheKeyGeneratorTests.java    From java-technology-stack with 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 #18
Source File: JCacheKeyGeneratorTests.java    From java-technology-stack with 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 #19
Source File: JCacheKeyGeneratorTests.java    From java-technology-stack with 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 #20
Source File: JCacheKeyGeneratorTests.java    From java-technology-stack with 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 #21
Source File: JCacheKeyGeneratorTests.java    From spring-analysis-note with 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 #22
Source File: JCacheKeyGeneratorTests.java    From spring-analysis-note with 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 #23
Source File: JCacheKeyGeneratorTests.java    From spring-analysis-note with 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());
}