public PropertyListingBusiness(ILogger <PropertyListingBusiness> logger, IMapper mapper, IPropertyListingRepository listingRepository, PropertyListingResponse listingResponse, ICacheManager cache) { if (logger == null || mapper == null || listingRepository == null || listingResponse == null || cache == null) { throw new ArgumentNullException("Please check the values being passed"); } _logger = logger; _mapper = mapper; _listingRepository = listingRepository; _listingResponse = listingResponse; _cache = cache; }
public async Task <PropertyListingResponse> GetListing(PropertyListingRequest request) { //Validate Request Parameters first if (request == null || ValidateRequestParameters(request) != "") { throw new ArgumentNullException(nameof(request)); } try { //Generate a key from the request parameters var cacheKey = new StringBuilder().GenerateKey(request).ToString(); //Get Listing from cache var cacheResponse = GetListingFromCache(cacheKey); //If listing is found in the cache, return the listing. No need to get from db if (cacheResponse != null && cacheResponse.Items != null) { cacheResponse.Message = $"Cache HIT -- Found Data in Cache"; return(cacheResponse); } //Get Listing from DB _logger.LogInformation($"Cache Miss -- Getting data from DB"); _listingResponse = await GetListingFromDB(request); //Set Listing in the cache only if there is some result in the DB, otherwise there is no use if (_listingResponse != null && _listingResponse.Items != null && _listingResponse.Items.Count() > 0) { SetListingCache(_listingResponse, cacheKey); } } catch (Exception ex) { _logger.LogError($"Error in [GetListing] -> {ex.StackTrace}"); } return(_listingResponse); }
private void SetListingCache(PropertyListingResponse listingResponse, string cacheKey) { _cache.SetCache(cacheKey, _listingResponse); }