-
Notifications
You must be signed in to change notification settings - Fork 6
coderdream_2022050602
CoderDream edited this page May 6, 2022
·
1 revision
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.caffinitas.ohc/ohc-core -->
<dependency>
<groupId>org.caffinitas.ohc</groupId>
<artifactId>ohc-core</artifactId>
<version>0.7.4</version>
</dependency>
package com.example.demo.compare;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
@Slf4j
public class CaffeineCache {
private static Cache<String, Object> caffeineCache = Caffeine.newBuilder()
.maximumSize(1000000)
.expireAfterWrite(60, TimeUnit.SECONDS)
.initialCapacity(1000)
//配置上recordStats,cache.stats()才能生效
//.recordStats()
.removalListener(new RemovalListener<String, Object>() {
@Override
public void onRemoval(@Nullable String key, @Nullable Object value, @NonNull RemovalCause cause) {
}
})
.build();
/*
*
* @desction: 获取缓存
*/
public static Object get(String key) {
try {
return StringUtils.isNotEmpty(key) ? caffeineCache.getIfPresent(key) : null;
} catch (Exception e) {
log.error("local cache by featureId 异常", e);
return null;
}
}
/*
*
* @desction: 放入缓存
*/
public static void put(String key, Object value) {
if (StringUtils.isNotEmpty(key) && value != null) {
caffeineCache.put(key, value);
}
}
/*
*
* @desction: 移除缓存
*/
public static void remove(String key) {
if (StringUtils.isNotEmpty(key)) {
caffeineCache.invalidate(key);
}
}
/*
*
* @desction: 批量删除缓存
*/
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();
//只测试写入
for (int j = 0; j < 500000; j++) {
CaffeineCache.put("" + j, j);
}
//测试读写
for (int j = 0; j < 500000; j++) {
CaffeineCache.get("" + j);
}
//读写+读为命中
for (int j = 0; j < 500000; j++) {
CaffeineCache.get("" + j + "noHits");
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
package com.example.demo.compare;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j
public class GuavaCache {
private static Cache<String, Object> cache = CacheBuilder.newBuilder()
.maximumSize(1000000)
.expireAfterWrite(60, TimeUnit.SECONDS)
.concurrencyLevel(4)
.initialCapacity(1000)
//配置上recordStats,cache.stats()才能生效
//.recordStats()
.removalListener(new RemovalListener<String, Object>() {
@Override
public void onRemoval(RemovalNotification<String, Object> rn) {
}
})
.build();
/*
*
* @desction: 获取缓存
*/
public static Object get(String key) {
try {
return StringUtils.isNotEmpty(key) ? cache.getIfPresent(key) : null;
} catch (Exception e) {
log.error("local cache by featureId 异常", e);
return null;
}
}
/*
*
* @desction: 放入缓存
*/
public static void put(String key, Object value) {
if (StringUtils.isNotEmpty(key) && value != null) {
cache.put(key, value);
}
}
/*
*
* @desction: 移除缓存
*/
public static void remove(String key) {
if (StringUtils.isNotEmpty(key)) {
cache.invalidate(key);
}
}
/*
*
* @desction: 批量删除缓存
*/
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();
//只测试写入
for (int j = 0; j < 500000; j++) {
GuavaCache.put("" + j, j);
}
//测试读写
for (int j = 0; j < 500000; j++) {
GuavaCache.get("" + j);
}
//读写+读为命中
for (int j = 0; j < 500000; j++) {
GuavaCache.get("" + j + "noHits");
}
GuavaCache.cache.cleanUp();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
package com.example.demo.compare;
import lombok.extern.slf4j.Slf4j;
import org.caffinitas.ohc.Eviction;
import org.caffinitas.ohc.OHCache;
import org.caffinitas.ohc.OHCacheBuilder;
@Slf4j
public class OhcCache {
private static OHCache<String, String> ohCache = OHCacheBuilder.<String, String>newBuilder()
.keySerializer(new OhcStringSerializer())
.valueSerializer(new OhcStringSerializer())
//.hashMode(HashAlgorithm.CRC32C)
//单位是字节,默认2GB空间
.capacity(2 * 1024 * 1024 * 1024L)
.timeouts(true)
.defaultTTLmillis(600 * 1000)
.eviction(Eviction.LRU)
.build();
/**
* 设置值
*
* @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();
//只测试写入
for (int j = 0; j < 500000; j++) {
OhcCache.put("" + j, j + "");
}
System.out.println("写入耗时:" + (System.currentTimeMillis() - start));
//测试读写
for (int j = 0; j < 500000; j++) {
OhcCache.get("" + j);
}
System.out.println("读取命中耗时:" + (System.currentTimeMillis() - start));
//读写+读为命中
for (int j = 0; j < 500000; j++) {
OhcCache.get("" + j + "noHits");
}
System.out.println("读取未命中耗时:" + (System.currentTimeMillis() - start));
System.out.println("总耗时:" + (System.currentTimeMillis() - start));
}
}