缓存是提高性能的一把利器。
常用到的缓存技术有分布式缓存,像Redis、MC;也有本地缓存,像ehcache、guava cache等。这里说的是本地缓存guava cache。
guava cache刚开始接触,这就记录下来。。
public static void main(String[] args) throws ExecutionException, InterruptedException{ //缓存接口这里是LoadingCache,LoadingCache在缓存项不存在时可以自动加载缓存 LoadingCache<Integer,Student> studentCache //CacheBuilder的构造函数是私有的,只能通过其静态方法newBuilder()来获得CacheBuilder的实例 = CacheBuilder.newBuilder() //设置并发级别为8,并发级别是指可以同时写缓存的线程数 .concurrencyLevel(8) //设置写缓存后8秒钟过期 .expireAfterWrite(8, TimeUnit.SECONDS)
//设置写缓存后1秒钟刷新
.refreshAfterWrite(1, TimeUnit. SECONDS) //设置缓存容器的初始容量为10 .initialCapacity(10) //设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项 .maximumSize(100) //设置要统计缓存的命中率 .recordStats() //设置缓存的移除通知 .removalListener(new RemovalListener<Object, Object>() { @Override public void onRemoval(RemovalNotification<Object, Object> notification) { System.out.println(notification.getKey() + " was removed, cause is " + notification.getCause()); } }) //build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 .build( new CacheLoader<Integer, Student>() { @Override public Student load(Integer key) throws Exception { System.out.println("load student " + key); Student student = new Student(); student.setId(key); student.setName("name " + key); return student; } } ); for (int i=0;i<20;i++) { //从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据 Student student = studentCache.get(1); System.out.println(student); //休眠1秒 TimeUnit.SECONDS.sleep(1); } System.out.println("cache stats:"); //最后打印缓存的命中率等 情况 System.out.println(studentCache.stats().toString()); }
还有另一种方法
public class CacheModel { public Student getStudent(Integer key){ System.out.println("load student " + key); Student student = new Student(); student.setId(key); student.setName("name " + key); return student; } //load Method public void loadCacheA() throws Exception{ LoadingCache<Integer,Student> studentCache= CacheBuilder.newBuilder().concurrencyLevel(8). expireAfterWrite(8, TimeUnit.SECONDS).refreshAfterWrite(1, TimeUnit. SECONDS).initialCapacity(10).maximumSize(100) .recordStats().removalListener(new RemovalListener<Object, Object>() { public void onRemoval(RemovalNotification<Object, Object> notification) { System.out.println(notification.getKey() + " was removed, cause is " + notification);} }).build( new CacheLoader<Integer, Student>() { @Override public Student load(Integer key) throws Exception { return getStudent(key); } } ); for (int i=0;i<20;i++) { Student student = studentCache.get(1); System.out.println(student); TimeUnit.SECONDS.sleep(1); } System.out.println("cache stats:"); System.out.println(studentCache.stats().toString()); } //call back Method public void loadCacheB(final Integer key) throws Exception{ Cache<Integer, Student> cache = CacheBuilder.newBuilder().maximumSize(1000).build(); Student resultVal = cache.get(key, new Callable<Student>() { public Student call() { return getStudent(key); } }); for (int i=0;i<20;i++) { System.out.println(resultVal); TimeUnit.SECONDS.sleep(1); } System.out.println("cache stats:"); System.out.println(cache.stats().toString()); } public static void main(String[] args) throws Exception { CacheModel cache = new CacheModel(); cache.loadCacheB(2); }
public static void main(String[] args) throws ExecutionException, InterruptedException{ //缓存接口这里是LoadingCache,LoadingCache在缓存项不存在时可以自动加载缓存 LoadingCache<integer,student> studentCache //CacheBuilder的构造函数是私有的,只能通过其静态方法newBuilder()来获得CacheBuilder的实例 = CacheBuilder.newBuilder() //设置并发级别为8,并发级别是指可以同时写缓存的线程数 .concurrencyLevel(8) //设置写缓存后8秒钟过期 .expireAfterWrite(8, TimeUnit.SECONDS) //设置缓存容器的初始容量为10 .initialCapacity(10) //设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项 .maximumSize(100) //设置要统计缓存的命中率 .recordStats() //设置缓存的移除通知 .removalListener(new RemovalListener<object, object="">() { @Override public void onRemoval(RemovalNotification<object, object=""> notification) { System.out.println(notification.getKey() + " was removed, cause is " + notification.getCause()); } }) //build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 .build( new CacheLoader<integer, student="">() { @Override public Student load(Integer key) throws Exception { System.out.println("load student " + key); Student student = new Student(); student.setId(key); student.setName("name " + key); return student; } } ); for (int i=0;i<20;i++) { //从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据 Student student = studentCache.get(1); System.out.println(student); //休眠1秒 TimeUnit.SECONDS.sleep(1); } System.out.println("cache stats:"); //最后打印缓存的命中率等 情况 System.out.println(studentCache.stats().toString()); }
原文:http://www.cnblogs.com/bestzhang/p/6400603.html