java如何缓存数据
Java 缓存数据的常见方法
使用 HashMap 实现简单缓存
import java.util.HashMap;
import java.util.Map;
public class SimpleCache<K, V> {
private final Map<K, V> cache = new HashMap<>();
public void put(K key, V value) {
cache.put(key, value);
}
public V get(K key) {
return cache.get(key);
}
public void remove(K key) {
cache.remove(key);
}
}
使用 Guava Cache
Guava Cache 提供了更强大的缓存功能,包括过期策略和缓存大小限制。
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
public class GuavaCacheExample {
private static final Cache<String, String> cache = CacheBuilder.newBuilder()
.maximumSize(100) // 最大缓存条目数
.expireAfterWrite(10, TimeUnit.MINUTES) // 写入后10分钟过期
.build();
public static void put(String key, String value) {
cache.put(key, value);
}
public static String get(String key) {
return cache.getIfPresent(key);
}
}
使用 Caffeine Cache
Caffeine 是一个高性能的 Java 缓存库,功能比 Guava Cache 更强大。
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
public class CaffeineCacheExample {
private static final Cache<String, String> cache = Caffeine.newBuilder()
.maximumSize(100)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build();
public static void put(String key, String value) {
cache.put(key, value);
}
public static String get(String key) {
return cache.getIfPresent(key);
}
}
使用 Ehcache
Ehcache 是一个广泛使用的开源 Java 缓存框架。
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
public class EhcacheExample {
private static CacheManager cacheManager;
private static Cache<String, String> cache;
static {
cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
cacheManager.init();
cache = cacheManager.createCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class, String.class,
ResourcePoolsBuilder.heap(100)));
}
public static void put(String key, String value) {
cache.put(key, value);
}
public static String get(String key) {
return cache.get(key);
}
}
使用 Spring Cache 注解
Spring 框架提供了缓存抽象,可以通过注解方式轻松实现缓存。
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@Cacheable("books")
public Book getBookByIsbn(String isbn) {
// 这个方法只有在缓存中没有对应数据时才会执行
return findBookInSlowSource(isbn);
}
private Book findBookInSlowSource(String isbn) {
// 模拟耗时操作
return new Book(isbn, "Some book");
}
}
使用 Redis 作为分布式缓存
对于分布式系统,可以使用 Redis 作为缓存解决方案。
import redis.clients.jedis.Jedis;
public class RedisCacheExample {
private static final String REDIS_HOST = "localhost";
private static final int REDIS_PORT = 6379;
public static void set(String key, String value) {
try (Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT)) {
jedis.set(key, value);
}
}
public static String get(String key) {
try (Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT)) {
return jedis.get(key);
}
}
}
缓存策略考虑因素
- 缓存大小:设置合理的缓存大小限制
- 过期策略:基于时间或基于访问频率
- 淘汰策略:LRU、LFU 等
- 持久化:是否需要将缓存数据持久化到磁盘
- 线程安全:确保缓存在多线程环境下的安全性







