Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class KeyHolder {
- private final String clientId;
- private final String deviceId;
- private final int processId;
- private final Cache<String, List<Response>> userCache;
- private static final long MAXIMUM_CACHE_SIZE = 5000000;
- private static final long EXPIRE_AFTER_WRITE = 120; // this is in seconds
- private KeyHolder(Builder builder) {
- this.clientId = builder.clientId;
- this.deviceId = builder.deviceId;
- this.processId = builder.processId;
- this.maximumCacheSize = builder.maximumCacheSize;
- this.expireAfterWrite = builder.expireAfterWrite;
- // how to execute this line only once
- this.userCache =
- CacheBuilder
- .newBuilder()
- .maximumSize(maximumCacheSize)
- .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
- .removalListener(
- RemovalListeners.asynchronous(new CustomListener(),
- Executors.newSingleThreadScheduledExecutor())).build();
- }
- public static class Builder {
- protected final int processId;
- protected String clientId = null;
- protected String deviceId = null;
- protected long maximumCacheSize = MAXIMUM_CACHE_SIZE;
- protected long expireAfterWrite = EXPIRE_AFTER_WRITE;
- public Builder(int processId) {
- this.processId = processId;
- }
- public Builder setClientId(String clientId) {
- this.clientId = clientId;
- return this;
- }
- public Builder setDeviceId(String deviceId) {
- this.deviceId = deviceId;
- return this;
- }
- public Builder setMaximumCacheSize(long size) {
- this.maximumCacheSize = size;
- return this;
- }
- public Builder setExpiryTimeAfterWrite(long duration) {
- this.expireAfterWrite = duration;
- return this;
- }
- public KeyHolder build() {
- return new KeyHolder(this);
- }
- }
- // getters here
- }
- this.userCache =
- CacheBuilder
- .newBuilder()
- .maximumSize(maximumCacheSize)
- .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
- .removalListener(
- RemovalListeners.asynchronous(new CustomListener(),
- Executors.newSingleThreadScheduledExecutor())).build();
- public class DataClient implements Client {
- private final ExecutorService executor = Executors.newFixedThreadPool(10);
- // for synchronous call
- @Override
- public List<Response> executeSync(KeyHolder key) {
- Cache<String, List<Response>> userCache = key.getUserCache();
- List<Response> response = userCache.getIfPresent(key.getUUID());
- if (CollectionUtils.isNotEmpty(response)) {
- return response;
- }
- // if not in cache, then normally call the flow and populate the cache
- List<Response> dataResponse = null;
- Future<List<Response>> future = null;
- try {
- future = executeAsync(key);
- dataResponse = future.get(key.getTimeout(), TimeUnit.MILLISECONDS);
- userCache.put(key.getUUID(), dataResponse);
- } catch (TimeoutException ex) {
- // log error and return DataResponse
- } catch (Exception ex) {
- // log error and return DataResponse
- }
- return dataResponse;
- }
- }
- class KeyHolder {
- private static Map<String, List<Response>> userCache;
- public static KeyHolder.Builder newBuilder(int id) {
- if(userCache == null) {
- userCache = ...;
- }
- return new Builder(id);
- }
- }
- class DataClient {
- private Map<String, List<Response>> userCache;
- public List<Response> executeSync(KeyHolder key) {
- List<Response> response = userCache.getIfPresent(key.getUUID());
- //...
- }
- }
- public final class KeyHolder {
- private final String clientId;
- private final String deviceId;
- private final int processId;
- //this var is now static, so it is shared across all instances
- private static Cache<String, List<Response>> userCache = null;
- private static final long MAXIMUM_CACHE_SIZE = 5000000;
- private static final long EXPIRE_AFTER_WRITE = 120; // this is in seconds
- private KeyHolder(Builder builder) {
- this.clientId = builder.clientId;
- this.deviceId = builder.deviceId;
- this.processId = builder.processId;
- this.maximumCacheSize = builder.maximumCacheSize;
- this.expireAfterWrite = builder.expireAfterWrite;
- //this will be executed only the first time, when the var is null
- if (userCache == null) {
- userCache =
- CacheBuilder
- .newBuilder()
- .maximumSize(maximumCacheSize)
- .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
- .removalListener(
- RemovalListeners.asynchronous(new CustomListener(),
- Executors.newSingleThreadScheduledExecutor())).build();
- }
- //rest of your class below
- private static Cache<String, List<Response>> userCache;
- private KeyHolder(Builder builder) {
- ...
- if (userCache == null) {
- userCache = CacheBuilder
- .newBuilder()
- .maximumSize(maximumCacheSize)
- .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
- .removalListener(
- RemovalListeners.asynchronous(new CustomListener(), Executors.newSingleThreadScheduledExecutor())
- ).build();
- }
- }
- // initialize the cache while defining it
- // replace maximumCacheSize and expireAfterWrite with constants
- private static final Cache... = CacheBuilder.newBuilder()...;
Add Comment
Please, Sign In to add comment