private async Task <ActionResult <GuidInfo> > ProcessGuidInfo(GuidInfoEntity info, bool fromCache) { if (info.Expire < _clock.UtcNow) { // retrieved info has expired if (fromCache) { // remove it from cache await InvalidateCache(info.Guid); } // and return 410 code (Gone) return(StatusCode((int)HttpStatusCode.Gone, new GuidApiError(GuidErrorCode.GuidExpired))); } else { // this is a valid (not expired) guid if (!fromCache) { // retrieved from database, add it to cache // NOTE: Assuming Redis LRU caching, see https://redis.io/topics/lru-cache // NOTE: Alternatively, we could set an expiration time for entries await _cache.SetEntityAsync(_cachePrefix, info.Guid.ToString(), info); } return(info.ToGuidInfo()); } }
private async Task <ActionResult <GuidInfo> > CreateGuidInfo(System.Guid guid, GuidInfoBase info) { if (string.IsNullOrWhiteSpace(info.User)) { return(BadRequest(new GuidApiError(GuidErrorCode.InvalidUser))); } else { var entity = new GuidInfoEntity() { Guid = guid, Expire = info.Expire ?? GetDefaultExpireDate(), User = info.User }; await _context.GuidInfos.AddAsync(entity); await _context.SaveChangesAsync(); var guidInfo = entity.ToGuidInfo(); return(CreatedAtRoute("GetGuidInfo", new { id = guidInfo.Guid }, guidInfo)); } }