guava缓存的使用及与spring的集成
来源:互联网 发布:精准数据库营销 编辑:程序博客网 时间:2024/06/14 08:44
一、guava缓存简介
guava是google为java开发的库,对jdk进行了扩展。在此我们只介绍guava的缓存。
guava缓存是内存缓存,也就是数据存在内存中的。
二、guava基本使用
1、添加maven依赖
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version></dependency>
2、自定义guava工具类
public class GuavaCacheUtil { public static LoadingCache<String, String> strCache = CacheBuilder .newBuilder() .maximumSize(1000) .expireAfterAccess(5, TimeUnit.MINUTES) .build( new CacheLoader<String, String>() { @Override public String load(String s) throws Exception { //当缓存中不存在时,自动加载新数据到缓存 return null; } } ); /** * 将值存入缓存 * * @param key * @param val */ public static void setStr(String key, String val) { strCache.put(key, val); } /** * 从缓存中取值 * * @param key * @return */ public static String getStr(String key) { String val = ""; try { val = strCache.get(key); } catch (ExecutionException e) { } return val; }}
3、使用
public class GuavaCacheMain { public static void main(String[] args) { String key = "k1"; String val = "v1"; GuavaCacheUtil.setStr(key, val); String v1 = GuavaCacheUtil.getStr(key); System.out.println("k1 : " + v1); String v2 = GuavaCacheUtil.getStr(key); System.out.println("k2 : " + v2); }}输出:
k1 : v1
k1 : v1
三、guava缓存与spring的集成
1、添加maven依赖
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version></dependency>2、在spring中配置guava缓存
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "> <context:component-scan base-package="com.dragon.study" /> <cache:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"> <property name="cacheSpecification" value="concurrencyLevel=4,expireAfterAccess=100s,expireAfterWrite=100s" /> <property name="cacheNames"> <list> <value>guavaCache</value> </list> </property> </bean></beans>3、在服务中使用guava缓存
public interface StudentService { public Student getStudent(Integer id); public Student updateStudent(Student stu); public void deleteStudent(Integer id); public void deleteAllStudent(); public void myDelete(Integer id);}
@Service("studentGuavaCache")public class StudentGuavaCacheImpl implements StudentService { @Cacheable(value = "guavaCache",key="'id_'+#id",condition = "#id<3") public Student getStudent(Integer id) { Student stu = new Student(); stu.setId(id); stu.setName("apple"); return stu; } @CachePut(value = "guavaCache",key="'id_'+#stu.getId()") public Student updateStudent(Student stu){ System.out.println("update stu"); return stu; } @CacheEvict(value = "guavaCache",key="'id_'+#id") public void deleteStudent(Integer id){ System.out.println("delete student "+id); } public void myDelete(Integer id){ try { StudentService ss = (StudentService) AopContext.currentProxy(); ss.deleteStudent(id); return ; }catch (Exception e){ e.printStackTrace(); } this.deleteStudent(id); } @CacheEvict(value = "guavaCache",allEntries = true) public void deleteAllStudent(){ System.out.println("delete all student "); }}4、测试
public class SpringGuavaCacheMain { public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-guava-cache.xml"); StudentService studentService = (StudentService) ac.getBean("studentGuavaCache"); Integer id =1; Student stu = studentService.getStudent(id); //新建缓存 stu = studentService.getStudent(id); //从缓存中取 studentService.myDelete(id); stu = studentService.getStudent(id); //从缓存中取 stu.setName("banana"); //重新设置值 studentService.updateStudent(stu); //更新缓存 stu = studentService.getStudent(id); //从缓存中取出新值 stu = new Student(); //新实例 stu.setId(0); studentService.updateStudent(stu); //用新建的实例进行更新,会新建缓存 stu = studentService.getStudent(0); //从缓存中取 studentService.deleteStudent(id); // 删除缓存 stu = studentService.getStudent(id); //再次新建缓存 id=2; stu = studentService.getStudent(id); //新建缓存 studentService.deleteAllStudent(); //删除所有缓存 id=1; stu = studentService.getStudent(id); //因所有缓存被前一步清除,会新建缓存 id=5; stu = studentService.getStudent(id); //不会新建缓存 因为设置了缓存条件必须小于3 stu = studentService.getStudent(id); //因没有缓存,不会从缓存中取 Assert.notNull(stu); }}输出:
delete student 1
update stu
update stu
delete student 1
delete all student
四、guava缓存与其它形式缓存共存
除了guava缓存,在还有其它如redis等缓存,使用方法类似,在此仅列出其spring的配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "> <context:component-scan base-package="com.dragon.study" /> <cache:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager"> <property name="cacheManagers"> <list> <ref bean="simpleCacheManager" /> <ref bean="guavaCacheManager" /> </list> </property> <property name="fallbackToNoOpCache" value="true" /> </bean> <bean id="simpleCacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default" /> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="mycache" /> <bean class="com.dragon.tbscheduleStudy.cache.RedisCache" p:name="redisCache" p:timeout="60000" /> </set> </property> </bean> <bean id="guavaCacheManager" class="org.springframework.cache.guava.GuavaCacheManager"> <property name="cacheSpecification" value="concurrencyLevel=4,expireAfterAccess=100s,expireAfterWrite=100s" /> <property name="cacheNames"> <list> <value>guavaCache</value> </list> </property> </bean></beans>
阅读全文
0 0
- guava缓存的使用及与spring的集成
- Quartz的使用及与spring集成使用
- 如何使用Guava的缓存管理
- 如何使用Guava的缓存管理
- 如何使用Guava的缓存管理
- 三、Guava的缓存
- Spring 与 ActiveMq的集成配置使用
- Apache ActiveMQ与Spring的集成使用
- Hessian使用(与Spring的集成)
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- Guava Cache的缓存统计
- jersey的简单介绍及与spring-boot的集成使用
- jersey的简单介绍及与spring-boot的集成使用
- 【Redis】spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- Redis 缓存 + Spring 的集成示例
- 图层
- 【java设计模式初探0】_单例模式
- 基于twitter的雪花算法生成不重复id
- 搜狐畅游笔试题
- 1015德才论(排序)
- guava缓存的使用及与spring的集成
- 成为未来几年最炙手可热的机器学习人才,基本功、秘密武器和弹药补给
- 微软2017年预科生计划在线编程笔试-#1491 : Monster Killing
- 使用opencv保存多张图片
- html5的视频播放
- Java异常分类
- 什么是javascript封装,封装的方法有几种
- python 试题:请描述decorator(装饰器)的用法和它的应用场景,如果可以的话,写一个decorator
- oracle查询(select)