java如何放到缓存
使用Java实现缓存的几种方法
在Java中实现缓存可以通过多种方式完成,以下是一些常见的实现方法:
基于HashMap实现简单缓存
创建一个简单的缓存可以使用HashMap,但需要注意线程安全问题:
import java.util.HashMap;
import java.util.Map;
public class SimpleCache<K, V> {
private final Map<K, V> cache = new HashMap<>();
public synchronized void put(K key, V value) {
cache.put(key, value);
}
public synchronized V get(K key) {
return cache.get(key);
}
public synchronized void remove(K key) {
cache.remove(key);
}
}
使用Guava Cache
Google的Guava库提供了强大的缓存功能:
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) // 最大缓存条目数
.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是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)
.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) {
// 模拟耗时操作
simulateSlowService();
return new Book(isbn, "Some book");
}
private void simulateSlowService() {
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
使用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);
}
}
}
选择缓存方案的考虑因素
- 简单应用:HashMap或ConcurrentHashMap
- 单机应用:Guava Cache或Caffeine
- 企业级应用:Ehcache或Spring Cache抽象
- 分布式系统:Redis或Memcached
每种方案都有其适用场景,应根据具体需求选择合适的缓存实现方式。






