You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CacheStrategy.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.data;
  17. import com.vaadin.shared.Range;
  18. /**
  19. * Determines what data an {@link AbstractRemoteDataSource} should fetch and
  20. * keep cached.
  21. *
  22. * @since 7.4
  23. * @author Vaadin Ltd
  24. */
  25. public interface CacheStrategy {
  26. /**
  27. * A helper class for creating a simple symmetric cache strategy that uses
  28. * the same logic for both rows before and after the currently cached range.
  29. * <p>
  30. * This simple approach rules out more advanced heuristics that would take
  31. * the current scrolling direction or past scrolling behavior into account.
  32. */
  33. public static abstract class AbstractBasicSymmetricalCacheStrategy
  34. implements CacheStrategy {
  35. @Override
  36. public void onDataArrive(double roundTripTime, int rowCount) {
  37. // NOP
  38. }
  39. @Override
  40. public Range getMinCacheRange(Range displayedRange, Range cachedRange,
  41. Range estimatedAvailableRange) {
  42. int cacheSize = getMinimumCacheSize(displayedRange.length());
  43. return displayedRange.expand(cacheSize, cacheSize)
  44. .restrictTo(estimatedAvailableRange);
  45. }
  46. @Override
  47. public Range getMaxCacheRange(Range displayedRange, Range cachedRange,
  48. Range estimatedAvailableRange) {
  49. int cacheSize = getMaximumCacheSize(displayedRange.length());
  50. return displayedRange.expand(cacheSize, cacheSize)
  51. .restrictTo(estimatedAvailableRange);
  52. }
  53. /**
  54. * Gets the maximum number of extra items to cache in one direction.
  55. *
  56. * @param pageSize
  57. * the current number of items used at once
  58. * @return maximum of items to cache
  59. */
  60. public abstract int getMaximumCacheSize(int pageSize);
  61. /**
  62. * Gets the the minimum number of extra items to cache in one direction.
  63. *
  64. * @param pageSize
  65. * the current number of items used at once
  66. * @return minimum number of items to cache
  67. */
  68. public abstract int getMinimumCacheSize(int pageSize);
  69. }
  70. /**
  71. * The default cache strategy used by {@link AbstractRemoteDataSource},
  72. * using multiples of the page size for determining the minimum and maximum
  73. * number of items to keep in the cache. By default, at least three times
  74. * the page size both before and after the currently used range are kept in
  75. * the cache and items are discarded if there's yet another page size worth
  76. * of items cached in either direction.
  77. */
  78. public static class DefaultCacheStrategy
  79. extends AbstractBasicSymmetricalCacheStrategy {
  80. private final int minimumRatio;
  81. private final int maximumRatio;
  82. /**
  83. * Creates a DefaultCacheStrategy keeping between 3 and 4 pages worth of
  84. * data cached both before and after the active range.
  85. */
  86. public DefaultCacheStrategy() {
  87. this(3, 4);
  88. }
  89. /**
  90. * Creates a DefaultCacheStrategy with custom ratios for how much data
  91. * to cache. The ratios denote how many multiples of the currently used
  92. * page size are kept in the cache in each direction.
  93. *
  94. * @param minimumRatio
  95. * the minimum number of pages to keep in the cache in each
  96. * direction
  97. * @param maximumRatio
  98. * the maximum number of pages to keep in the cache in each
  99. * direction
  100. */
  101. public DefaultCacheStrategy(int minimumRatio, int maximumRatio) {
  102. this.minimumRatio = minimumRatio;
  103. this.maximumRatio = maximumRatio;
  104. }
  105. @Override
  106. public int getMinimumCacheSize(int pageSize) {
  107. return pageSize * minimumRatio;
  108. }
  109. @Override
  110. public int getMaximumCacheSize(int pageSize) {
  111. return pageSize * maximumRatio;
  112. }
  113. }
  114. /**
  115. * Called whenever data requested by the data source has arrived. This
  116. * information can e.g. be used for measuring how long it takes to fetch
  117. * different number of rows from the server.
  118. * <p>
  119. * A cache strategy implementation cannot use this information to keep track
  120. * of which items are in the cache since the data source might discard items
  121. * without notifying the cache strategy.
  122. *
  123. * @param roundTripTime
  124. * the total number of milliseconds elapsed from requesting the
  125. * data until the response was passed to the data source
  126. * @param rowCount
  127. * the number of received rows
  128. */
  129. public void onDataArrive(double roundTripTime, int rowCount);
  130. /**
  131. * Gets the minimum row range that should be cached. The data source will
  132. * fetch new data if the currently cached range does not fill the entire
  133. * minimum cache range.
  134. *
  135. * @param displayedRange
  136. * the range of currently displayed rows
  137. * @param cachedRange
  138. * the range of currently cached rows
  139. * @param estimatedAvailableRange
  140. * the estimated range of rows available for the data source
  141. *
  142. * @return the minimum range of rows that should be cached, should at least
  143. * include the displayed range and should not exceed the total
  144. * estimated available range
  145. */
  146. public Range getMinCacheRange(Range displayedRange, Range cachedRange,
  147. Range estimatedAvailableRange);
  148. /**
  149. * Gets the maximum row range that should be cached. The data source will
  150. * discard cached rows that are outside the maximum range.
  151. *
  152. * @param displayedRange
  153. * the range of currently displayed rows
  154. * @param cachedRange
  155. * the range of currently cached rows
  156. * @param estimatedAvailableRange
  157. * the estimated range of rows available for the data source
  158. *
  159. * @return the maximum range of rows that should be cached, should at least
  160. * include the displayed range and should not exceed the total
  161. * estimated available range
  162. */
  163. public Range getMaxCacheRange(Range displayedRange, Range cachedRange,
  164. Range estimatedAvailableRange);
  165. }