下面列出了怎么用org.springframework.cache.interceptor.SimpleKey的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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();
}
}
}
@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();
}
@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);
}
@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);
}
@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();
}
@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();
}
@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;
}
@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;
}
@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);
}
@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);
}
@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);
}