本地缓存的几种框架

您所在的位置:网站首页 java本地缓存框架 本地缓存的几种框架

本地缓存的几种框架

2023-09-29 22:18| 来源: 网络整理| 查看: 265

Guava Cache

Google Guava Cache是一种非常优秀本地缓存解决方案,提供了基于容量,时间和引用的缓存回收方式。

怎么用?​

引入依赖

com.google.guava guava 18.0

相关加载缓存代码

public class GiteaUserServiceImpl implements GiteaUserService { // 缓存的list private LoadingCache emailUserList; // 在项目启动的时候构造缓存 @PostConstruct public void init() { // 设置两小时的过期时间 emailUserList = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.HOURS).build(new CacheLoader() { // 缓存失效之后的重新加载逻辑 @Override public Optional load(String email) { CommonUserDto commonUserDto = null; try { commonUserDto = giteaExService.qryUserDtoByEmail(email); } catch (Exception e) { logger.error("LoadingCache Portal User error...", e); } return Optional.ofNullable(commonUserDto); } }); // 加载缓存 this.loadUserDtoList(); } /** * 查询并且构造用户集合 */ private void loadUserDtoList() { // 查询全量用户 List userDtoList = giteaExService.qryUserDtoList(); // 加载缓存 userDtoList.stream() .filter(commonUserDto -> !ObjectUtils.isEmpty(commonUserDto) && StringUtils.isNotEmpty(commonUserDto.getEmail())) .forEach(commonUserDto -> { // 构造email缓存 emailUserList.put(commonUserDto.getEmail(), Optional.of(commonUserDto)); }); } }

load方法是缓存失效之后的重新加载逻辑,因为guava cache不允许出现null,所以使用Optional作为容器,设置null;

ehcache

Ehcache主要基于内存缓存,磁盘缓存为辅的,使用起来方便。支持缓存内容存储在磁盘中。

使用方式

引入依赖

net.sf.ehcache ehcache 2.10.4

在项目资源目录下新建一个ehcache.xml文件,文件内容参考如下:

注入bean的方式构造cacheManager

@Configuration public class EhCacheConfig { /** * 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地. */ @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() { EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean(); cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); cacheManagerFactoryBean.setShared(true); return cacheManagerFactoryBean; } }

写一个缓存工具类

@Component @EnableCaching public class CacheUtils { private static Cache cache; private static final String CACHE_NAME = "defaultCache"; @Autowired private EhCacheCacheManager ehCacheCacheManager; public static void put(String key, Object value) { put(CACHE_NAME, key, value); } public static Object get(String key) { Element element = cache.get(key); if (null != element) { return element.getObjectValue(); } return element; } @PostConstruct private void init() { EhCacheCacheManager ehCacheCacheManager = ApplicationContextHelper.getBean(EhCacheCacheManager.class); CacheManager cacheManager = ehCacheCacheManager.getCacheManager(); try { cache = cacheManager.getCache(CACHE_NAME); if (cache == null) { throw new RuntimeException("当前系统中没有定义“" + CACHE_NAME + "”这个缓存。"); } } catch (Exception ex) { ex.printStackTrace(); } } /** * 写入缓存 通用方法 * * @param cacheName * @param key * @param value */ private static void put(String cacheName, String key, Object value) { Element element = new Element(key, value); cache.put(element); } }

以上是两种本地缓存的框架的简单使用,个人使用来看Google的缓存更加简单实用。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3