Пример #1
0
        public ActionResult ShowPlan(string sitename, int revisionId)
        {
            Query query = QueryUtil.GetQueryForRevision(revisionId);

            if (query == null)
            {
                return(PageNotFound());
            }

            var parsedQuery = new ParsedQuery(query.QueryBody, Request.Params);

            if (!parsedQuery.IsExecutionReady)
            {
                return(PageBadRequest());
            }

            CachedResult cache = QueryUtil.GetCachedResults(
                parsedQuery,
                Site.Id
                );

            if (cache == null || cache.ExecutionPlan == null)
            {
                return(PageNotFound());
            }

            return(new QueryPlanResult(cache.ExecutionPlan));
        }
Пример #2
0
        public void CacheRssResults(IIndexer indexer, IEnumerable <ReleaseInfo> releases)
        {
            lock (cache)
            {
                var trackerCache = cache.Where(c => c.TrackerId == indexer.ID).FirstOrDefault();
                if (trackerCache == null)
                {
                    trackerCache             = new TrackerCache();
                    trackerCache.TrackerId   = indexer.ID;
                    trackerCache.TrackerName = indexer.DisplayName;
                    cache.Add(trackerCache);
                }

                foreach (var release in releases.OrderByDescending(i => i.PublishDate))
                {
                    var existingItem = trackerCache.Results.Where(i => i.Result.Guid == release.Guid).FirstOrDefault();
                    if (existingItem == null)
                    {
                        existingItem         = new CachedResult();
                        existingItem.Created = DateTime.Now;
                        trackerCache.Results.Add(existingItem);
                    }

                    existingItem.Result = release;
                }

                // Prune cache
                foreach (var tracker in cache)
                {
                    tracker.Results = tracker.Results.OrderByDescending(i => i.Created).Take(MAX_RESULTS_PER_TRACKER).ToList();
                }
            }
        }
Пример #3
0
        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {
#if Webhosted
            Cache cache = GetCache();
#else
            ObjectCache cache = this.GetCache();
#endif
            string cacheKey = this.CreateCacheKey(inputs);
            CachedResult cacheItem = cache[cacheKey] as CachedResult;
            if (cacheItem != null)
            {
                outputs = cacheItem.Outputs;
                return cacheItem.ReturnValue;
            }
            else
            {
                object result = this.originalInvoker.Invoke(instance, inputs, out outputs);
                cacheItem = new CachedResult { ReturnValue = result, Outputs = outputs };
#if Webhosted
                cache.Insert(cacheKey, cacheItem, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(this.cacheDuration));
#else
                cache.Add(cacheKey, cacheItem, DateTimeOffset.UtcNow.Add(TimeSpan.FromSeconds(this.cacheDuration)));
#endif
                return result;
            }
        }
Пример #4
0
            private bool IsInRole(CachedResult cachedResult, WindowsBuiltInRole role, DateTime SystemTimeUtcNow,
                                  Action <string> logDebug, bool isOAuthNull, bool isGlobalAdmin, Action <string, Exception> logWarnException)
            {
                try
                {
                    var authorizationGroups = cachedResult.AuthorizationGroups.Value;

                    switch (role)
                    {
                    case WindowsBuiltInRole.Administrator:
                        return(IsAdministratorNoCache(authorizationGroups));

                    case WindowsBuiltInRole.BackupOperator:
                        return(IsBackupOperatorNoCache(authorizationGroups));

                    default:
                        throw new NotSupportedException(role.ToString());
                    }
                }
                catch (Exception e)
                {
                    logWarnException("Could not determine whatever user is admin or not, assuming not", e);
                    return(false);
                }
            }
        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
#if Webhosted
            Cache cache = GetCache();
#else
            ObjectCache cache = this.GetCache();
#endif
            string           cacheKey         = this.CreateCacheKey(inputs);
            CachedResult     cacheItem        = cache[cacheKey] as CachedResult;
            CachingUserState cachingUserState = new CachingUserState
            {
                CacheItem            = cacheItem,
                CacheKey             = cacheKey,
                OriginalUserCallback = callback,
                OriginalUserState    = state
            };

            IAsyncResult originalAsyncResult;
            if (cacheItem != null)
            {
                InvokerDelegate invoker = cacheItem.GetValue;
                object[]        dummy;
                originalAsyncResult = invoker.BeginInvoke(inputs, out dummy, this.InvokerCallback, cachingUserState);
            }
            else
            {
                originalAsyncResult = this.originalInvoker.InvokeBegin(instance, inputs, this.InvokerCallback, cachingUserState);
            }

            return(new CachingAsyncResult(originalAsyncResult, cachingUserState));
        }
Пример #6
0
        public virtual List <T> GetList <T>(string key)
        {
            List <T> items = new List <T>();

            ReadLock(delegate
            {
                CachedResult result = Get(key);
                if (result == null)
                {
                    return;
                }

                if (result.Value is T)
                {
                    items.Add((T)result.Value);
                    return;
                }

                foreach (string itemKey in (IEnumerable <string>)result.Value)
                {
                    CachedResult itemResult = Get(itemKey);
                    if (itemResult != null)
                    {
                        items.Add((T)itemResult.Value);
                    }
                }
                return;
            });
            return(items);
        }
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse     response    = base.GetWebResponse(request);
            HttpWebResponse webResponse = response as HttpWebResponse;

            if (webResponse != null)
            {
                // Check to see if it's a redirect
                if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399)
                {
                    string            uriString  = webResponse.Headers["Location"];
                    NetworkCredential credential = request.Credentials.GetCredential(request.RequestUri, "Basic");
                    returnValue = GetCachedResult(credential.UserName, request.RequestUri.ToString());
                    if ((returnValue == null) || (!returnValue.Url.Equals(uriString)))
                    {
                        if (returnValue != null)
                        {
                            ClearCacheResult(credential.UserName, request.RequestUri.ToString());
                            returnValue = null;
                        }

                        CookieAwareWebClient webClient = GetWebClient();

                        string cacheableDataValue = webClient.DownloadString(uriString);
                        returnValue = CacheResult(credential.UserName, request.RequestUri.ToString(), uriString, serializer.Deserialize(cacheableDataValue));
                    }
                    else
                    {
                        isCachedResult = true;
                    }
                }
            }

            return(response);
        }
        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {
#if Webhosted
            Cache cache = GetCache();
#else
            ObjectCache cache = this.GetCache();
#endif
            string       cacheKey  = this.CreateCacheKey(inputs);
            CachedResult cacheItem = cache[cacheKey] as CachedResult;
            if (cacheItem != null)
            {
                outputs = cacheItem.Outputs;
                return(cacheItem.ReturnValue);
            }
            else
            {
                object result = this.originalInvoker.Invoke(instance, inputs, out outputs);
                cacheItem = new CachedResult {
                    ReturnValue = result, Outputs = outputs
                };
#if Webhosted
                cache.Insert(cacheKey, cacheItem, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(this.cacheDuration));
#else
                cache.Add(cacheKey, cacheItem, DateTimeOffset.UtcNow.Add(TimeSpan.FromSeconds(this.cacheDuration)));
#endif
                return(result);
            }
        }
Пример #9
0
            public bool IsAdministrator(WindowsIdentity windowsIdentity)
            {
                CachedResult value;

                if (cache.TryGetValue(windowsIdentity.User, out value) && (SystemTime.UtcNow - value.Timestamp) <= maxDuration)
                {
                    Interlocked.Increment(ref value.Usage);
                    return(value.Value.Value);
                }

                var cachedResult = new CachedResult
                {
                    Usage = value == null ? 1 : value.Usage + 1,
                    Value = new Lazy <bool>(() =>
                    {
                        try
                        {
                            return(IsAdministratorNoCache(windowsIdentity.Name));
                        }
                        catch (Exception e)
                        {
                            log.WarnException("Could not determine whatever user is admin or not, assuming not", e);
                            return(false);
                        }
                    }),
                    Timestamp = SystemTime.UtcNow
                };

                cache.AddOrUpdate(windowsIdentity.User, cachedResult, (_, __) => cachedResult);
                if (cache.Count > CacheMaxSize)
                {
                    foreach (var source in cache
                             .Where(x => (SystemTime.UtcNow - x.Value.Timestamp) > maxDuration))
                    {
                        CachedResult ignored;
                        cache.TryRemove(source.Key, out ignored);
                        log.Debug("Removing expired {0} from cache", source.Key);
                    }
                    if (cache.Count > CacheMaxSize)
                    {
                        foreach (var source in cache
                                 .OrderByDescending(x => x.Value.Usage)
                                 .ThenBy(x => x.Value.Timestamp)
                                 .Skip(CacheMaxSize))
                        {
                            if (source.Key == windowsIdentity.User)
                            {
                                continue;                                 // we don't want to remove the one we just added
                            }
                            CachedResult ignored;
                            cache.TryRemove(source.Key, out ignored);
                            log.Debug("Removing least used {0} from cache", source.Key);
                        }
                    }
                }

                return(cachedResult.Value.Value);
            }
Пример #10
0
        private State StoreResult(State oldState, object[] arglist, CachedResult result)
        {
            if ((Flags & TaskFlags.ReadCache) == 0)
            {
                throw new InvalidOperationException("Attempt to store result to a task without caching enabled.");
            }

            return(Cache.SetItem(oldState, arglist, result));
        }
Пример #11
0
 private static void SetCachedResult(SqlCommand command, object value)
 {
     if (command.CommandType == CommandType.StoredProcedure && StoredProceduresToCache.ContainsKey(command.CommandText))
     {
         _cachedStoredProcedureResults[command] = new CachedResult
         {
             TimeAdded = DateTime.Now,
             Value     = value
         };
     }
 }
Пример #12
0
            public bool IsInRole(WindowsIdentity windowsIdentity, WindowsBuiltInRole role)
            {
                CachedResult value;

                if (cache.TryGetValue(windowsIdentity.User, out value) && (SystemTime.UtcNow - value.Timestamp) <= maxDuration)
                {
                    Interlocked.Increment(ref value.Usage);
                    return(IsInRole(value, role));
                }

                var cachedResult = new CachedResult
                {
                    Usage = value == null ? 1 : value.Usage + 1,
                    AuthorizationGroups = new Lazy <IList <Principal> >(() => GetUserAuthorizationGroups(windowsIdentity.Name)),
                    Timestamp           = SystemTime.UtcNow
                };

                cache.AddOrUpdate(windowsIdentity.User, cachedResult, (_, __) => cachedResult);
                if (cache.Count > CacheMaxSize)
                {
                    foreach (var source in cache
                             .Where(x => (SystemTime.UtcNow - x.Value.Timestamp) > maxDuration))
                    {
                        CachedResult ignored;
                        cache.TryRemove(source.Key, out ignored);
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Removing expired {0} from cache", source.Key);
                        }
                    }
                    if (cache.Count > CacheMaxSize)
                    {
                        foreach (var source in cache
                                 .OrderByDescending(x => x.Value.Usage)
                                 .ThenBy(x => x.Value.Timestamp)
                                 .Skip(CacheMaxSize))
                        {
                            if (source.Key == windowsIdentity.User)
                            {
                                continue; // we don't want to remove the one we just added
                            }
                            CachedResult ignored;
                            cache.TryRemove(source.Key, out ignored);
                            if (log.IsDebugEnabled)
                            {
                                log.Debug("Removing least used {0} from cache", source.Key);
                            }
                        }
                    }
                }

                return(IsInRole(cachedResult, role));
            }
Пример #13
0
        public Task <DailyTrendDataViewModel> GetTrendDataForTermAsync(string searchTerm)
        {
            if (string.IsNullOrWhiteSpace(searchTerm))
            {
                throw new ArgumentException("Search term cannot be null or empty", nameof(searchTerm));
            }

            var countsByDay = postCountsCache.Get();

            if (!resultsCache.TryGet(searchTerm, out var cached))
            {
                var searchResults = indexManager.Search(searchTerm);

                var counts   = new List <ushort>();
                var maxCount = 0;

                for (var i = 0; i < countsByDay.Days.Count; i++)
                {
                    var date = countsByDay.Days[i];

                    var onDate = searchResults.Count(x => x.Date.Date == date.Date);

                    counts.Add((ushort)onDate);

                    if (onDate > maxCount)
                    {
                        maxCount = onDate;
                    }
                }

                cached = new CachedResult
                {
                    Counts   = counts,
                    MaxCount = maxCount
                };

                if (searchResults.Count > 1000)
                {
                    resultsCache.Cache(searchTerm, cached);
                }
            }

            var result = new DailyTrendDataViewModel
            {
                Counts      = cached.Counts,
                Start       = countsByDay.Min,
                End         = countsByDay.Max,
                CountMax    = cached.MaxCount,
                DailyTotals = countsByDay.PostsPerDay
            };

            return(Task.FromResult(result));
        }
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse     response    = base.GetWebResponse(request);
            HttpWebResponse webResponse = response as HttpWebResponse;

            if (webResponse != null)
            {
                // Check to see if it's a redirect
                if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399)
                {
                    string uriString  = webResponse.Headers["Location"];
                    string cachingKey = String.Empty;
                    if (request.Credentials != null)
                    {
                        NetworkCredential credential = request.Credentials.GetCredential(request.RequestUri, "Basic");
                        cachingKey = credential.UserName;
                    }
                    else
                    {
                        CookieCollection cookieCollection = Cookies.GetCookies(request.RequestUri);
                        foreach (Cookie currentCookie in cookieCollection)
                        {
                            if (currentCookie.Name.Equals(RallyRestApi.ZSessionID, StringComparison.InvariantCultureIgnoreCase))
                            {
                                cachingKey = currentCookie.Value;
                                break;
                            }
                        }
                    }

                    returnValue = GetCachedResult(cachingKey, request.RequestUri.ToString());
                    if ((returnValue == null) || (!returnValue.Url.Equals(uriString)))
                    {
                        if (returnValue != null)
                        {
                            ClearCacheResult(cachingKey, request.RequestUri.ToString());
                            returnValue = null;
                        }

                        CookieAwareWebClient webClient = GetWebClient();

                        string cacheableDataValue = webClient.DownloadString(uriString);
                        returnValue = CacheResult(cachingKey, request.RequestUri.ToString(), uriString, serializer.Deserialize(cacheableDataValue));
                    }
                    else
                    {
                        isCachedResult = true;
                    }
                }
            }

            return(response);
        }
        public QueryResults WithCache(CachedResult cache)
        {
            if (cache == null || cache.Results == null)
            {
                return(this);
            }

            this.Messages      = cache.Messages;
            this.ResultSets    = JsonConvert.DeserializeObject <List <ResultSet> >(cache.Results);
            this.ExecutionPlan = cache.ExecutionPlan;

            return(this);
        }
Пример #16
0
            public bool IsInRole(WindowsIdentity windowsIdentity, WindowsBuiltInRole role, DateTime SystemTimeUtcNow,
                                 Action <string> logDebug, bool isOAuthNull, bool isGlobalAdmin, Action <string, Exception> logWarnException)
            {
                CachedResult value;

                if (cache.TryGetValue(windowsIdentity.User, out value) && (SystemTimeUtcNow - value.Timestamp) <= maxDuration)
                {
                    Interlocked.Increment(ref value.Usage);
                    return(IsInRole(value, role, SystemTimeUtcNow, logDebug, isOAuthNull, isGlobalAdmin, logWarnException));
                }

                var cachedResult = new CachedResult
                {
                    Usage = value == null ? 1 : value.Usage + 1,
                    AuthorizationGroups = new Lazy <IList <Principal> >(() => GetUserAuthorizationGroups(windowsIdentity.Name)),
                    Timestamp           = SystemTimeUtcNow
                };

                cache.AddOrUpdate(windowsIdentity.User, cachedResult, (_, __) => cachedResult);
                if (cache.Count > CacheMaxSize)
                {
                    foreach (var source in cache
                             .Where(x => (SystemTimeUtcNow - x.Value.Timestamp) > maxDuration))
                    {
                        CachedResult ignored;
                        cache.TryRemove(source.Key, out ignored);
                        string exceptionString = String.Format("Removing expired {0} from cache", source.Key);
                        logDebug(exceptionString);
                    }
                    if (cache.Count > CacheMaxSize)
                    {
                        foreach (var source in cache
                                 .OrderByDescending(x => x.Value.Usage)
                                 .ThenBy(x => x.Value.Timestamp)
                                 .Skip(CacheMaxSize))
                        {
                            if (source.Key == windowsIdentity.User)
                            {
                                continue; // we don't want to remove the one we just added
                            }
                            CachedResult ignored;
                            cache.TryRemove(source.Key, out ignored);
                            string exceptionString = String.Format("Removing expired {0} from cache", source.Key);
                            logDebug(exceptionString);
                        }
                    }
                }

                return(IsInRole(cachedResult, role, SystemTimeUtcNow,
                                logDebug, isOAuthNull, isGlobalAdmin, logWarnException));
            }
Пример #17
0
        public Engine CheckoutScript(Func <ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request)
        {
            CachedResult value;

            if (cacheDic.TryGetValue(request, out value))
            {
                Interlocked.Increment(ref value.Usage);
                Engine context;
                if (value.Queue.TryDequeue(out context))
                {
                    return(context);
                }
            }
            var result = createEngine(request);

            var cachedResult = new CachedResult
            {
                Usage     = 1,
                Queue     = new ConcurrentQueue <Engine>(),
                Timestamp = SystemTime.UtcNow
            };

            cacheDic.AddOrUpdate(request, cachedResult, (_, existing) =>
            {
                Interlocked.Increment(ref existing.Usage);
                return(existing);
            });
            if (cacheDic.Count > CacheMaxSize)
            {
                foreach (var source in cacheDic
                         .Where(x => x.Value != null)
                         .OrderByDescending(x => x.Value.Usage)
                         .ThenBy(x => x.Value.Timestamp)
                         .Skip(CacheMaxSize - CacheMaxSize / 10))
                {
                    if (Equals(source.Key, request))
                    {
                        continue;                         // we don't want to remove the one we just added
                    }
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
                foreach (var source in cacheDic.Where(x => x.Value == null))
                {
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
            }

            return(result);
        }
Пример #18
0
        public virtual bool IsValidTfsServerUrl(string url)
        {
            string       cacheKey = "IsValidTfsServerUrl_" + url;
            CachedResult result   = cache.Get(cacheKey);

            if (result != null)
            {
                return((bool)result.Value);
            }
            bool validUrl = Helper.IsValidTFSUrl(url, Proxy.DefaultProxy);

            cache.Set(cacheKey, validUrl);
            return(validUrl);
        }
Пример #19
0
        public CachedResult <T> GetCachedValue <T>(string prefix, string subKey, Func <ApiResult <T> > loader, TimeSpan expire, string cacheName = "Couch", bool useLock = false)
        {
            var result = CachedResult <T> .MakeSucessResult();

            var key = prefix + subKey;
            T   data;

            var callStep = new CallStep();

            var methodName = _loaderRegex.Replace(loader.Method.Name.Replace("<", ""), "");

            callStep.Start(methodName);


            var cache = CacheInstance.Collection[cacheName];

            if (!cache.TryGet(key, out data))
            {
                if (useLock && _LockObjects.ContainsKey(prefix))
                {
                    Monitor.Enter(_LockObjects[prefix]);
                }

                if (!useLock || !cache.TryGet(key, out data))
                {
                    var loadResult = loader();

                    if (loadResult.Success)
                    {
                        data = loadResult.Data;
                        cache.Add(key, data, expire);
                    }

                    result.HitCache = false;
                    result.Copy(loadResult);
                }

                if (useLock && _LockObjects.ContainsKey(prefix))
                {
                    Monitor.Exit(_LockObjects[prefix]);
                }
            }

            result.Data = data;

            callStep.Stop(result, result.HitCache);

            return(result);
        }
Пример #20
0
		public Engine CheckoutScript(Func<ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request)
		{
			CachedResult value;
			if (cacheDic.TryGetValue(request, out value))
			{
				Interlocked.Increment(ref value.Usage);
				Engine context;
				if (value.Queue.TryDequeue(out context))
				{
					return context;
				}
			}
			var result = createEngine(request);

			var cachedResult = new CachedResult
			{
				Usage = 1,
				Queue = new ConcurrentQueue<Engine>(),
				Timestamp = SystemTime.UtcNow
			};

			cacheDic.AddOrUpdate(request, cachedResult, (_, existing) =>
			{
				Interlocked.Increment(ref existing.Usage);
				return existing;
			});
			if (cacheDic.Count > CacheMaxSize)
			{
				foreach (var source in cacheDic
					.Where(x=>x.Value !=null)
					.OrderByDescending(x => x.Value.Usage)
					.ThenBy(x => x.Value.Timestamp)
					.Skip(CacheMaxSize - CacheMaxSize/10))
				{
					if (Equals(source.Key, request))
						continue; // we don't want to remove the one we just added
					CachedResult ignored;
					cacheDic.TryRemove(source.Key, out ignored);
				}
				foreach (var source in cacheDic.Where(x => x.Value == null))
				{
					CachedResult ignored;
					cacheDic.TryRemove(source.Key, out ignored);
				}
			}

			return result;
		}
 public object Invoke(object instance, object[] inputs, out object[] outputs)
 {
     string cacheKey = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString();
     CachedResult cacheResult = this.datacache.Get(cacheKey) as CachedResult;
     if (cacheResult != null)
     {
         outputs = cacheResult.Outputs;
         return cacheResult.ReturnValue;
     }
     else
     {
         object result = this.operationInvoker.Invoke(instance, inputs, out outputs);
         cacheResult = new CachedResult { ReturnValue = result, Outputs = outputs };
         datacache.Put(cacheKey, cacheResult);
         return result;
     }
 }
Пример #22
0
 public void Dispose()
 {
     SearchKey = null;
     if (IsBufferMode)
     {
         CachedResult.Dispose();
     }
     else
     {
         DBConnection.Dispose();
         DataReader.Dispose();
         if (DataReader2 != null)
         {
             DataReader2.Dispose();
         }
     }
 }
Пример #23
0
        public Engine GetEngine(Func <PatchRequest, Engine> createEngine, PatchRequest request, string customFunctions)
        {
            CachedResult value;
            var          patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);

            if (_cache.TryGetValue(patchRequestAndCustomFunctionsTuple, out value))
            {
                value.Usage++;
                return(value.Engine);
            }
            var result = createEngine(request);

            if (string.IsNullOrWhiteSpace(customFunctions) == false)
            {
                result.Execute(string.Format(@"var customFunctions = function() {{  var exports = {{ }}; {0};
                            return exports;
                        }}();
                        for(var customFunction in customFunctions) {{
                            this[customFunction] = customFunctions[customFunction];
                        }};", customFunctions), new ParserOptions {
                    Source = "customFunctions.js"
                });
            }
            var cachedResult = new CachedResult
            {
                Usage     = 1,
                Timestamp = SystemTime.UtcNow,
                Engine    = result
            };

            if (_cache.Count > CacheMaxSize)
            {
                foreach (var item in _cache.OrderBy(x => x.Value?.Usage)
                         .ThenByDescending(x => x.Value?.Timestamp)
                         .Take(CacheMaxSize / 10)
                         .Select(source => source.Key)
                         .ToList())
                {
                    _cache.Remove(item);
                }
            }
            _cache[patchRequestAndCustomFunctionsTuple] = cachedResult;


            return(result);
        }
        public ActionResult ShowSingleSiteCsv(string sitename, int revisionId, string slug)
        {
            Query query = QueryUtil.GetQueryForRevision(revisionId);

            if (query == null)
            {
                return(PageNotFound());
            }

            Site site;

            if (!TryGetSite(sitename, out site))
            {
                return(site == null ?  (ActionResult)PageNotFound() : RedirectPermanent(string.Format("/{0}/csv/{1}{2}{3}",
                                                                                                      site.TinyName.ToLower(), revisionId, slug.HasValue() ? "/" + slug : "", Request.Url.Query
                                                                                                      )));
            }

            var parsedQuery = new ParsedQuery(query.QueryBody, Request.Params);

            if (!parsedQuery.IsExecutionReady)
            {
                return(PageBadRequest());
            }

            CachedResult cachedResults = QueryUtil.GetCachedResults(
                parsedQuery,
                Site.Id
                );
            List <ResultSet> resultSets;

            if (cachedResults != null)
            {
                resultSets = JsonConvert.DeserializeObject <List <ResultSet> >(cachedResults.Results, QueryResults.GetSettings());
            }
            else
            {
                resultSets = QueryRunner.GetResults(
                    parsedQuery,
                    site,
                    CurrentUser
                    ).ResultSets;
            }

            return(new CsvResult(resultSets));
        }
        /// <summary>
        /// Serializes data to be sent across the wire.
        /// <para>Serialization errors of lists may be caused by a missing constructor with the following signature:
        /// public [CLASS NAME](SerializationInfo info, StreamingContext context)
        /// </para>
        /// </summary>
        /// <param name="obj">The object to serialize.</param>
        /// <returns>The serialized data.</returns>
        private static byte[] SerializeData(CachedResult obj)
        {
            if (obj == null)
            {
                return(null);
            }

            BinaryFormatter binFor = new BinaryFormatter();

            byte[] data = null;
            using (MemoryStream stream = new MemoryStream())
            {
                binFor.Serialize(stream, obj);
                data = stream.ToArray();
            }

            return(data);
        }
Пример #26
0
        public static Result Query(string query, string revFirst, string revLast, bool useCache)
        {
            CleanupCache();
            CachedResult result = null;
            string       key    = query + "|" + revFirst + "|" + revLast;

            if (useCache)
            {
                lock (Cache) Cache.TryGetValue(key, out result);
            }
            if (result == null)
            {
                result = new CachedResult(Index.Query(query, revFirst, revLast, new LineHighlight(SvnApi)));
                lock (Cache) Cache[key] = result;
            }
            result.LastAccess = DateTime.UtcNow;
            return(result.Result);
        }
Пример #27
0
        public ActionResult ShowSingleSiteCsv(string sitename, int revisionId)
        {
            Query query = QueryUtil.GetQueryForRevision(revisionId);

            if (query == null)
            {
                return(PageNotFound());
            }

            var site = GetSite(sitename);

            if (sitename == null)
            {
                return(PageNotFound());
            }

            var parsedQuery = new ParsedQuery(query.QueryBody, Request.Params);

            if (!parsedQuery.IsExecutionReady)
            {
                return(PageBadRequest());
            }

            CachedResult cachedResults = QueryUtil.GetCachedResults(
                parsedQuery,
                Site.Id
                );
            List <ResultSet> resultSets;

            if (cachedResults != null)
            {
                resultSets = JsonConvert.DeserializeObject <List <ResultSet> >(cachedResults.Results);
            }
            else
            {
                resultSets = QueryRunner.GetResults(
                    parsedQuery,
                    site,
                    CurrentUser
                    ).ResultSets;
            }

            return(new CsvResult(resultSets));
        }
Пример #28
0
        //Set does and insert or update, as necessary
        public void Set(string cacheKey, T val, CachePolicy cachePolicy = null)
        {
            Ensure.That(cachePolicy).IsNull();
            CachedResult value;

            if (cache.TryGetValue(cacheKey, out value) && (DateTime.UtcNow - value.Timestamp) <= this.maxDuration)
            {
                //cache.TryUpdate()
            }
            var cachedResult = new CachedResult
            {
                Usage     = value == null ? 1 : value.Usage + 1,
                Value     = val,
                Timestamp = DateTime.UtcNow
            };

            cache.AddOrUpdate(cacheKey, cachedResult, (_, __) => cachedResult);
            RemoveExpire(cacheKey);
        }
        private CachedResult CacheResult(string userName, string sourceUrl, string redirectUrl, DynamicJsonObject responseData)
        {
            string       cacheKey     = GetCacheKey(userName, sourceUrl);
            CachedResult cachedResult = new CachedResult(redirectUrl, responseData);

            lock (dataLock)
            {
                if (cachedResults.ContainsKey(cacheKey))
                {
                    cachedResults[cacheKey] = cachedResult;
                }
                else
                {
                    cachedResults.Add(cacheKey, cachedResult);
                }
            }

            return(cachedResult);
        }
Пример #30
0
        public void BufferData(int highestRow)
        {
            if (!IsBufferMode)
            {
                var isStop = false;
                while (CachedResult.Rows.Count < highestRow && !isStop)
                {
                    isStop = true;
                    DataRow row = null;
                    if (DataReader != null && DataReader.Read())
                    {
                        row = CachedResult.NewRow();
                        CachedResult.Rows.Add(row);

                        var count = DataReader.FieldCount;
                        for (var i = 0; i < count; i++)
                        {
                            row[DataReader.GetName(i)] = DataReader.GetValue(i);
                        }

                        isStop = false;
                    }

                    if (DataReader2 != null && DataReader2.Read())
                    {
                        if (row == null)
                        {
                            row = CachedResult.NewRow();
                            CachedResult.Rows.Add(row);
                        }

                        var count = DataReader2.FieldCount;
                        for (var i = 0; i < count; i++)
                        {
                            row[DataReader2.GetName(i)] = DataReader2.GetValue(i);
                        }

                        isStop = false;
                    }
                }
            }
        }
Пример #31
0
        public virtual void Add(string key, string value)
        {
            if (value == null)
            {
                return;
            }

            HashSet <string> items;
            CachedResult     result = Get(key);

            if (result != null)
            {
                items = (HashSet <string>)result.Value;
            }
            else
            {
                items = new HashSet <string>();
                Set(key, items);
            }
            items.Add(value);
        }
Пример #32
0
        public bool Add(string cacheKey, T val, CachePolicy cachePolicy = null)
        {
            //LRUcache 不支持对单个缓存对象的缓存策略
            Ensure.That(cachePolicy).IsNull();
            CachedResult value;

            if (cache.TryGetValue(cacheKey, out value) && (DateTime.UtcNow - value.Timestamp) <= this.maxDuration)
            {
                return(false);
            }
            var cachedResult = new CachedResult
            {
                Usage     = value == null ? 1 : value.Usage + 1,
                Value     = val,
                Timestamp = DateTime.UtcNow
            };

            bool resutlt = cache.TryAdd(cacheKey, cachedResult);

            RemoveExpire(cacheKey);
            return(resutlt);
        }
Пример #33
0
        public virtual CachedResult Get(string key)
        {
            CachedResult result = null;

            ReadLock(delegate
            {
                if (RequestCache.Items.Contains(key))
                {
                    result = new CachedResult(RequestCache.Items[key]);
                }
                else
                {
                    result = cache.Get(key);
                    if (result != null)
                    {
                        // we have to store it back in the per request, to ensure that we
                        // wouldn't lose it during this request
                        RequestCache.Items[key] = result.Value;
                    }
                }
            });
            return(result);
        }
Пример #34
0
			public bool IsAdministrator(WindowsIdentity windowsIdentity)
			{
				CachedResult value;
				if (cache.TryGetValue(windowsIdentity.User, out value) && (SystemTime.UtcNow - value.Timestamp) <= maxDuration)
				{
					Interlocked.Increment(ref value.Usage);
					return value.Value.Value;
				}

				var cachedResult = new CachedResult
				{
					Usage = value == null ? 1 : value.Usage + 1,
					Value = new Lazy<bool>(() =>
					{
						try
						{
							return IsAdministratorNoCache(windowsIdentity.Name);
						}
						catch (Exception e)
						{
							log.WarnException("Could not determine whatever user is admin or not, assuming not", e);
							return false;
						}
					}),
					Timestamp = SystemTime.UtcNow
				};

				cache.AddOrUpdate(windowsIdentity.User, cachedResult, (_, __) => cachedResult);
				if (cache.Count > CacheMaxSize)
				{
					foreach (var source in cache
							.Where(x => (SystemTime.UtcNow - x.Value.Timestamp) > maxDuration))
					{
						CachedResult ignored;
						cache.TryRemove(source.Key, out ignored);
						log.Debug("Removing expired {0} from cache", source.Key);
					}
					if (cache.Count > CacheMaxSize)
					{
						foreach (var source in cache
						.OrderByDescending(x => x.Value.Usage)
						.ThenBy(x => x.Value.Timestamp)
						.Skip(CacheMaxSize))
						{
							if (source.Key == windowsIdentity.User)
								continue; // we don't want to remove the one we just added
							CachedResult ignored;
							cache.TryRemove(source.Key, out ignored);
							log.Debug("Removing least used {0} from cache", source.Key);
						}
					}
				}

				return cachedResult.Value.Value;
			}
Пример #35
0
			public bool IsInRole(WindowsIdentity windowsIdentity, WindowsBuiltInRole role)
			{
				CachedResult value;
				if (cache.TryGetValue(windowsIdentity.User, out value) && (SystemTime.UtcNow - value.Timestamp) <= maxDuration)
				{
					Interlocked.Increment(ref value.Usage);
					return IsInRole(value, role);
				}

				var cachedResult = new CachedResult
				{
					Usage = value == null ? 1 : value.Usage + 1,
					AuthorizationGroups = new Lazy<IList<Principal>>(() => GetUserAuthorizationGroups(windowsIdentity.Name)),
					Timestamp = SystemTime.UtcNow
				};

				cache.AddOrUpdate(windowsIdentity.User, cachedResult, (_, __) => cachedResult);
				if (cache.Count > CacheMaxSize)
				{
					foreach (var source in cache
							.Where(x => (SystemTime.UtcNow - x.Value.Timestamp) > maxDuration))
					{
						CachedResult ignored;
						cache.TryRemove(source.Key, out ignored);
						log.Debug("Removing expired {0} from cache", source.Key);
					}
					if (cache.Count > CacheMaxSize)
					{
						foreach (var source in cache
						.OrderByDescending(x => x.Value.Usage)
						.ThenBy(x => x.Value.Timestamp)
						.Skip(CacheMaxSize))
						{
							if (source.Key == windowsIdentity.User)
								continue; // we don't want to remove the one we just added
							CachedResult ignored;
							cache.TryRemove(source.Key, out ignored);
							log.Debug("Removing least used {0} from cache", source.Key);
						}
					}
				}

				return IsInRole(cachedResult, role);
			}
Пример #36
0
			private bool IsInRole(CachedResult cachedResult, WindowsBuiltInRole role)
			{
				try
				{
					var authorizationGroups = cachedResult.AuthorizationGroups.Value;

					switch (role)
					{
						case WindowsBuiltInRole.Administrator:
							return IsAdministratorNoCache(authorizationGroups);
						case WindowsBuiltInRole.BackupOperator:
							return IsBackupOperatorNoCache(authorizationGroups);
						default:
							throw new NotSupportedException(role.ToString());
					}
				}
				catch (Exception e)
				{
					log.WarnException("Could not determine whatever user is admin or not, assuming not", e);
					return false;
				}
			}
		protected override WebResponse GetWebResponse(WebRequest request)
		{
			WebResponse response = base.GetWebResponse(request);
			HttpWebResponse webResponse = response as HttpWebResponse;
			if (webResponse != null)
			{
				// Check to see if it's a redirect
				if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399)
				{
					string uriString = webResponse.Headers["Location"];
					string cachingKey = String.Empty;
					if (request.Credentials != null)
					{
						NetworkCredential credential = request.Credentials.GetCredential(request.RequestUri, "Basic");
						cachingKey = credential.UserName;
					}
					else
					{
						CookieCollection cookieCollection = Cookies.GetCookies(request.RequestUri);
						foreach (Cookie currentCookie in cookieCollection)
						{
							if (currentCookie.Name.Equals(RallyRestApi.ZSessionID, StringComparison.InvariantCultureIgnoreCase))
							{
								cachingKey = currentCookie.Value;
								break;
							}
						}
					}

					returnValue = GetCachedResult(cachingKey, request.RequestUri.ToString());
					if ((returnValue == null) || (!returnValue.Url.Equals(uriString)))
					{
						if (returnValue != null)
						{
							ClearCacheResult(cachingKey, request.RequestUri.ToString());
							returnValue = null;
						}

						CookieAwareWebClient webClient = GetWebClient();

						string cacheableDataValue = webClient.DownloadString(uriString);
						returnValue = CacheResult(cachingKey, request.RequestUri.ToString(), uriString, serializer.Deserialize(cacheableDataValue));
					}
					else
						isCachedResult = true;
				}
			}

			return response;
		}
		private CachedResult CacheResult(string userName, string sourceUrl, string redirectUrl, DynamicJsonObject responseData)
		{
			string cacheKey = GetCacheKey(userName, sourceUrl);
			CachedResult cachedResult = new CachedResult(redirectUrl, responseData);
			lock (dataLock)
			{
				if (cachedResults.ContainsKey(cacheKey))
					cachedResults[cacheKey] = cachedResult;
				else
					cachedResults.Add(cacheKey, cachedResult);


				FileInfo fileLoction = GetFileLocation(cacheKey);
				File.WriteAllBytes(fileLoction.FullName, SerializeData(cachedResult));
			}

			return cachedResult;
		}
		/// <summary>
		/// Serializes data to be sent across the wire.
		/// <para>Serialization errors of lists may be caused by a missing constructor with the following signature:
		/// public [CLASS NAME](SerializationInfo info, StreamingContext context)
		/// </para>
		/// </summary>
		/// <param name="obj">The object to serialize.</param>
		/// <returns>The serialized data.</returns>
		private static byte[] SerializeData(CachedResult obj)
		{
			if (obj == null)
				return null;

			BinaryFormatter binFor = new BinaryFormatter();
			byte[] data = null;
			using (MemoryStream stream = new MemoryStream())
			{
				binFor.Serialize(stream, obj);
				data = stream.ToArray();
			}

			return data;
		}
Пример #40
0
        public Engine CheckoutScript(Func<ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request, RavenJObject customFunctions)
        {
            CachedResult value;
            var patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);
            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out value))
            {
                Interlocked.Increment(ref value.Usage);
                Engine context;
                if (value.Queue.TryDequeue(out context))
                {
                    return context;
                }
            }
            var result = createEngine(request);

            RavenJToken functions;
            if (customFunctions != null && customFunctions.TryGetValue("Functions", out functions))

                result.Execute(string.Format(@"var customFunctions = function() {{  var exports = {{ }}; {0};
                            return exports;
                        }}();
                        for(var customFunction in customFunctions) {{
                            this[customFunction] = customFunctions[customFunction];
                        }};", functions), new ParserOptions { Source = "customFunctions.js" });
            var cachedResult = new CachedResult
            {
                Usage = 1,
                Queue = new ConcurrentQueue<Engine>(),
                Timestamp = SystemTime.UtcNow
            };

            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, cachedResult, (_, existing) =>
            {
                Interlocked.Increment(ref existing.Usage);
                return existing;
            });
            if (cacheDic.Count > CacheMaxSize)
            {
                foreach (var source in cacheDic
                    .Where(x => x.Value != null)
                    .OrderByDescending(x => x.Value.Usage)
                    .ThenBy(x => x.Value.Timestamp)
                    .Skip(CacheMaxSize - CacheMaxSize / 10))
                {
                    if (Equals(source.Key, request))
                        continue; // we don't want to remove the one we just added
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
                foreach (var source in cacheDic.Where(x => x.Value == null))
                {
                    CachedResult ignored;
                    cacheDic.TryRemove(source.Key, out ignored);
                }
            }

            return result;
        }