java spring 内存缓存_java spring 使用注解来实现缓存

这里举例使用spring3.1.4 + ehcache

注解的方式使用cache 是在spring3.1加入的

使用方法:

1.ehcache依赖+spring依赖

net.sf.ehcache

ehcache

2.7.2

org.springframework

spring-core

3.1.4.RELEASE

org.springframework

spring-web

3.1.4.RELEASE

org.springframework

spring-webmvc

3.1.4.RELEASE

org.springframework

spring-context

3.1.4.RELEASE

cglib

cglib

2.2.2

2.spring+ehcache的简单配置

name

cache的唯一标识

maxElementsInMemory

最大创建条数

eternal

缓存是否是永久的,默认false

timeToLiveSeconds

缓存的存活时间

timeToIdleSeconds

多长时间不访问就清楚该缓存

overflowToDisk

内存不足是否写入磁盘,默认False

maxElementsOnDisk

持久化到硬盘最大条数

memoryStoreEvictionPolicy

缓存满了后,清除缓存的规则,

自带三种:FIFO(先进先出),LFU(最少使用),LRU(最近最少使用)

diskSpoolBufferSizeMB

磁盘缓存的缓存区大小,默认30M

diskExpiryThreadIntervalSeconds

磁盘失效线程时间间隔

2.1 首先建立一个ehcache.xml的配置文件

/>

2.2在spring的apllication.xml 加入注入的cache

这里还需要在配置文件头部引入

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

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

xsi:schemaLocation="

...

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

"

在mvc-servlet.xml 里加入

3.注解使用

3.1 用于对象 class

@Cacheable(value = "onecache")

class A1{}

这种情况类中方法中返回值都会被缓存,

3.2用于方法method

@Cacheable(value = "onecache", key = "#name",condition ="#age < 25")public xx findEmployeeBySurname(String firstName, String name, intage) {returnxxx

}

这个就会将age小于25的值,按name为key缓存

3.3 清除

@CacheEvict(value="onecache",key="#name + 'haha'")public voiddelete(String name) {

System.out.println("delete one name");

}

还可使用下面的清除全部

@CacheEvict(value="oneCache",allEntries=true)

4.代码调用

@AutowiredprivateCacheManager cacheManager;//获取当前时间

public String getABCCache() {

cacheManager.getCache("ccc").put("hello", newDate().toString());return (String) cacheManager.getCache("ccc").get("hello").get();

}

可以通过注入Spring框架提供的CacheManager来实现缓存的存取,具体步骤如下: 1. 配置CacheManager 在Spring配置文件中配置CacheManager,如下所示: ```xml <bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager"> <property name="cacheNames"> <list> <value>cacheName1</value> <value>cacheName2</value> </list> </property> </bean> ``` 2. 注入CacheManager 在需要使用缓存的类中注入CacheManager,如下所示: ```java @Component public class CacheService { @Autowired private CacheManager cacheManager; public void put(String cacheName, Object key, Object value) { Cache cache = cacheManager.getCache(cacheName); cache.put(key, value); } public Object get(String cacheName, Object key) { Cache cache = cacheManager.getCache(cacheName); Cache.ValueWrapper valueWrapper = cache.get(key); return valueWrapper != null ? valueWrapper.get() : null; } } ``` 3. 使用缓存 在需要使用缓存的方法中调用CacheService的put和get方法,如下所示: ```java @Service public class MyService { @Autowired private CacheService cacheService; public void doSomething(String key) { Object value = cacheService.get("cacheName", key); if (value == null) { value = calculateValue(key); cacheService.put("cacheName", key, value); } // 使用value做一些操作 } private Object calculateValue(String key) { // 计算value的过程 } } ``` 这样就可以通过注入CacheManager来实现缓存的存取,而不需要使用注解的形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值