예제 #1
0
        public async Task <FunctionVm> Handle(GetDataFromFunctionQuery request, CancellationToken cancellationToken)
        {
            FunctionVm functionVm = new FunctionVm();

            string key       = $@"getservicosveiculosobrigatorios-{request.pempresa}-${request.previsao}-${request.pveiculoversao}";
            var    cacheData = await _cacheHandler
                               .GetCacheValueAsync(key);

            // TODO: should implement a way to detect object value changes
            if (cacheData == null || cacheData.Length <= 0)
            {
                IEnumerable <T_ServicoVeiculo> functionResult = await _applicationDbContext
                                                                .GetData(request.pempresa, request.previsao, request.pveiculoversao);

                functionVm.Data = functionResult;

                byte[] serializedFunctionVm = _jsonSerializer
                                              .ObjectToByteArray(functionVm);

                _cacheHandler
                .SetCacheValueAsync(key, serializedFunctionVm);

                return(functionVm);
            }

            byte[] byteArrayCacheData = await _cacheHandler
                                        .GetCacheValueAsync(key);

            string cacheDataJsonString = _jsonSerializer
                                         .GetJsonStringFromByteArray(byteArrayCacheData);

            return(_jsonSerializer
                   .GetDeserializedObject(cacheDataJsonString));
        }