java ehcache使用_Java Ehcache缓存框架入门级使用实例

前言

JAVA缓存实现方案有很多,最基本的自己使用Map去构建缓存,或者使用memcached或Redis,但是上述两种缓存框架都要搭建服务器,而Map自行构建的缓存可能没有很高的使用效率,那么我们可以尝试一下使用Ehcache缓存框架。

Ehcache主要基于内存缓存,磁盘缓存为辅的,使用起来方便。下面介绍如何在项目中使用Ehcache

入门使用教程

1.maven引用

net.sf.ehcache

ehcache

2.10.4

2.在classpath下建立一个ehcache.xml

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

name="merchant-apply-cache"

eternal="false"

timeToIdleSeconds="2400"

timeToLiveSeconds="2400"

maxEntriesLocalHeap="10000"

maxEntriesLocalDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

overflowToDisk="false"

memoryStoreEvictionPolicy="LRU">

3.与spring的cacheManager结合使用

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache.xsd">

4.代码使用

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.Cache;

import org.springframework.cache.CacheManager;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baomidou.mybatisplus.toolkit.IdWorker;

import com.easylink.mall.entity.Merchant;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring/spring.xml")

public class EhcacheTest {

@Autowired

private CacheManager cacheManager;

@Test

public void execute() {

// 获取商户申请缓存容器

Cache cache = cacheManager.getCache("merchant-apply-cache");

Merchant merchant = new Merchant();

Long id = IdWorker.getId();

merchant.setId(id);

merchant.setName("缓存测试");

// 将商户申请数据添加至缓存中 // key : id value : object

cache.put(id, merchant);

// 获取商户申请数据

// 方法1

Merchant cacheMerchant1 = (Merchant) cache.get(id).get();

System.out.println(cacheMerchant1.getName());

// 方法2

Merchant cacheMerchant2 = cache.get(id, Merchant.class);

System.out.println(cacheMerchant2.getName());

// 将商户申请数据从缓存中移除

cache.evict(id);

}

}

5.注意事项

cache.get(key) 和cache.get(key, class);方法,由于不知道你存入的key是什么类型,所以get的时候不会做key的类型检查,如上述例子中

Long id = IdWorker.getId();

cache.put(id, merchant);

Merchant cacheMerchant2 = cache.get(id, Merchant.class);

put进去时的key是Long类型的,get的时候也只能传入对应Long类型的key才能获取到对应的value,如果传入的是String类型的key,即使两个key的值是一致的,也会导致无法获取到对应的value。这个情况很容易发生在对request请求的参数,由于是String字符串类型,但是忘了做类型转换就直接把这个String当做key去获取对应的value。导致获取不到,请同学们要注意,亲身经历,血与泪的教训。

总结

以上就是我自己总结的Ehcache入门级用法,Ehcache是个不错的内存缓存框架,如果没使用过的话,可以尝试使用。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值