Java中本地缓存的4种实现方式总结
目录
- 前言
- 一、基础缓存实现
- 1. 缓存实体类
- 2. 缓存工具类
- 二、Guava LoadingCache
- 1. 使用示例
- 三、SpringBoot整合Caffeine
- 1. 开启缓存功能
- 2. 配置缓存管理器
- 3. 使用缓存注解
- 四、JetCache——阿里巴巴的分布式缓存框架
- 1. 引入依赖
- 2. 配置JetCache
- 3. 使用JetCache注解
- 总结
前言
在Java开发中,缓存技术是提高应用性能php的关键手段之一。
今天,我们来聊聊Java中的四种主流本地缓存技术,并通过实例代码帮助大家更好地理解和应用这些技术。
一、基础缓存实现
首先,我们从最python基础的缓存实现讲起。
一个简单的缓存系统通常包括缓存实体类、添加、删除、查询和清除缓存的功能。
1. 缓存实体类
缓存实体类用于存储缓存的键值对以及过期时间。
代码如下:。
public class CacheEntity { private String cacheKey; private Object cacheValue; private long expireTime; // 过期时间戳 // 构造方法、getter和setter省略 }
2. 缓存工具类
接下来,我们实现一个缓存工具类,使用ConcurrentHashMap
作为存储结构,并通过定时任务清除过期数据。
import java.util.concurrent.*; import java.util.Map; publicclassCacheUtil { privatefinalstatic Map<String, CacheEntity> CACHE_MAP = newConcurrentHashMap<>(); privatestaticScheduledExecutorServiceexecutorService= Executors.newSingleThreadScheduledExecutor(); static { executorService.scheduleAtFixedRate(() -> { longcurrentTime= System.currentTimeMillis(); CACHE_MAP.values().removeIf(entity -> entity.getExpireTime() < currentTime); }, 0, 500, TimeUnit.MILLISECONDS); } publicstaticvoidput(String key, Object value, long expireTimeInSeconds) { longexpireTime= System.currentTimeMillis() + expireTimeInSeconds * 1000; CACHE_MAP.put(key, newCacheEntity(key, value, expireTime)); } publicstatic Object get(String key) { CacheEntityentity= CACHE_MAP.get(key); if (entity == null || entity.getExpireTime() < System.currentTimeMillis()) { CACHE_MAP.remove(key); returnnull; } return entity.getCacheValue(); } publicstaticvoiddelete(String key) { CACHE_MAP.remove(key); } publicstaticvoidclear() { CACHE_MAP.clear(); } }
测试代码:
public class Test { public static void main(String[] args) throws InterruptedException { CacheUtil.put("name", "zzc", 10L); System.out.println("第一次查询结果:" + CacheUtil.get("name")); Thread.sleep(2000L); System.out.println("第二次查询结果:" + CacheUtil.get("name")); } }
二、Guava LoadingCache
Guava是Google提供的一个Java基础库,其中的LoadingCache是一个强大的缓存工具。
1. 使用示例
import com.google.common.cache.*; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; publicclassTest { publicstaticvoidmain(String[] args)throws ExecutionException { LoadingCache<String, String> cache = CacheBuil编程客栈der.newBuilder() .concurrencyLevel(8) .expireAfterWrite(10, TimeUnit.SECONDS) .build(newCacheLoader<String, String>() { @Override public String load(String key)throws Exception { return"default_value"; } }); cache.put("name", "zzc"); StringnameValue= cache.get("name"); StringageValue= cache.get("age", () -> "default_age"); StringsexValue= cache.get("sex", () -> "key 不存在"); System.out.println("nameValue: " + nameValue); System.out.println("ageValue: " + ageValue); System.out.println("sexValue: " + sexValue); } }
在上面的代码中,当调用cache.get(key)
方法时,如果缓存中不存在对应的key,则会通过CacheLoader
的load
方法加载默认值。
三、SpringBoot整合Caffeine
Caffeine是一个高性能的Java缓存库,SpringBoot提供了与Caffeine的无缝整合。
1. 开启缓存功能
在启动类上添加@EnableCaching
注解。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @EnableCaching @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
2. 配置缓存管理器
import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnablandroideCaching; import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration @EnableCaching publicclassCacheConfig { @Bean("caffeineCacheManager") public CacheManager cacheManager() { CaffeineCacheManagercacheManager=newCaffeineCacheManager("userCache"); cacheManager.getCache("userCache").getConfig().setCaffeine(caffeineCacheBuilder()); return cacheManager; } Caffeine<Object, Object> caffeineCacheBuilder() { www.devze.com return Caffeine.newBuilder() .expireAfterWrite(10, TimeUnit.SECONDS) .maximumSize(100); } }
3. 使用缓存注解
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service publicclassUserService { // 模拟数据库操作 private Map<Integer, User> userMap = newHashMap<>(); @Cacheable(value = "userCache", key = "#id") public User getUserById(Integer id) { // 假设从数据库获取用户数据 Useruser=newUser(); user.setId(id); user.setName("User" + id); userMap.put(id, user); return user; } @CacheEvict(value = "userCache", key = "#id") publicvoiddeleteUserById(Integer id) { userMap.remove(id); } }
四、JetCache——阿里巴巴的分布式缓存框架
JetCache是阿里巴巴开源的一款基于Spring和Redis的分布式缓存框架,提供了强大的缓存抽象和注解支持。
1. 引入依赖
在pom.XML
中添加JetCache依赖。
<dependency> <groupId>com.alicp.jetcache</groupId> <artifactId>jetcache-starter-redis</artifactId> <version>最新版本号</version> </dependency>
2. 配置JetCache
在application.yml
中配置JetCache。
jetcache: stat:enable# 开启统计 remote: default: type:redis keyConvertor:fastjson# 序列化方式 valueEncoder:java valueDecoder:java poolConfig: minIdle:5 maxIdle:20 maxTotal:50 host:localhost port: 6379
3. 使用JetCache注解
import com.alicp.jetcache.anno.CacheType; import com.alicp.jetcache.anno.Cached; import com.alicp.jetcache.anno.CacheUpdate; import com.alicp.jetcache.anno.CacheInvalidate; import org.springframework.stereotype.Service; @Service publicclassUserService { @Cached(name = "userCache", key = "#id", cacheType = CacheType.BOTH) public String getUser(int id) { return"用户:" + id; } @CacheUpdate(name = "userCache", key = "#id", value = "#user") publicvoidupdateUser(int id, String user) { System.out.println("更新用户:" + user); } @CacheInvalidate(name = "userCache", key = "#id") publicvoiddeleteUser(int id) { System.out.println("删除用户:" + id); } }
JetCache支持本地缓存和远程缓存的组合,非常适合分布式系统。
总结
今天我们一起探索了Java本地缓存的多种实现方式,从手写缓存到Guava Cache、Caffeine、Ehcache和JetCache。每种方式都有自己的特点和适用场景。
到此这篇关于Java中本地缓存的4种实现方式的文章就介绍到这了,更多相关Java本地缓存实现内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论