This paper background
Caching is an absolute tool for dealing with high concurrency, and can be used to provide performance when many business scenarios allow it.
Since the cache is used, monitoring the cache is essential. For example, cache loading time, new time and so on.
In JetCache, there is no problem with the Cache of Redis. After the Cache is buried, the Key is intact, which is the name+ Key of the Cache, as shown in the following figure:
In addition to embedding the Cache of Redis, Caffeine also has a embedding operation for the local Cache. It is found that Caffeine has a embedding problem. The problem is that the name of the Cache is lost.
Then I went to the official tacks group and asked the maintenance staff to update the version. Then I upgrade to the latest 2.6.0 or not, this is not pit me 😂!
Find out why
The so-called do-it-yourself, abundant food and clothing. The Cache name is not lost because the Redis Config contains the keyPrefix.
Then when the operation is performed on Redis, the cached Key will be built, and the Key will be built with the keyPrefix, so the Redis Key is normal.
com.alicp.jetcache.external.AbstractExternalCache
public byte[] buildKey(K key) {
try {
Object newKey = key;
if (key instanceof byte[]) {
newKey = key;
} else if (key instanceof String) {
newKey = key;
} else if (this.config.getKeyConvertor() != null) {
newKey = this.config.getKeyConvertor().apply(key);
}
return ExternalKeyUtil.buildKeyAfterConvert(newKey, this.config.getKeyPrefix());
} catch (IOException var3) {
throw new CacheException(var3);
}
}
Copy the code
Caffeine does not have a Keyprefix in its config.
And then when you build the cache Key, there’s no Keyprefix, so that’s where the problem is.
com.alicp.jetcache.embedded.AbstractEmbeddedCache
public Object buildKey(K key) {
if (key == null) {
return null;
} else {
Object newKey = key;
Function<K, Object> keyConvertor = this.config.getKeyConvertor();
if (keyConvertor != null) {
newKey = keyConvertor.apply(key);
}
return newKey;
}
}
Copy the code
RedisCacheConfig inherits ExternalCacheConfig, and ExternalCacheConfig inherits CacheConfig.
KeyPrefix is defined in ExternalCacheConfig.
EmbeddedCacheConfig only inherits CacheConfig, so it naturally has no keyPrefix field.
The solution
We’ve got the cause. We can fix it. The problem is that it’s an open source framework, not in-house code. However, you can directly clone the source code, transform it, and then package it into your private server.
You can pass the keyPrefix through by inheriting ExternalCacheConfig as well, and then concatenate it at buildKey.
As an alternative to changing the relationship between the configuration classes, config contains information for monitors that will store the cached monitoring information. This information will record the types of actions (GET, PUT, etc.) that will be cached, and then the time when each action will be executed. Statistics such as the number of operations, and eventually a thread will periodically output this information to the log.
So we can solve this problem temporarily by getting cacheName from monitors.
private String getCacheName() {
List<CacheMonitor> monitors = config.getMonitors();
if (CollectionUtils.isEmpty(monitors)) {
return "";
}
DefaultCacheMonitor cacheMonitor = (DefaultCacheMonitor) monitors.get(0);
String cacheName = cacheMonitor.getCacheName();
return cacheName;
}
Copy the code
Function code: github.com/yinjihuan/k…
About the author: Yin Jihuan, a simple technology lover, the author of Spring Cloud Micro-service – Full stack Technology and Case Analysis, Spring Cloud Micro-service Introduction combat and Progress, the founder of the public account Monkey World. Personal wechat jihuan900, welcome to hook up.
I have organized a very complete learning material. If you are interested, you can search “Monkey World” on wechat and reply to the keyword “learning material” to obtain the Spring Cloud, Spring Cloud Alibaba, Sharing-JDBC sub-library sub-table and task scheduling framework XXL-job that I have organized. MongoDB, crawler and other related information.