当前位置:网站首页>Local cache selection (guava/caffeine/ohc) and performance comparison
Local cache selection (guava/caffeine/ohc) and performance comparison
2022-06-24 00:59:00 【User 7255712】
1 Guava
1.1) Based on using
@Slf4j
public class GuavaCache {
private static Cache<String, Object> cache = CacheBuilder.newBuilder()
.maximumSize(1000000)
.expireAfterWrite(60, TimeUnit.SECONDS)
.concurrencyLevel(4)
.initialCapacity(1000)
// Configuration recordStats,cache.stats() To take effect
//.recordStats()
.removalListener(new RemovalListener<String, Object>() {
@Override
public void onRemoval(RemovalNotification<String, Object> rn) {
}
}).build();
/*
*
* @desction: Access to the cache
*/
public static Object get(String key) {
try {
return StringUtils.isNotEmpty(key) ? cache.getIfPresent(key) : null;
} catch (Exception e) {
log.error("local cache by featureId abnormal ", e);
return null;
}
}
/*
*
* @desction: Put into cache
*/
public static void put(String key, Object value) {
if (StringUtils.isNotEmpty(key) && value != null) {
cache.put(key, value);
}
}
/*
*
* @desction: Remove the cache
*/
public static void remove(String key) {
if (StringUtils.isNotEmpty(key)) {
cache.invalidate(key);
}
}
/*
*
* @desction: Bulk delete cache
*/
public static void remove(List<String> keys) {
if (keys != null && keys.size() > 0) {
cache.invalidateAll(keys);
}
}
public static CacheStats getStats() {
return cache.stats();
}
/**
* test
*
* @param args
*/
public static void main(String[] args) {
long start = System.currentTimeMillis();
// Test write only
for (int j = 0; j < 500000; j++) {
GuavaCache.put("" + j, j);
}
// Test reading and writing
for (int j = 0; j < 500000; j++) {
GuavaCache.get("" + j);
}
// Reading and writing + Read as hit
for (int j = 0; j < 500000; j++) {
GuavaCache.get("" + j + "noHits");
}
GuavaCache.cache.cleanUp();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}2 CaffeineCache
2.1 Based on using
@Slf4j
public class CaffeineCache {
private static Cache<String, Object> caffeineCache = Caffeine.newBuilder()
.maximumSize(1000000)
.expireAfterWrite(60, TimeUnit.SECONDS)
.initialCapacity(1000)
// Configuration recordStats,cache.stats() To take effect
//.recordStats()
.removalListener(new RemovalListener<String, Object>() {
@Override
public void onRemoval(@Nullable String key, @Nullable Object value, @NonNull RemovalCause cause) {
}
})
.build();
/*
*
* @desction: Access to the cache
*/
public static Object get(String key) {
try {
return StringUtils.isNotEmpty(key) ? caffeineCache.getIfPresent(key) : null;
} catch (Exception e) {
log.error("local cache by featureId abnormal ", e);
return null;
}
}
/*
*
* @desction: Put into cache
*/
public static void put(String key, Object value) {
if (StringUtils.isNotEmpty(key) && value != null) {
caffeineCache.put(key, value);
}
}
/*
*
* @desction: Remove the cache
*/
public static void remove(String key) {
if (StringUtils.isNotEmpty(key)) {
caffeineCache.invalidate(key);
}
}
/*
*
* @desction: Bulk delete cache
*/
public static void remove(List<String> keys) {
if (keys != null && keys.size() > 0) {
caffeineCache.invalidateAll(keys);
}
}
public static CacheStats getStats() {
return caffeineCache.stats();
}
/**
* test
*
* @param args
*/
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
// Test write only
for (int j = 0; j < 500000; j++) {
CaffeineCache.put("" + j, j);
}
// Test reading and writing
for (int j = 0; j < 500000; j++) {
CaffeineCache.get("" + j);
}
// Reading and writing + Read as hit
for (int j = 0; j < 500000; j++) {
CaffeineCache.get("" + j + "noHits");
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}3 Off heap caching Ohc
@Slf4j
public class OhcCache {
private static OHCache<String, String> ohCache = OHCacheBuilder.<String, String>newBuilder()
.keySerializer(new OhcStringSerializer())
.valueSerializer(new OhcStringSerializer())
//.hashMode(HashAlgorithm.CRC32C)
// Unit is byte , Default 2GB Space
.capacity(2 * 1024 * 1024 * 1024L)
.timeouts(true)
.defaultTTLmillis(600 * 1000)
.eviction(Eviction.LRU)
.build();
/**
* Set the value
*
* @param k
* @param v
* @return
*/
public static boolean put(String k, String v) {
return put(k, v, 9223372036854775807L);
}
public static boolean put(String k, String v, Long time) {
try {
return ohCache.put(k, v, time);
} catch (Exception e) {
log.error("ohc cache put error", e);
return false;
}
}
public static String get(String k) {
return ohCache.get(k);
}
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
// Test write only
for (int j = 0; j < 500000; j++) {
OhcCache.put("" + j, j + "");
}
System.out.println(" Writing takes time :" + (System.currentTimeMillis() - start));
// Test reading and writing
for (int j = 0; j < 500000; j++) {
OhcCache.get("" + j);
}
System.out.println(" Read hit time :" + (System.currentTimeMillis() - start));
// Reading and writing + Read as hit
for (int j = 0; j < 500000; j++) {
OhcCache.get("" + j + "noHits");
}
System.out.println(" Read miss time :" + (System.currentTimeMillis() - start));
System.out.println(" Total time :" + (System.currentTimeMillis() - start));
}
}4 Performance comparison
Comparative data
type | 50 Ten thousand words : Time consuming :ms | 50 Write +50 read ( Read all hits ): Time consuming :ms | 50 Wan write +50 Wan Du ( Full hit )+50 Wan Du ( Not hit ): Time consuming :ms | 50 Wan Du +50 Ten thousand misses |
|---|---|---|---|---|
Guava | 329/340/326/328/328 | 536/518/546/525/558 | 647/646/638/668/641 | 490/501/482/485/492 |
Caffeine | 292/284/270/279/267 | 414/382/353/385/361 | 479/513/460/487/481 | 343/326/333/336/369 |
Ohc | 448/433/430/446/442 | 763/748/765/741/705 | 918/947/901/964/903 | 653/676/607/639/704 |
Ohc-Obj | 1343/1315/1217/1249/1193 | 1910/1830/1849/1803/1786 | 1979/1965/1947/1968/1946 | 1487/1573/1499/1491/1483 |
summary
Whether reading or writing ,Caffeine Performance ratio Guava It is better to . Caffeine be based on java8 A high performance , Near optimal cache Library . Caffeine Memory cache usage reference provided Google guava Of API. Caffeine Is based on Google guava and ConcurrentLinkedHashMap The results of improvement in design experience . Caffeine yes Spring 5 Default supported Cache, so Spring The importance of it ,Spring abandon Guava Turned to Caffeine.
5 Other advanced uses
5.1 Usage Summary
Automatically load entities into the cache asynchronously Size based recycling policy Time based recycling strategy Automatically refresh key Automatically encapsulate virtual references value Automatically encapsulate weak or soft references Notification that an entity has expired or been deleted Write external resources Statistics cumulative access cache
5.2 Load policy
Manual loading
// Retrieve one entry, If not, it is null Graph graph = cache.getIfPresent(key); // Retrieve one entry, If entry by null, Through key Create a entry And add cache graph = cache.get(key, k -> createExpensiveGraph(key)); // Insert or update an entity cache.put(key, graph); // Remove an entity cache.invalidate(key);
Synchronous loading
structure Cache When ,build Method passes in a CacheLoader Implementation class . Realization load Method , adopt key load value.
.build(key -> createExpensiveGraph(key));
Load asynchronously
.buildAsync((key, executor) -> createExpensiveGraphAsync(key, executor));
5.3 Recovery strategy
Caffeine Provides 3 Two recycling strategies : Size based recycling , Time based recycling , Based on reference recycling
5.4 External storage : For writing to the database / Multi level cache synchronization
// adopt CacheWriter The cache can be written back to external storage .
LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
.writer(new CacheWriter<Key, Graph>() {
@Override public void write(Key key, Graph graph) {
// Write to external storage or L2 cache
}
@Override public void delete(Key key, Graph graph, RemovalCause cause) {
// Delete external storage or L2 cache
}
})
.build(key -> createExpensiveGraph(key));5.5 Statistics of cache usage
By using Caffeine.recordStats(), It can be transformed into a set of Statistics . adopt Cache.stats() Return to one CacheStats. CacheStats Provide the following statistical methods ps:// Configuration recordStats----cache.stats() To take effect hitRate(): Returns the cache hit ratio evictionCount(): Number of cache recycles averageLoadPenalty(): Average time to load new values
6 other : Why? Caffeine Than Guava good
Other pressure measurement references :https://github.com/ben-manes/caffeine/wiki/Benchmarks
边栏推荐
猜你喜欢

Apple iphone14 is equipped with Beidou navigation system. What are the advantages of Beidou vs GPS?

小猫爪:PMSM之FOC控制15-MRAS法

js输入输出语句,变量

What are the two types of digital factories
![Graduation project - thesis writing notes [design topic type, thesis writing details, design materials]](/img/66/c0c400609b56dd012d87c620ca66e4.png)
Graduation project - thesis writing notes [design topic type, thesis writing details, design materials]

MIP nerf: anti aliasing multiscale neural radiation field iccv2021

【CVPR 2022】高分辨率小目标检测:Cascaded Sparse Query for Accelerating High-Resolution Smal Object Detection

用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧

飞桨产业级开源模型库:加速企业AI任务开发与应用

How to get started with machine learning?
随机推荐
13 `bs_ duixiang. Tag tag ` get a tag object
What are the two types of digital factories
Cross domain and jsonp
Social recruitment interview is indispensable -- 1000 interview questions for Android engineers from Internet companies
Using anydesk remote control for intranet penetration horizontal movement
setfacl命令的基本用法
Arm learning (7) symbol table and debugging
13 `bs_duixiang.tag标签`得到一个tag对象
Error reported using worker: uncaught domexception: failed to construct 'worker': script at***
Social order in the meta universe
[CVPR 2020 oral] a physics based noise formation model for extreme low light raw denoising
How many of the 36 difficult points of activity do you know?, Android interview 2020
DML operation
[applet] when compiling the preview applet, a -80063 error prompt appears
js输入输出语句,变量
version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)
逻辑的定义
Security | warm tips: security incidents on the cloud have intensified recently. Please do a good job in backup monitoring of cloud security protection!
毕业设计-论文写作笔记【毕设题目类型、论文写作细节、毕设资料】
Is it safe to open an account online? What conditions need to be met?