Пример #1
0
 private IList GetSelectedKeys(DocumentCache cache, long evictSize)
 {
     lock (_index.SyncRoot)
     {
         return(_index.GetSelectedKeys(cache, evictSize));
     }
 }
Пример #2
0
        bool IEvictionPolicy.Execute(DocumentCache cache, long evictSize)
        {
            if (LoggerManager.Instance.StorageLogger != null && LoggerManager.Instance.StorageLogger.IsDebugEnabled)
            {
                LoggerManager.Instance.StorageLogger.Debug("LocalCache.Evict()", "Cache Size: {0}" + cache.CacheCount().ToString());
            }

            //muds:
            //if user has updated the values in configuration file then new values will be reloaded.

            DateTime startTime    = DateTime.Now;
            IList    selectedKeys = GetSelectedKeys(cache, evictSize);
            DateTime endTime      = DateTime.Now;

            if (LoggerManager.Instance.StorageLogger != null && LoggerManager.Instance.StorageLogger.IsDebugEnabled)
            {
                LoggerManager.Instance.StorageLogger.Debug("LocalCache.Evict()", String.Format("Time Span for {0} Items: " + (endTime - startTime), selectedKeys.Count));
            }

            IEnumerator e = selectedKeys.GetEnumerator();

            while (e.MoveNext())
            {
                long key = (long)e.Current;
                try
                {
                    cache.CacheRemove(key);
                    //removedItems = cache.RemoveSync(keysTobeRemoved.ToArray(), ItemRemoveReason.Underused, false, lruEvictionOperationContext) as ArrayList;
                    //cache remove counter increment
                    //context.PerfStatsColl.IncrementEvictPerSecStatsBy(keysTobeRemoved.Count);
                    if (_statsCollector != null)
                    {
                        _statsCollector.IncrementStatsValue(StatisticsType.CacheEvictionPerSec);
                    }
                }
                catch (Exception ex)
                {
                    if (LoggerManager.Instance.StorageLogger != null && LoggerManager.Instance.StorageLogger.IsErrorEnabled)
                    {
                        LoggerManager.Instance.StorageLogger.Error("LruEvictionPolicy.Execute", "an error occurred while removing items. Error " + ex.ToString());
                    }
                }
            }

            //if (keysTobeRemoved.Count > 0)
            //{
            //    try
            //    {
            //        cache.CacheRemove(key);
            //        //removedItems = cache.RemoveSync(keysTobeRemoved.ToArray(), ItemRemoveReason.Underused, false, lruEvictionOperationContext) as ArrayList;
            //        //update perfmon stats
            //        //context.PerfStatsColl.IncrementEvictPerSecStatsBy(keysTobeRemoved.Count);
            //    }
            //    catch (Exception ex)
            //    {
            //        cacheLog.Error("LruEvictionPolicy.Execute", "an error occurred while removing items. Error " + ex.ToString());
            //    }
            //}
            return(true);
        }
Пример #3
0
 public static byte[] GetFromChache(Document document, string FileFormat, string XmlLabel)
 {
     try
     {
         logger.DebugFormat("GetFromChache {0}, {1}, {2}", document.IdDocument, FileFormat, XmlLabel);
         if (document.Cache == null)
         {
             //Load chache
             document.Cache = DbProvider.GetDocumentCache(document.IdDocument, System.Environment.MachineName);
         }
         DocumentCache cache = null;
         if (document.Cache != null && (cache = document.Cache.Where(x => x.FileName.EndsWith((FileFormat.IndexOf('.') > 0 ? Path.GetExtension(FileFormat) : FileFormat)) && ((string.IsNullOrEmpty(XmlLabel) && string.IsNullOrEmpty(x.Signature)) || x.Signature == XmlLabel)).FirstOrDefault()) != null)
         {
             logger.DebugFormat("GetFromChache Get Document {0} {1}", document.IdDocument, cache.FileName);
             if (File.Exists(cache.FileName))
             {
                 return(File.ReadAllBytes(cache.FileName));
             }
             else
             {
                 DeleteCache(cache, document);
             }
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex);
     }
     return(null);//No document in chache
 }
Пример #4
0
        protected virtual async Task <DocumentWithDetailsDto> GetDocumentWithDetailsDto(
            Project project,
            string documentName,
            string version)
        {
            var cacheKey = $"Document@{project.ShortName}#{documentName}#{version}";

            return(await DocumentCache.GetOrAddAsync(
                       cacheKey,
                       async() =>
            {
                Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store...");
                var store = _documentStoreFactory.Create(project.DocumentStoreType);
                var document = await store.GetDocumentAsync(project, documentName, version);
                Logger.LogInformation($"Document retrieved: {documentName}");
                return CreateDocumentWithDetailsDto(project, document);
            },
                       () => new DistributedCacheEntryOptions
            {
                //TODO: Configurable?
                AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2),
                SlidingExpiration = TimeSpan.FromMinutes(30)
            }
                       ));
        }
Пример #5
0
        private XElement GetMethodDocument()
        {
            switch (document.State)
            {
            case DocumentCache.CacheState.NotInitialized:
            {
                var doc = GetAssemblyDocument(Method.DeclaringType.Assembly);
                if (doc == null)
                {
                    document = new DocumentCache(DocumentCache.CacheState.NotFound, null);
                }
                var elt = doc
                          .Descendants("member")
                          .Where(m => m.Attribute("name")?.Value == GetMethodSignatureForDoc(Method))
                          .FirstOrDefault();
                if (elt != null)
                {
                    document = new DocumentCache(DocumentCache.CacheState.Found, elt);
                }
                else
                {
                    document = new DocumentCache(DocumentCache.CacheState.NotFound, null);
                }
                return(document.Document);
            }

            case DocumentCache.CacheState.Found:
                return(document.Document);

            case DocumentCache.CacheState.NotFound:
            default:
                return(null);
            }
        }
Пример #6
0
 protected internal ArangoDatabase(Communication
                                   <Response, ConnectionSync
                                   > communication, VPack vpacker, VPack
                                   vpackerNull, VPackParser vpackParser, DocumentCache
                                   documentCache, CollectionCache collectionCache, string name
                                   )
     : base(new ArangoExecutorSync(communication, vpacker, vpackerNull
                                   , vpackParser, documentCache, collectionCache), name)
 {
 }
        public void Parse_Kitchen_Sink_Query_With_Cache()
        {
            // arrange
            var request = new GraphQLRequestDto
            {
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            };

            byte[] buffer       = Encoding.UTF8.GetBytes(request.Query);
            string expectedHash = Convert.ToBase64String(
                SHA1.Create().ComputeHash(buffer));

            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(request
                                            ).NormalizeLineBreaks());

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> first = requestParser.Parse();

            cache.TryAddDocument(first[0].QueryName, first[0].Query);

            // act
            requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> second = requestParser.Parse();

            // assert
            Assert.Equal(first[0].Query, second[0].Query);
            Assert.Collection(second,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal(expectedHash, r.QueryName);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
Пример #8
0
        internal IList GetSelectedKeys(DocumentCache cache, long evictSize)
        {
            EvictionIndexEntry entry        = null;
            ClusteredArrayList selectedKeys = new ClusteredArrayList();
            long totalSize          = 0;
            bool selectionCompleted = false;
            long index = _head;

            if (_head != -1)
            {
                do
                {
                    entry = _index[index] as EvictionIndexEntry;

                    if (entry != null)
                    {
                        IList keys = entry.GetAllKeys();
                        foreach (long key in keys)
                        {
                            CacheItem citem = cache.CacheGetWithoutNotify(key);

                            if (citem == null || citem.Flag.IsBitSet(BitsetConstants.DocumentDirty) || citem.Flag.IsBitSet(BitsetConstants.MetaDataDirty))
                            {
                                //if any of the constants is set dont evict this item.
                                continue;
                            }

                            long itemSize = citem.Size;
                            if (totalSize + itemSize >= evictSize && totalSize > 0)
                            {
                                if (evictSize - totalSize > (itemSize + totalSize) - evictSize)
                                {
                                    selectedKeys.Add(key);
                                }

                                selectionCompleted = true;
                                break;
                            }
                            else
                            {
                                selectedKeys.Add(key);
                                totalSize += itemSize;
                                //prvsSize = itemSize;
                            }
                        }
                    }
                    index = entry.Next;
                }while (!selectionCompleted && index != -1);
            }
            return(selectedKeys);
        }
Пример #9
0
 public static void DeleteCache(DocumentCache cache, Document document)
 {
     try
     {
         logger.DebugFormat("DeleteCache {0} {1}", document.IdDocument, cache.FileName);
         DbProvider.DeleteCache(cache, document.IdDocument);
         if (File.Exists(cache.FileName))
         {
             File.Delete(cache.FileName);
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex);
     }
 }
Пример #10
0
        public void Parse_Kitchen_Sink_Query_With_Cache()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(
                    new GraphQLRequestDto
            {
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            }).NormalizeLineBreaks());

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> first = requestParser.Parse();

            cache.Add(first[0].QueryName, first[0].Query);

            // act
            requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> second = requestParser.Parse();

            // assert
            Assert.Equal(first[0].Query, second[0].Query);
            Assert.Collection(second,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal("KwPz8bJWrVDRrtFPjW2sh5CUQwE=", r.QueryName);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
        public void Parse_Id_As_Name()
        {
            // arrange
            var request = new RelayGraphQLRequestDto
            {
                Id    = "FooBar",
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            };

            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(request
                                            ).NormalizeLineBreaks());

            byte[] buffer       = Encoding.UTF8.GetBytes(request.Query);
            string expectedHash = Convert.ToBase64String(
                SHA1.Create().ComputeHash(buffer));

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            // act
            IReadOnlyList <GraphQLRequest> result = requestParser.Parse();

            // assert
            Assert.Collection(result,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal("FooBar", r.QueryName);
                Assert.Equal(expectedHash, r.QueryHash);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
Пример #12
0
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();

            var cacheManager = new CacheManager();
            var documentsDirectory = Server.MapPath("~/Documents");
            var templatesDirectory = Server.MapPath("~/Templates");

            if (!Directory.Exists(documentsDirectory))
            {
                Directory.CreateDirectory(documentsDirectory);
            }

            if (!Directory.Exists(templatesDirectory))
            {
                Directory.CreateDirectory(templatesDirectory);
            }

            pdfDocumentCache = cacheManager.CreateCache(documentsDirectory, templatesDirectory);
        }
Пример #13
0
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();

            var cacheManager       = new CacheManager();
            var documentsDirectory = Server.MapPath("~/Documents");
            var templatesDirectory = Server.MapPath("~/Templates");

            if (!Directory.Exists(documentsDirectory))
            {
                Directory.CreateDirectory(documentsDirectory);
            }

            if (!Directory.Exists(templatesDirectory))
            {
                Directory.CreateDirectory(templatesDirectory);
            }

            pdfDocumentCache = cacheManager.CreateCache(documentsDirectory, templatesDirectory);
        }
Пример #14
0
        protected virtual async Task <DocumentWithDetailsDto> GetDocumentWithDetailsDtoAsync(
            Project project,
            string documentName,
            string languageCode,
            string version)
        {
            var cacheKey = $"Document@{project.ShortName}#{languageCode}#{documentName}#{version}";

            async Task <DocumentWithDetailsDto> GetDocumentAsync()
            {
                Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store...");
                var store     = _documentStoreFactory.Create(project.DocumentStoreType);
                var languages = await GetLanguageListAsync(store, project, version);

                var language = GetLanguageByCode(languages, languageCode);
                var document = await store.GetDocumentAsync(project, documentName, language.Code, version);

                Logger.LogInformation($"Document retrieved: {documentName}");
                return(CreateDocumentWithDetailsDto(project, document, languages, language.Code));
            }

            if (Debugger.IsAttached)
            {
                return(await GetDocumentAsync());
            }

            return(await DocumentCache.GetOrAddAsync(
                       cacheKey,
                       GetDocumentAsync,
                       () => new DistributedCacheEntryOptions
            {
                //TODO: Configurable?
                AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2),
                SlidingExpiration = TimeSpan.FromMinutes(30)
            }
                       ));
        }
        public void Parse_Id_As_Name()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(
                    new RelayGraphQLRequestDto
            {
                Id    = "FooBar",
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            }).NormalizeLineBreaks());

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            // act
            IReadOnlyList <GraphQLRequest> result = requestParser.Parse();

            // assert
            Assert.Collection(result,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal("FooBar", r.QueryName);
                Assert.Equal("KwPz8bJWrVDRrtFPjW2sh5CUQwE=", r.QueryHash);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
Пример #16
0
        protected virtual async Task <DocumentWithDetailsDto> GetDocumentWithDetailsDtoAsync(
            Project project,
            string documentName,
            string languageCode,
            string version)
        {
            version = string.IsNullOrWhiteSpace(version) ? project.LatestVersionBranchName : version;

            var cacheKey = $"Document@{project.ShortName}#{languageCode}#{documentName}#{version}";

            async Task <DocumentWithDetailsDto> GetDocumentAsync()
            {
                Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store...");
                var store    = _documentStoreFactory.Create(project.DocumentStoreType);
                var document = await store.GetDocumentAsync(project, documentName, languageCode, version);

                Logger.LogInformation($"Document retrieved: {documentName}");
                return(CreateDocumentWithDetailsDto(project, document));
            }

            if (HostEnvironment.IsDevelopment())
            {
                return(await GetDocumentAsync());
            }

            return(await DocumentCache.GetOrAddAsync(
                       cacheKey,
                       GetDocumentAsync,
                       () => new DistributedCacheEntryOptions
            {
                //TODO: Configurable?
                AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2),
                SlidingExpiration = TimeSpan.FromMinutes(30)
            }
                       ));
        }
 private void TheCacheShouldBeEmpty(DocumentCache cache)
 {
     cache.Documents.Should().BeEmpty();
 }
 private DocumentCache TheCacheGetsCleared(DocumentCache cache)
 {
     cache.Clear();
     return(cache);
 }
 private DocumentCache ADocumentIsLoaded(DocumentCache cache)
 {
     cache.AddDocument("Sample");
     return(cache);
 }
Пример #20
0
 public void UseCacheProvider(DocumentCache documentCache)
 {
     CacheInstance = documentCache;
 }