SpringBoot手动使用EhCache的方法示例

网友投稿 743 2023-02-17


SpringBoot手动使用EhCache的方法示例

SpringBoot在annotation的层面实现了数据缓存的功能,基于Spring的AOP技术。所有的缓存配置只是在annotation层面配置,像声明式事务一样。

Spring定义了CacheManager和Cache接口统一不同的缓存技术。其中CacheManager是Spring提供的各种缓存技术的抽象接口。而Cache接口包含缓存的各种操作。

CacheManger

针对不同的缓存技术,需要实现不同的cacheManager,Spring定义了如下的cacheManger实现。

CacheManger

描述

SimpleCacheManager

使用简单的Collection来存储缓存,主要用于测试

ConcurrentMapCacheManager

使用ConcurrentMap作为缓存技术(默认)

NoOpCacheManageoFjZaYBVrhttp://

测试用

EhCacheCacheManager

使用EhCache作为缓存技术,以前在hibernate的时候经常用

GuavaCacheManager

使用google guava的GuavaCache作为缓存技术

HazelcastCacheManager

使用Hazelcast作为缓存技术

JCacheCacheManager

使用JCache标准的实现作为缓存技术,如Apache Commons JCS

RedisCacheManager

使用Redis作为缓存技术

常规的SpringBoot已经为我们自动配置了EhCache、Collection、Guava、ConcurrentMap等缓存,默认使用ConcurrentMapCacheManager。SpringBoot的application.properties配置文件,使用spring.cache前缀的属性进行配置。

application配置

spring.cache.type=#缓存的技术类型

spring.cache.cache-names=应用程序启动创建缓存的名称

spring.cache.ehcache.config=ehcache的配置文件位置

spring.cache.infinispan.config=infinispan的配置文件位置

spring.cache.jcache.config=jcache配置文件位置

spring.cache.jcache.provider=当多个jcache实现类时,指定选择jcache的实现类

入口类配置

加入注解 @EnableCaching

缓存注解

注解

描述

@Cacheable

在调用方法之前,首先应该在缓存中查找方法的返回值,如果这个值能够找到,就会返回缓存的值。否则,这个方法就会被调用,返回值会放到缓存之中。

@CachePut

将方法的返回值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用。

@CacheEvict

在缓存中清除一个或多个条目。

@Caching

分组的注解,能够同时应用多个其他的缓存注解。

手动使用EhCache

在实际开发过程中,存在不使用注解,需要自己添加缓存的情况。下面就以Ehcache为例,简单写一下配置过程。

1. 添加依赖

引入springboot-cache和ehcache。需要注意,EhCache不需要配置version,SpringBoot的根pom已经集成了。

org.springframework.boot

spring-boot-starter-cache

net.sf.ehcache

ehcache

2. 入口类配置

加入注解 @EnableCaching

@SpringBootApplication

@EnableCaching

public class DemoApplication {

}

3. EhCache配置

在src\main\resources目录下,添加ehcache.xml文件,内容见文末。

4. application.application配置

# 配置ehcache缓存

spring.cache.type=ehcache

# 指定ehcache配置文件路径

spring.cache.ehcache.config=classpath:/ehcache.xml

5. 使用Cache

注入SpringBoot自动配置的bean,org.springframework.cache.CacheManager。

一个简单的测试类:

package com.bbf.frame.test;

import com.bbf.frame.Application;

import org.apache.commons.lang3.StringUtils;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.cache.Cache;

import org.springframework.cache.CacheManager;

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

import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)

@WebAppConfiguration

@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)

public class TestCache {

@Resource

private CacheManager cacheManager;

@Test

public void cacheTest() {

// 显示所有的Cache空间

System.out.println(StringUtils.join(cacheManager.getCacheNames(), ","));

Cache cache = cacheManager.getCache("userCache");

cache.put("key", "123");

System.out.println("缓存成功");

String res = cache.get("key", String.class);

System.out.println(res);

}

}

附录 EhCache.xml

xsi:noNamespaceSchemaLocation = "http://ehcache.org/ehcache.xsd"

updateCheck = "false">

eternal = "false"

maxElementsInMemory = "10000"

overflowToDisk = "true"

diskPersistent = "false"

timeToIdleSeconds = "120"

timeToLiveSeconds = "120"

diskExpiryThreadIntervalSeconds = "120"

memoryStoreEvictionPolicy = "LRU"/>

name = "userCache"

eternal = "false"

maxElementsInMemory = "100"

overflowToDisk = "false"

diskPersistent = "false"

timeToIdleSeconds = "120"

timeToLiveSeconds = "180"

memoryStoreEvictionPolicy = "LRU"/>

xsi:noNamespaceSchemaLocation = "http://ehcache.org/ehcache.xsd"

updateCheck = "false">

eternal = "false"

maxElementsInMemory = "10000"

overflowToDisk = "true"

diskPersistent = "false"

timeToIdleSeconds = "120"

timeToLiveSeconds = "120"

diskExpiryThreadIntervalSeconds = "120"

memoryStoreEvictionPolicy = "LRU"/>

name = "userCache"

eternal = "false"

maxElementsInMemory = "100"

overflowToDisk = "false"

diskPersistent = "false"

timeToIdleSeconds = "120"

timeToLiveSeconds = "180"

memoryStoreEvictionPolicy = "LRU"/>

eternal = "false"

maxElementsInMemory = "10000"

overflowToDisk = "true"

diskPersistent = "false"

timeToIdleSeconds = "120"

timeToLiveSeconds = "120"

diskExpiryThreadIntervalSeconds = "120"

memoryStoreEvictionPolicy = "LRU"/>

name = "userCache"

eternal = "false"

maxElementsInMemory = "100"

overflowToDisk = "false"

diskPersistent = "false"

timeToIdleSeconds = "120"

timeToLiveSeconds = "180"

memoryStoreEvictionPolicy = "LRU"/>

name = "userCache"

eternal = "false"

maxElementsInMemory = "100"

overflowToDisk = "false"

diskPersistent = "false"

timeToIdleSeconds = "120"

timeToLiveSeconds = "180"

memoryStoreEvictionPolicy = "LRU"/>


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决"XML Parser Error on line 1: 前言中不允许有内容"错误
下一篇:简单实现Java通讯录系统
相关文章

 发表评论

暂时没有评论,来抢沙发吧~