• Guava入门~RemovalListener


    RemovalNotification

    实现Map.Entry接口

    getCause()获取RemovalCause

    1.COLLECTED: key或value被垃圾回收

    2.EXPIRED:已过期;

    3.EXPLICIT:手动移除;

    4.REPLACED:被替换;

    5.SIZE:超过了最大限制数量。

    1. package bbejeck.guava.chapter6.cache;
    2. /**
    3. * User: Bill Bejeck
    4. * Date: 4/22/13
    5. * Time: 9:37 PM
    6. */
    7. import com.google.common.cache.RemovalCause;
    8. import com.google.common.cache.RemovalListener;
    9. public abstract class BaseRemovalListener<K, V> implements RemovalListener<K, V> {
    10. protected RemovalCause removalCause;
    11. protected K removedKey;
    12. protected V removedValue;
    13. public RemovalCause getRemovalCause() {
    14. return removalCause;
    15. }
    16. public K getRemovedKey() {
    17. return removedKey;
    18. }
    19. public V getRemovedValue() {
    20. return removedValue;
    21. }
    22. }
    1. package bbejeck.guava.chapter6.cache;
    2. import bbejeck.guava.common.model.TradeAccount;
    3. import com.google.common.cache.RemovalListener;
    4. import com.google.common.cache.RemovalNotification;
    5. /**
    6. * User: Bill Bejeck
    7. * Date: 4/20/13
    8. * Time: 11:49 PM
    9. */
    10. public class TradeAccountRemovalListener extends BaseRemovalListener, TradeAccount> {
    11. public void onRemoval(RemovalNotification<String, TradeAccount> notification) {
    12. this.removalCause = notification.getCause();
    13. this.removedKey = notification.getKey();
    14. this.removedValue = notification.getValue();
    15. }
    16. }
    1. package bbejeck.guava.chapter6.cache;
    2. import bbejeck.guava.common.model.Book;
    3. import bbejeck.guava.common.model.TradeAccount;
    4. import bbejeck.guava.common.service.BookServiceImpl;
    5. import bbejeck.guava.common.service.TradeAccountService;
    6. import com.google.common.base.Ticker;
    7. import com.google.common.cache.*;
    8. import org.junit.AfterClass;
    9. import org.junit.BeforeClass;
    10. import org.junit.Test;
    11. import java.util.concurrent.TimeUnit;
    12. import static org.hamcrest.CoreMatchers.is;
    13. import static org.junit.Assert.assertThat;
    14. /**
    15. * User: Bill Bejeck
    16. * Date: 4/20/13
    17. * Time: 10:17 PM
    18. */
    19. public class RemovalListenerTest {
    20. private static TradeAccountService tradeAccountService;
    21. private static BookServiceImpl bookService;
    22. @BeforeClass
    23. public static void startUpBeforeAll() {
    24. bookService = new BookServiceImpl();
    25. tradeAccountService = new TradeAccountService();
    26. }
    27. @AfterClass
    28. public static void tearDownAfterAll() throws Exception {
    29. bookService.shutdown();
    30. tradeAccountService.shutdown();
    31. }
    32. @Test
    33. public void testLoadingCacheExpireAfterWrite() throws Exception {
    34. TradeAccountRemovalListener removalListener = new TradeAccountRemovalListener();
    35. LoadingCache<String, TradeAccount> tradeAccountCache = CacheBuilder.newBuilder()
    36. .expireAfterWrite(5L, TimeUnit.MILLISECONDS)
    37. .maximumSize(5000L)
    38. .removalListener(removalListener)
    39. .ticker(Ticker.systemTicker())
    40. .build(new CacheLoader<String, TradeAccount>() {
    41. @Override
    42. public TradeAccount load(String key) throws Exception {
    43. return tradeAccountService.getTradeAccountById(key);
    44. }
    45. });
    46. //223,"Rogers, Jim",250000.12
    47. TradeAccount tradeAccount = tradeAccountCache.get("223");
    48. assertThat(tradeAccount.getBalance(), is(250000.12));
    49. Thread.sleep(10L);
    50. tradeAccountCache.get("223");
    51. assertThat(removalListener.getRemovalCause(), is(RemovalCause.EXPIRED));
    52. assertThat(removalListener.getRemovedValue(), is(tradeAccount));
    53. }
    54. @Test
    55. public void testRemovalAfterLastAccessed() throws Exception {
    56. BookRemovalListener bookRemovalListener = new BookRemovalListener();
    57. LoadingCache<String, Book> bookCache = CacheBuilder.newBuilder()
    58. .expireAfterAccess(10, TimeUnit.MILLISECONDS)
    59. .softValues()
    60. .recordStats()
    61. .removalListener(bookRemovalListener)
    62. .build(new CacheLoader<String, Book>() {
    63. @Override
    64. public Book load(String key) throws Exception {
    65. return bookService.findBookByIsbn(key);
    66. }
    67. });
    68. Book book = bookCache.get("ISBN-234567");
    69. assertThat(book.getAuthor(), is("Vandeley, Art"));
    70. assertThat(book.getIsbn(), is("ISBN-234567"));
    71. Thread.sleep(20);
    72. //Need to call again to force eviction
    73. Book bookII = bookCache.get("ISBN-234567");
    74. assertThat(bookII.getAuthor(), is("Vandeley, Art"));
    75. assertThat(bookII.getIsbn(), is("ISBN-234567"));
    76. CacheStats cacheStats = bookCache.stats();
    77. assertThat(cacheStats.evictionCount(),is(1l));
    78. assertThat(bookRemovalListener.getRemovalCause(), is(RemovalCause.EXPIRED));
    79. assertThat(bookRemovalListener.getRemovedKey(), is("ISBN-234567"));
    80. assertThat(bookRemovalListener.getRemovedValue(), is(book));
    81. }
    82. @Test
    83. public void testInvalidateBadValue() throws Exception {
    84. BookRemovalListener bookRemovalListener = new BookRemovalListener();
    85. LoadingCache<String, Book> bookCache = CacheBuilder.newBuilder()
    86. .expireAfterAccess(10, TimeUnit.HOURS)
    87. .softValues()
    88. .recordStats()
    89. .removalListener(bookRemovalListener)
    90. .build(new CacheLoader<String, Book>() {
    91. @Override
    92. public Book load(String key) throws Exception {
    93. return bookService.findBookByIsbn(key);
    94. }
    95. });
    96. Book book = bookCache.get("ISBN-234567");
    97. assertThat(book.getTitle(), is("Be an Architect"));
    98. bookCache.invalidate("ISBN-234567");
    99. assertThat(bookRemovalListener.getRemovalCause(), is(RemovalCause.EXPLICIT));
    100. assertThat(bookRemovalListener.getRemovedValue().getTitle(), is("Be an Architect"));
    101. }
    102. @Test
    103. public void testRefreshingCacheValues() throws Exception {
    104. TradeAccountRemovalListener removalListener = new TradeAccountRemovalListener();
    105. LoadingCache<String, TradeAccount> tradeAccountCache = CacheBuilder.newBuilder()
    106. .concurrencyLevel(10)
    107. .refreshAfterWrite(5L, TimeUnit.MILLISECONDS)
    108. .removalListener(removalListener)
    109. .ticker(Ticker.systemTicker())
    110. .recordStats()
    111. .build(new CacheLoader<String, TradeAccount>() {
    112. @Override
    113. public TradeAccount load(String key) throws Exception {
    114. return tradeAccountService.getTradeAccountById(key);
    115. }
    116. });
    117. //223,"Rogers, Jim",250000.12
    118. TradeAccount tradeAccount = tradeAccountCache.get("223");
    119. assertThat(tradeAccount.getBalance(), is(250000.12));
    120. Thread.sleep(10L);
    121. tradeAccountCache.get("223");
    122. CacheStats stats = tradeAccountCache.stats();
    123. assertThat(stats.loadSuccessCount(),is(2l));
    124. assertThat(removalListener.getRemovalCause(), is(RemovalCause.REPLACED));
    125. assertThat(removalListener.getRemovedValue(), is(tradeAccount));
    126. }
    127. }

  • 相关阅读:
    web安全之XSS攻击
    数据库应用:CentOS 7离线安装PostgreSQL
    NR PDSCH(三) TB size determination
    【LeetCode】54、螺旋矩阵
    Android系统的启动流程
    三、程序员指南:数据平面开发套件
    java基础
    python手柄pygame joystick文档
    41-面向对象编程(中级部分)-2
    HTML制作一个汽车介绍网站【大学生网页制作期末作业】(汽车首页 1页 带psd)
  • 原文地址:https://blog.csdn.net/gqltt/article/details/126705297