forked from springside/springside4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
springside#8 hzl7652提交的GuavaCacheDemo,但rebase出错了,再手工提交一遍。
- Loading branch information
1 parent
1cb90c9
commit cb23f45
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...s/showcase/src/main/java/org/springside/examples/showcase/cache/guava/GuavaCacheDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |