Guest User

Untitled

a guest
Dec 10th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. public final class KeyHolder {
  2. private final String clientId;
  3. private final String deviceId;
  4. private final int processId;
  5. private final Cache<String, List<Response>> userCache;
  6. private static final long MAXIMUM_CACHE_SIZE = 5000000;
  7. private static final long EXPIRE_AFTER_WRITE = 120; // this is in seconds
  8.  
  9. private KeyHolder(Builder builder) {
  10. this.clientId = builder.clientId;
  11. this.deviceId = builder.deviceId;
  12. this.processId = builder.processId;
  13. this.maximumCacheSize = builder.maximumCacheSize;
  14. this.expireAfterWrite = builder.expireAfterWrite;
  15.  
  16. // how to execute this line only once
  17. this.userCache =
  18. CacheBuilder
  19. .newBuilder()
  20. .maximumSize(maximumCacheSize)
  21. .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
  22. .removalListener(
  23. RemovalListeners.asynchronous(new CustomListener(),
  24. Executors.newSingleThreadScheduledExecutor())).build();
  25.  
  26. }
  27.  
  28. public static class Builder {
  29. protected final int processId;
  30. protected String clientId = null;
  31. protected String deviceId = null;
  32. protected long maximumCacheSize = MAXIMUM_CACHE_SIZE;
  33. protected long expireAfterWrite = EXPIRE_AFTER_WRITE;
  34.  
  35.  
  36. public Builder(int processId) {
  37. this.processId = processId;
  38. }
  39.  
  40. public Builder setClientId(String clientId) {
  41. this.clientId = clientId;
  42. return this;
  43. }
  44.  
  45. public Builder setDeviceId(String deviceId) {
  46. this.deviceId = deviceId;
  47. return this;
  48. }
  49.  
  50. public Builder setMaximumCacheSize(long size) {
  51. this.maximumCacheSize = size;
  52. return this;
  53. }
  54.  
  55. public Builder setExpiryTimeAfterWrite(long duration) {
  56. this.expireAfterWrite = duration;
  57. return this;
  58. }
  59.  
  60. public KeyHolder build() {
  61. return new KeyHolder(this);
  62. }
  63. }
  64.  
  65. // getters here
  66. }
  67.  
  68. this.userCache =
  69. CacheBuilder
  70. .newBuilder()
  71. .maximumSize(maximumCacheSize)
  72. .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
  73. .removalListener(
  74. RemovalListeners.asynchronous(new CustomListener(),
  75. Executors.newSingleThreadScheduledExecutor())).build();
  76.  
  77. public class DataClient implements Client {
  78. private final ExecutorService executor = Executors.newFixedThreadPool(10);
  79.  
  80. // for synchronous call
  81. @Override
  82. public List<Response> executeSync(KeyHolder key) {
  83. Cache<String, List<Response>> userCache = key.getUserCache();
  84. List<Response> response = userCache.getIfPresent(key.getUUID());
  85. if (CollectionUtils.isNotEmpty(response)) {
  86. return response;
  87. }
  88. // if not in cache, then normally call the flow and populate the cache
  89. List<Response> dataResponse = null;
  90. Future<List<Response>> future = null;
  91. try {
  92. future = executeAsync(key);
  93. dataResponse = future.get(key.getTimeout(), TimeUnit.MILLISECONDS);
  94. userCache.put(key.getUUID(), dataResponse);
  95. } catch (TimeoutException ex) {
  96. // log error and return DataResponse
  97. } catch (Exception ex) {
  98. // log error and return DataResponse
  99. }
  100.  
  101. return dataResponse;
  102. }
  103. }
  104.  
  105. class KeyHolder {
  106. private static Map<String, List<Response>> userCache;
  107.  
  108. public static KeyHolder.Builder newBuilder(int id) {
  109. if(userCache == null) {
  110. userCache = ...;
  111. }
  112.  
  113. return new Builder(id);
  114. }
  115. }
  116.  
  117. class DataClient {
  118. private Map<String, List<Response>> userCache;
  119.  
  120. public List<Response> executeSync(KeyHolder key) {
  121. List<Response> response = userCache.getIfPresent(key.getUUID());
  122. //...
  123. }
  124. }
  125.  
  126. public final class KeyHolder {
  127. private final String clientId;
  128. private final String deviceId;
  129. private final int processId;
  130.  
  131. //this var is now static, so it is shared across all instances
  132. private static Cache<String, List<Response>> userCache = null;
  133.  
  134. private static final long MAXIMUM_CACHE_SIZE = 5000000;
  135. private static final long EXPIRE_AFTER_WRITE = 120; // this is in seconds
  136.  
  137. private KeyHolder(Builder builder) {
  138. this.clientId = builder.clientId;
  139. this.deviceId = builder.deviceId;
  140. this.processId = builder.processId;
  141. this.maximumCacheSize = builder.maximumCacheSize;
  142. this.expireAfterWrite = builder.expireAfterWrite;
  143.  
  144. //this will be executed only the first time, when the var is null
  145. if (userCache == null) {
  146. userCache =
  147. CacheBuilder
  148. .newBuilder()
  149. .maximumSize(maximumCacheSize)
  150. .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
  151. .removalListener(
  152. RemovalListeners.asynchronous(new CustomListener(),
  153. Executors.newSingleThreadScheduledExecutor())).build();
  154.  
  155. }
  156.  
  157. //rest of your class below
  158.  
  159. private static Cache<String, List<Response>> userCache;
  160.  
  161. private KeyHolder(Builder builder) {
  162. ...
  163.  
  164. if (userCache == null) {
  165. userCache = CacheBuilder
  166. .newBuilder()
  167. .maximumSize(maximumCacheSize)
  168. .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
  169. .removalListener(
  170. RemovalListeners.asynchronous(new CustomListener(), Executors.newSingleThreadScheduledExecutor())
  171. ).build();
  172. }
  173. }
  174.  
  175. // initialize the cache while defining it
  176. // replace maximumCacheSize and expireAfterWrite with constants
  177. private static final Cache... = CacheBuilder.newBuilder()...;
Add Comment
Please, Sign In to add comment