public async Task <IActionResult> GetList() { if (_cacheService.Any("users")) { var users = _cacheService.Get <List <GetUsersResorce> >("users"); return(Ok(users)); } else { BaseResponse <IEnumerable <User> > userListResponse = await userServices.GetWhere(x => x.Id > 0); if (userListResponse.Success) { var result = userServices.GetWhereUserResource(userListResponse.Extra); _cacheService.Add("users", result); return(Ok(result)); } else { return(BadRequest(userListResponse.ErrorMessage)); } } }
public ListResponse <City> GetCountry(string countryName) { try { if (!string.IsNullOrEmpty(countryName)) { if (_cacheService.Any(countryName)) { List <City> cities = _cacheService.Get <List <City> >(countryName); if (cities != null) { return new ListResponse <City>() { IsSuccess = true, Message = "Transaction Successful.", Result = cities, TotalCount = cities.Count(), Exception = null } } ; else { return new ListResponse <City>() { IsSuccess = false, Message = "No Registration For This Country.", Result = new List <City>(), TotalCount = 0, Exception = null } }; } else { return(new ListResponse <City>() { IsSuccess = false, Message = "No Registration For This Country.", Result = new List <City>(), TotalCount = 0, Exception = null }); } } else { return(new ListResponse <City>() { IsSuccess = false, Message = "Parameter Null", Result = new List <City>(), TotalCount = 0, Exception = null }); } } catch (Exception ex) { return(new ListResponse <City>() { IsSuccess = false, Message = "An Error Occurred.", Result = new List <City>(), TotalCount = 0, Exception = ex }); } }
public override void Intercept(IInvocation invocation) { if (!_cacheService.IsEnabled) { invocation.Proceed(); return; } var method = invocation.MethodInvocationTarget; var key = Key(invocation); if (_cacheService.Any(key).Result) { var resultType = invocation.Method.ReturnType.GenericTypeArguments.FirstOrDefault(); var returnValue = _cacheService.Get(key).Result; dynamic temp = JsonConvert.DeserializeObject(returnValue, resultType); invocation.ReturnValue = Task.FromResult(temp); return; } invocation.Proceed(); var isAsync = method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null; if (isAsync && typeof(Task).IsAssignableFrom(method.ReturnType)) { invocation.ReturnValue = InterceptAsync((dynamic)invocation.ReturnValue, key); } }
public CitiesController(ICacheService cacheService) { _cacheService = cacheService; if (!_cacheService.Any("turkey")) { _cacheService.Add("turkey", GetCityList("turkey"), 5); } }
public IActionResult Index() { if (_cacheService.Any("books")) { var books = _cacheService.Get <List <Book> >("books"); return(Ok(books)); } _cacheService.Add("books", _books); return(Ok(_books)); }
public async Task <Result> CheckIfExists(string key) { Result rslt = new Result(); try { rslt.Data = await _cacheService.Any(key); rslt.Status = 1; rslt.Message = "Success"; } catch (Exception ex) { rslt.Status = 0; rslt.Message = ex.Message; } return(rslt); }
public override void Intercept(IInvocation invocation) { //fullname contains namespace with method name var fullName = $"{invocation.Method.ReflectedType.FullName}.{invocation.Method.Name}"; var arguments = invocation.Arguments.ToArray(); var key = $"{fullName}({JsonConvert.SerializeObject(arguments)})"; if (_cacheService.Any(key)) { invocation.ReturnValue = _cacheService.Get(key); } else { invocation.Proceed(); _cacheService.Add(key, invocation.ReturnValue, _timeout); } }