Guava cache

Guava cache http://www.cnblogs.com/peida/p/Guava_Cache.html 缓存,在我们日常开发中是必不可少的一种解决性能问题的方法。简单的说,cache 就是为了提升系统性能而开辟的一块内存空间。 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用。在日常开发的很多场合,由于受限于硬盘IO的性能或者我们自身业务系统的数据处理和获取可能非常费时,当我们发现我们的系统这个数据请求量很大的时候,频繁的IO和频繁的逻辑处理会导致硬盘和CPU资源的瓶颈出现。缓存的作用就是将这些来自不易的数据保存在内存中,当有其他线程或者客户端需要查询相同的数据资源时,直接从缓存的内存块中返回数据,这样不但可以提高系统的响应时间,同时也可以节省对这些数据的处理流程的资源消耗,整体上来说,系统性能会有大大的提升。 缓存在很多系统和架构中都用广泛的应用,例如: 1.CPU缓存 2.操作系统缓存 3.本地缓存 4.分布式缓存 5.HTTP缓存 6.数据库缓存 等等,可以说在计算机和网络领域,缓存无处不在。可以这么说,只要有硬件性能不对等,涉及到网络传输的地方都会有缓存的身影。 Guava Cache是一个全内存的本地缓存实现,它提供了线程安全的实现机制。整体上来说Guava cache 是本地缓存的不二之选,简单易用,性能好。 Guava Cache有两种创建方式: cacheLoader callable callback 通过这两种方法创建的cache,和通常用map来缓存的做法比,不同在于,这两种方法都实现了一种逻辑——从缓存中取key X的值,如果该值已经缓存过了,则返回缓存中的值,如果没有缓存过,可以通过某个方法来获取这个值。但不同的在于cacheloader的定义比较宽泛,是针对整个cache定义的,可以认为是统一的根据key值load value的方法。而callable的方式较为灵活,允许你在get的时候指定。 cacheLoader方式实现实例: @Test public void TestLoadingCache() throws Exception{ LoadingCache<String,String> cahceBuilder=CacheBuilder .newBuilder() .build(new CacheLoader<String, String>(){ @Override public String load(String key) throws Exception { String strProValue="hello "+key+"!"; return strProValue; } }); System.out.println("jerry value:"+cahceBuilder.apply("jerry")); System.out.println("jerry value:"+cahceBuilder.get("jerry")); System.out.println("peida value:"+cahceBuilder.get("peida")); System.out.println("peida value:"+cahceBuilder.apply("peida")); System.out.println("lisa value:"+cahceBuilder.apply("lisa")); cahceBuilder.put("harry", "ssdded"); System.out.println("harry value:"+cahceBuilder.get("harry")); } 输出: ...

Created 31316-03-31 · Updated 9096-10-09 · 3 min · 496 words · -

Guava Multimap

Guava Multimap 在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数据结构,以便做相应的业务逻辑处理。例如: import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Test; public class MultimapTest { Map<String, List<StudentScore>> StudentScoreMap = new HashMap<String, List<StudentScore>>(); @Test public void testStudentScore(){ for(int i=10;i<20;i++){ StudentScore studentScore=new StudentScore(); studentScore.CourseId=1001+i; studentScore.score=100-i; addStudentScore("peida",studentScore); } System.out.println("StudentScoreMap:"+StudentScoreMap.size()); System.out.println("StudentScoreMap:"+StudentScoreMap.containsKey("peida")); System.out.println("StudentScoreMap:"+StudentScoreMap.containsKey("jerry")); System.out.println("StudentScoreMap:"+StudentScoreMap.size()); System.out.println("StudentScoreMap:"+StudentScoreMap.get("peida").size()); List<StudentScore> StudentScoreList=StudentScoreMap.get("peida"); if(StudentScoreList!=null&&StudentScoreList.size()>0){ for(StudentScore stuScore:StudentScoreList){ System.out.println("stuScore one:"+stuScore.CourseId+" score:"+stuScore.score); } } } public void addStudentScore(final String stuName,final StudentScore studentScore) { List<StudentScore> stuScore = StudentScoreMap.get(stuName); if (stuScore == null) { stuScore = new ArrayList<StudentScore>(); StudentScoreMap.put(stuName, stuScore); } stuScore.add(studentScore); } } class StudentScore{ int CourseId; int score; } 说明: 想 Map<String, List<StudentScore>> StudentScoreMap = new HashMap<String, List<StudentScore>>()这样的数据结构,自己实现起来太麻烦,你需要检查key是否存在,不存在时则创建一个,存在时在List后面添加上一个。这个过程是比较痛苦的,如果你希望检查List中的对象是否存在,删除一个对象,或者遍历整个数据结构,那么则需要更多的代码来实现。 ...

Created 18186-10-18 · Updated 9096-10-09 · 2 min · 287 words · -

Google Guava

Google Guava Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你的JAVa代码更加优雅,更加简洁,让你工作更加轻松愉悦。下面我们就开启优雅Java编程学习之旅! **项目相关信息: ** 官方首页: http://code.google.com/p/guava-libraries 官方下载: http://code.google.com/p/guava-libraries/downloads/list 官方文档: http://docs.guava-libraries.googlecode.com/git/javadoc http://www.ostools.net/apidocs/apidoc?api=guava **源码包的简单说明: ** com.google.common.annotations: 普通注解类型。 com.google.common.base: 基本工具类库和接口。 com.google.common.cache: 缓存工具包,非常简单易用且功能强大的JVM内缓存。 com.google.common.collect: 带泛型的集合接口扩展和实现,以及工具类,这里你会发现很多好玩的集合。 com.google.common.eventbus: 发布订阅风格的事件总线。 com.google.common.hash: 哈希工具包。 com.google.common.io: I/O工具包。 com.google.common.math: 原始算术类型和超大数的运算工具包。 com.google.common.net: 网络工具包。 com.google.common.primitives: 八种原始类型和无符号类型的静态工具包。 com.google.common.reflect: 反射工具包。 com.google.common.util.concurrent: 多线程工具包。 **类库使用手册: ** **一. 基本工具类: **让使用Java语言更令人愉悦。 使用和避免 null: null 有语言歧义, 会产生令人费解的错误, 反正他总是让人不爽。很多 Guava 的工具类在遇到 null 时会直接拒绝或出错,而不是默默地接受他们。 前提条件: 更容易的对你的方法进行前提条件的测试。 ...

Created 12126-11-12 · Updated 12126-11-12 · 1 min · 211 words · -

Google Guava

Google Guava Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你的JAVa代码更加优雅,更加简洁,让你工作更加轻松愉悦。下面我们就开启优雅Java编程学习之旅! **项目相关信息: ** 官方首页: http://code.google.com/p/guava-libraries 官方下载: http://code.google.com/p/guava-libraries/downloads/list 官方文档: http://docs.guava-libraries.googlecode.com/git/javadoc http://www.ostools.net/apidocs/apidoc?api=guava **源码包的简单说明: ** com.google.common.annotations: 普通注解类型。 com.google.common.base: 基本工具类库和接口。 com.google.common.cache: 缓存工具包,非常简单易用且功能强大的JVM内缓存。 com.google.common.collect: 带泛型的集合接口扩展和实现,以及工具类,这里你会发现很多好玩的集合。 com.google.common.eventbus: 发布订阅风格的事件总线。 com.google.common.hash: 哈希工具包。 com.google.common.io: I/O工具包。 com.google.common.math: 原始算术类型和超大数的运算工具包。 com.google.common.net: 网络工具包。 com.google.common.primitives: 八种原始类型和无符号类型的静态工具包。 com.google.common.reflect: 反射工具包。 com.google.common.util.concurrent: 多线程工具包。 **类库使用手册: ** **一. 基本工具类: **让使用Java语言更令人愉悦。 使用和避免 null: null 有语言歧义, 会产生令人费解的错误, 反正他总是让人不爽。很多 Guava 的工具类在遇到 null 时会直接拒绝或出错,而不是默默地接受他们。 前提条件: 更容易的对你的方法进行前提条件的测试。 ...

Created 12126-11-12 · Updated 12126-11-12 · 1 min · 211 words · -