Example #1
0
 public virtual void Add(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
 {
     _cache?.Add(name + model.Id.ToString(), model);
     _dbSet.Add(model);
     _db.SaveChanges();
     _rep?.Add(model);
 }
 public void Add_Valid_Class_And_Verify_If_Has_Value()
 {
     cache.Add <T>();
     Assert.IsTrue(cache.HasMethodSet());
     Assert.IsTrue(cache.HasMethodGet());
     Assert.IsTrue(cache.HasAttribute());
     Assert.IsTrue(cache.HasProperty());
 }
Example #3
0
        public async Task <T> Execute(string parameters, bool canCache)
        {
            if (canCache)
            {
                // get the most recent non expired item from cache
                var cached = GetFromCache(parameters);
                if (cached != null)
                {
                    return(cached);
                }
            }

            // iterate and execute until we get a result from any operation
            foreach (var operation in _operations)
            {
                if (!CheckQuota(operation))
                {
                    _logger.LogCritical($"Over quota in {GetType().Name}.{operation.GetType()}");

                    // fall back to next operation
                    continue;
                }

                var resultObj = await operation.Execute(parameters);

                if (resultObj != null)
                {
                    var json = JsonConvert.SerializeObject(resultObj);
                    _cacheRepository.Add(GetType().Name, operation, parameters, json, false);
                    return(resultObj);
                }

                // add in cache even if its errored because it has to count in the service's quota
                _cacheRepository.Add(GetType().Name, operation, parameters, string.Empty, true);
            }

            _logger.LogCritical($"{GetType().Name}: All operations failed to returned data. Attempting to use expired cache...");

            // fall back to any cached item even if its expired
            var cachedExpired = GetFromCache(parameters, true);

            if (cachedExpired == null)
            {
                _logger.LogCritical($"{GetType().Name}: No data returned");
            }

            return(cachedExpired);
        }
Example #4
0
        private IEnumerable <FileSystemItem> DoGetEntries(FileSystemRequest request)
        {
            var cancellableOperation = new CancellableOperation <string, IEnumerable <FileSystemItem> >(
                fileExplorerService.GetItemsInPath, request.Path, request.SourceToken, EploreFilesOperationsGroup);

            cancellableOperation.CancelAfter(requestConfiguration.ExecutionTimeout);
            cancellableOperation.Operation.Wait();

            var result = cancellableOperation.Operation.Result;

            if (result.Any())
            {
                fileSystemItemsCacheRepository.Add(request.Path, result.ToArray());
            }

            return(result);
        }
Example #5
0
        //in this region we can add Entitys
        #region Add

        /// <summary>
        /// Adding module if you include cache add cache
        /// </summary>
        /// <param name="model"></param>
        /// <param name="lineNumber"></param>
        /// <param name="caller"></param>

        public void Add(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
        {
            if (string.IsNullOrEmpty(model.Id))
            {
                model.Id = ObjectId.GenerateNewId().ToString();
            }
            _cache?.Add(model.Id, model);
            _db.InsertOne(model);
        }
Example #6
0
        public void Add(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
        {
            Stopwatch watch = new Stopwatch();

            try
            {
                watch.Start();
                _cache?.Add(name + model.Id.ToString(), model);
                _dbSet.Add(model);
                watch.Stop();
                Logging("Add modal", watch.ElapsedMilliseconds, lineNumber, caller, model);
            }
            catch (Exception ext)
            {
                // _cache?.CatcheDelete("Add Exeptions",model.Id.ToString(), model);
                watch.Stop();
                ErrorLogging("Add Error", watch.ElapsedMilliseconds, model, ext, caller, lineNumber);
            }
        }
Example #7
0
        public void RefreshCache()
        {
            var newCachedList = CachedList != null ? new List <CacheConfigurationDTO>(CachedList) : new List <CacheConfigurationDTO>();

            var lastModifyDate    = newCachedList.OrderByDescending(a => a.LastModifyDate).FirstOrDefault()?.LastModifyDate;
            var configurationList = _storageProvider.Search(_applicationName, lastModifyDate ?? DateTime.MinValue);

            if (!configurationList.Any())
            {
                return;
            }

            foreach (var configuration in configurationList)
            {
                var existingCacheItem = newCachedList.FirstOrDefault(a => a.Id == configuration.Id);
                if (existingCacheItem != null)
                {
                    existingCacheItem.Type           = configuration.Type;
                    existingCacheItem.Value          = configuration.Value;
                    existingCacheItem.LastModifyDate = configuration.LastModifyDate;
                    existingCacheItem.IsActive       = configuration.IsActive;
                }
                else
                {
                    newCachedList.Add(new CacheConfigurationDTO
                    {
                        Id              = configuration.Id,
                        Type            = configuration.Type,
                        LastModifyDate  = configuration.LastModifyDate,
                        Value           = configuration.Value,
                        Name            = configuration.Name,
                        ApplicationName = configuration.ApplicationName,
                        CreationDate    = configuration.CreationDate,
                        IsActive        = configuration.IsActive
                    });
                }
            }

            _cacheRepository.Remove(CacheKey.ApplicationConfiguration);
            _cacheRepository.Add(CacheKey.ApplicationConfiguration, newCachedList, _refreshTimerIntervalInMs);
        }
Example #8
0
        private async Task <T> CacheResponseBody <T>(HttpResponseMessage response, string path)
        {
            // Read response body (content).
            T data = await response.Content.ReadAsAsync <T>();

            // Read the Cache-Control max-age value.
            TimeSpan?expiresIn = GetMaxAge(response);

            // Save to the cache.
            _cacheRepository.Add <T>(path, data, expiresIn.Value);
            return(data);
        }
Example #9
0
 public virtual async Task Add(T model)
 {
     try
     {
         _cache?.Add(model.Id.ToString(), model);
         _db.Add(model);
         Save();
     }
     catch (Exception ext)
     {
         throw new System.Exception("Add Exeption", ext);
     }
 }
        public T Get <T>(string cacheKey, Func <T> fallbackFunction, TimeSpan timeSpan, bool useCache = true) where T : class
        {
            if (_cacheRepository == null || !useCache)
            {
                return(fallbackFunction());
            }

            var data = _cacheRepository.Get(cacheKey);

            if (data != null)
            {
                if (data == DBNull.Value)
                {
                    return(null);
                }
                return(data as T);
            }

            var data2 = fallbackFunction();

            _cacheRepository.Add(cacheKey, data2 ?? (object)DBNull.Value, timeSpan);

            return(data2);
        }
Example #11
0
        private CountFilesResponse DoCount(FileSystemRequest request)
        {
            var cancellableOperation = new CancellableOperation <string, CountFilesResult>(
                fileExplorerService.CountFilesInDirectory, request.Path, request.SourceToken, CountOperationsGroup);

            requestPool.Add(cancellableOperation);

            cancellableOperation.CancelAfter(requestConfiguration.ExecutionTimeout);
            cancellableOperation.Operation.Wait();

            var result   = cancellableOperation.Operation.Result;
            var response = CreateCountFilesResponse(result, cancellableOperation.Cancelled);

            countFilesCacheRepository.Add(request.Path, response);

            return(response);
        }
        public void InsertWordToCache(string requestWord, IList <string> anagrams)
        {
            InsertWordToRequestedWords(requestWord);

            bool wordIsCached = _cacheRepository.GetCacheListByRequestWord(requestWord).Any();

            if (!wordIsCached)
            {
                foreach (var item in anagrams)
                {
                    CachedEntity cachedEntity = new CachedEntity()
                    {
                        RequestId = _requestRepository.GetByWord(requestWord).Id,
                        AnagramId = _wordRepository.GetByWord(item).Id
                    };
                    _cacheRepository.Add(cachedEntity);
                }
            }
        }
 public void Add(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
 {
     _cache?.Add(model.Id.ToString(), model);
     _db.Query(name).Insert(model);
 }
Example #14
0
 public void Add(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
 {
     _cache?.Add(model.Id.ToString(), model);
     _session.Store(model);
     _session.SaveChanges();
 }
Example #15
0
        //in this region we can add Entitys
        #region Add

        /// <summary>
        /// Adding module if you include cache add cache
        /// </summary>
        /// <param name="model"></param>
        /// <param name="lineNumber"></param>
        /// <param name="caller"></param>

        public void Add(T model, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null)
        {
            _cache?.Add(model.Id, model);
            _db.InsertOne(model);
        }
 protected void StartCache()
 {
     _cacheService.Add <TValue>();
     _cacheService.Add <TFilter>();
 }