Skip to content

Commit

Permalink
springside#8 hzl7652提交的GuavaCacheDemo,但rebase出错了,再手工提交一遍。
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Mar 27, 2012
1 parent 1cb90c9 commit cb23f45
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.springside.examples.showcase.cache.guava;

import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springside.examples.showcase.common.entity.User;
import org.springside.examples.showcase.common.service.AccountManager;
import org.springside.modules.test.data.H2Fixtures;
import org.springside.modules.test.spring.SpringTxTestCase;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
* 本地缓存演示,使用GuavaCache.
* 可以通过log4jdbc查看sql情况.
*
* @author hzl7652
*/
@ContextConfiguration(locations = { "/applicationContext.xml" })
@TransactionConfiguration(transactionManager = "defaultTransactionManager")
public class GuavaCacheDemo extends SpringTxTestCase {

@Resource
private AccountManager accountManager;

@Test
public void test1() throws Exception {
H2Fixtures.reloadAllTable(dataSource, "/data/sample-data.xml");
LoadingCache<Long, User> cache = CacheBuilder.newBuilder().maximumSize(100)//设置缓存个数
.expireAfterAccess(3, TimeUnit.SECONDS)//缓存过期时间为8秒
.build(new CacheLoader<Long, User>() {

@Override
public User load(Long key) throws Exception {
return accountManager.getUser(key);
}

});

User user = cache.get(1L);
assertEquals("admin", user.getLoginName());//第一次加载会查数据库

User user2 = cache.get(1L);//第二次加载时直接从缓存里取
assertEquals("admin", user2.getLoginName());

Thread.sleep(10000);
user = cache.get(1L);
;//第三次加载时,因为缓存已经过期所以会查数据库
System.out.println(user);
assertEquals("admin", user.getLoginName());
}
}

0 comments on commit cb23f45

Please sign in to comment.