Что лучше? Кэш в памяти или объект контекста службы - PullRequest
0 голосов
/ 08 января 2019

В Java рекомендуется использовать кэш в памяти для доступа к переменным внутри ответа службы на один запрос. Или лучше просто поддерживать объект контекста службы для этого запроса и обращаться к необходимым переменным с помощью объекта.

Нет ли задержки в кеше в памяти для извлечения объекта ответа?

Например:

Объект контекста службы:

public class ServiceResponseAccessor {

    private ServiceResponse response;

    public String getCustomerId() {
        return response.getCustomerId();
    }

    public Preferences getCustomerPreferences() {
        Customer customer = response.getCustomer();
        if (customer == null) {
            throw new Exception("No Customer Preferences");
        }
        return customer.getPreferences();
    }
}

Использование кэша в памяти:

public class ServiceResponseAccessor {

    private CacheAccessor cacheAccessor;
    private String cacheKey;

    public ServiceResponseAccessor(String cacheKey) {
        this.cacheKey = cacheKey;
    }

    public String getCustomerId() {
        ServiceResponse response = cacheAccessor.get(cacheKey);
        return response.getCustomerId();
    }

    public Preferences getCustomerPreferences() {
            ServiceResponse response = cacheAccessor.get(cacheKey);
        Customer customer = response.getCustomer();
        if (customer == null) {
            throw new Exception("No Customer Preferences");
        }
        return customer.getPreferences();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...