类org.springframework.cache.interceptor.SimpleKey源码实例Demo

下面列出了怎么用org.springframework.cache.interceptor.SimpleKey的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: conf4j   文件: CachingTypeConverter.java
/**
 * 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();
        }
    }
}
 
源代码2 项目: memcached-spring-boot   文件: MemcachedCacheIT.java
@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();
}
 
源代码3 项目: memcached-spring-boot   文件: MemcachedCacheIT.java
@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();
}
 
@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());
}
 
@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());
}
 
@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());
}
 
@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);
}
 
@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());
}
 
@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());
}
 
@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());
}
 
@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);
}
 
源代码12 项目: blade-tool   文件: RedisKeySerializer.java
@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);
}
 
@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());
}
 
@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());
}
 
@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());
}
 
@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);
}
 
源代码17 项目: memcached-spring-boot   文件: MemcachedCacheIT.java
@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();
}
 
源代码18 项目: memcached-spring-boot   文件: MemcachedCacheIT.java
@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();
}
 
源代码19 项目: cloudbreak   文件: AwsCachingConfig.java
@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;
}
 
源代码20 项目: cloudbreak   文件: AwsCredentialCachingConfig.java
@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;
}
 
源代码21 项目: cloudbreak   文件: CachingConfig.java
@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);
}
 
源代码22 项目: cloudbreak   文件: CachingConfig.java
@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);
}
 
源代码23 项目: cloudbreak   文件: CachingConfig.java
@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);
}
 
 类方法
 同包方法