(初学者)在springboot中使用ehcache
1.在父类项目中添加公共依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
使用的项目中加入:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
2.在src/main/resources目录下新建ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"
memoryStoreEvictionPolicy="LRU" />
<cache
name="getInfoCache"
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="259200"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
3.ehcache配置类
/**
* ehcache缓存的配置
*
* @author ..
* @date 2018年10月11日 下午2:37:18
*/
@Configuration
@EnableCaching
public class CacheConfiguration {
/**
* 根据shared与否的设置,spring分别通过CacheManager。create()或new CacheManager()方式来创建一个ehcache基地
*
* @author ..
* @date 2018年10月11日 下午2:42:12
*/
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
/**
* 主要的管理器
*
* @author ..
* @date 2018年10月11日 下午2:53:11
*/
@Bean(name="appEhCacheCacheManager")
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean) {
return new EhCacheCacheManager(bean.getObject());
}
}
4.ehcache的工具包装类
/**
* ehcache缓存工具类
*
* @author ..
* @date 2018年10月11日 下午2:54:41
*/
@Component
public class EhcacheUtil {
@Autowired
EhCacheCacheManager appEhCacheCacheManager;
/**
* 设置缓存
*
* @author ..
* @date 2018年10月11日 下午3:06:41
*/
public void put(String cacheName, String key, Object value) {
Cache cache = appEhCacheCacheManager.getCacheManager().getCache(cacheName);
Element element = new Element(key, value);
cache.put(element);
System.out.println(cache);
}
/**
* 获取缓存
*
* @author ..
* @date 2018年10月11日 下午3:06:53
*/
public Object get(String cacheName, String key) {
Cache cache = appEhCacheCacheManager.getCacheManager().getCache(cacheName);
Element element = cache.get(key);
return element == null ? null : element.getObjectValue();
}
/**
* 获取ehcache
*
* @author ..
* @date 2018年10月11日 下午3:07:14
*/
public Cache get(String cacheName) {
return appEhCacheCacheManager.getCacheManager().getCache(cacheName);
}
/**
* 删除缓存
*
* @author ..
* @date 2018年10月11日 下午3:07:37
*/
public void remove(String cacheName, String key) {
Cache cache = appEhCacheCacheManager.getCacheManager().getCache(cacheName);
cache.remove(key);
}
}
5.一定要这样引入工具类,不然注入不进去为null
@Autowired private EhcacheUtil ehcacheUtil;
使用方法
ehcacheUtil.put(“getInfoCache”, “名字”, “Jack”);
Object aaString = ehcacheUtil.get(“getInfoCache”,“名字”);
提示:cacheName必须是xml中注册过的相对应才能获取到哦!
去学习更高端的用法吧!