使用google guava 实现定时缓存功能

使用google guava 实现定时缓存功能。


创建Book.java
public class Book {
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Book(int id) {
		super();
		this.id = id;
	}
	public Book() {
		super();
	}
	@Override
	public String toString() {
		return ""+id;
	}
}



创建BookDao.java
public class BookDao {
	public Object executeSQL() {
		System.out.println("此处调用了Dao方法。executeSQL");
		List books = new ArrayList();
		for (int i = 0; i < 3; i++) {
			Book b = new Book(i);
			books.add(b);
		}


		return books;
	}
}



创建BookCache.java
public class BookCache {
	public static Cache> cache = CacheBuilder.newBuilder()
			.expireAfterAccess(8, TimeUnit.SECONDS).maximumSize(10)
			.build();
}




创建运行类。
public static void main(String[] args) {
	try {
		System.out.println("第一次调用dao方法,正确状态:应该调用Dao里的方法");
		List books = BookCache.cache.get("points",
				new Callable>() {
					@Override
					public List call() {
						BookDao dao = new BookDao();
						List list = (List) dao.executeSQL();
						if (null == list || list.size() <= 0) {
							list = new ArrayList();
						}
						return list;
					}
				});
		for (Book book : books) {
			System.out.println(book);
		}
		System.out.println("第二次调用dao方法,正确状态:不调用Dao里的方法");
		List books2 = BookCache.cache.get("points",
				new Callable>() {
					@Override
					public List call() {
						BookDao dao = new BookDao();
						List list = (List) dao.executeSQL();
						if (null == list || list.size() <= 0) {
							list = new ArrayList();
						}
						return list;
					}
				});
		for (Book book : books2) {
			System.out.println(book);
		}
	} catch (Exception e1) {
		e1.printStackTrace();
	}
	try {
		Thread.currentThread();
		Thread.sleep(TimeUnit.SECONDS.toMillis(10));
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		System.out.println("休息十秒后,第三次调用dao方法,正确状态:调用Dao里的方法");
		List books = BookCache.cache.get("points",
				new Callable>() {
					@Override
					public List call() {
						BookDao dao = new BookDao();
						List list = (List) dao.executeSQL();
						if (null == list || list.size() <= 0) {
							list = new ArrayList();
						}
						return list;
					}
				});
		for (Book book : books) {
			System.out.println(book);
		}
		System.out.println("第四次调用dao方法,正确状态:不调用Dao里的方法");
		List books2 = BookCache.cache.get("points",
				new Callable>() {
					@Override
					public List call() {
						BookDao dao = new BookDao();
						List list = (List) dao.executeSQL();
						if (null == list || list.size() <= 0) {
							list = new ArrayList();
						}
						return list;
					}
				});
		for (Book book : books2) {
			System.out.println(book);
		}
	} catch (Exception e1) {
		e1.printStackTrace();
	}
}



运行结果:
第一次调用dao方法,正确状态:应该调用Dao里的方法
此处调用了Dao方法。executeSQL
0
1
2
第二次调用dao方法,正确状态:不调用Dao里的方法
0
1
2
休息十秒后,第三次调用dao方法,正确状态:调用Dao里的方法
此处调用了Dao方法。executeSQL
0
1
2
第四次调用dao方法,正确状态:不调用Dao里的方法
0
1
2





你可能感兴趣的:(google)