Пример #1
0
        public void TestAuthentication()
        {
            using (var srv = Ignition.Start(SecureServerConfig()))
            {
                srv.GetCluster().SetActive(true);

                using (var cli = Ignition.StartClient(SecureClientConfig()))
                {
                    CacheClientConfiguration ccfg = new CacheClientConfiguration()
                    {
                        Name          = "TestCache",
                        QueryEntities = new[]
                        {
                            new QueryEntity
                            {
                                KeyType   = typeof(string),
                                ValueType = typeof(string),
                            },
                        },
                    };

                    ICacheClient <string, string> cache = cli.GetOrCreateCache <string, string>(ccfg);

                    cache.Put("key1", "val1");

                    cache.Query(new SqlFieldsQuery("CREATE USER \"my_User\" WITH PASSWORD 'my_Password'")).GetAll();
                }

                var cliCfg = SecureClientConfig();

                cliCfg.UserName = "******";
                cliCfg.Password = "******";

                using (var cli = Ignition.StartClient(cliCfg))
                {
                    ICacheClient <string, string> cache = cli.GetCache <string, string>("TestCache");

                    string val = cache.Get("key1");

                    Assert.True(val == "val1");
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Start new node, create new user with given credentials and try to authenticate.
        /// </summary>
        /// <param name="user">Username</param>
        /// <param name="pass">Password</param>
        private void CreateNewUserAndAuthenticate(string user, string pass)
        {
            using (var srv = Ignition.Start(SecureServerConfig()))
            {
                srv.GetCluster().SetActive(true);

                using (var cli = Ignition.StartClient(GetSecureClientConfig()))
                {
                    CacheClientConfiguration ccfg = new CacheClientConfiguration
                    {
                        Name          = "TestCache",
                        QueryEntities = new[]
                        {
                            new QueryEntity
                            {
                                KeyType   = typeof(string),
                                ValueType = typeof(string),
                            },
                        },
                    };

                    ICacheClient <string, string> cache = cli.GetOrCreateCache <string, string>(ccfg);

                    cache.Put("key1", "val1");

                    cache.Query(new SqlFieldsQuery("CREATE USER \"" + user + "\" WITH PASSWORD '" + pass + "'")).GetAll();
                }

                var cliCfg = GetSecureClientConfig();

                cliCfg.UserName = user;
                cliCfg.Password = pass;

                using (var cli = Ignition.StartClient(cliCfg))
                {
                    ICacheClient <string, string> cache = cli.GetCache <string, string>("TestCache");

                    string val = cache.Get("key1");

                    Assert.True(val == "val1");
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Execute individual Put and Get.
        /// </summary>
        /// <param name="cache">Cache instance.</param>
        private static void PutGet(ICacheClient <int, Organization> cache)
        {
            // Create new Organization to store in cache.
            Organization org = new Organization(
                "Microsoft",
                new Address("1096 Eddy Street, San Francisco, CA", 94109),
                OrganizationType.Private,
                DateTime.Now
                );

            // Put created data entry to cache.
            cache.Put(1, org);

            // Get recently created employee as a strongly-typed fully de-serialized instance.
            Organization orgFromCache = cache.Get(1);

            Console.WriteLine();
            Console.WriteLine(">>> Retrieved organization instance from cache: " + orgFromCache);
        }
        public static long Increment(this ICacheClient client, string key, uint value, TimeSpan timeToLive, long?startingValue = null)
        {
            if (!startingValue.HasValue)
            {
                startingValue = 0;
            }

            var count = client.Get <long?>(key);

            if (count.HasValue)
            {
                return(client.Increment(key, value));
            }

            long newValue = startingValue.Value + value;

            client.Set(key, newValue, timeToLive);
            return(newValue);
        }
Пример #5
0
        public object Get(DownloadLicense downloadRequest)
        {
            var cacheKey = UrnId.Create <Model.License>("IssueToken", downloadRequest.Token.ToString());
            var license  = cacheClient.Get <Portable.Licensing.License>(cacheKey);

            if (license == null)
            {
                return(new HttpResult(HttpStatusCode.NotFound));
            }

            var responseStream = new MemoryStream();

            license.Save(responseStream);

            var response = new HttpResult(responseStream, "application/xml");

            response.Headers[HttpHeaders.ContentDisposition] = "attachment; filename=License.lic";

            return(response);
        }
Пример #6
0
        public T Get <T>(string key, Func <T> defaultValue = null, Func <T, bool> isEmpty = null)
        {
            if (Contain(key))
            {
                return(client.Get <T>(key));
            }
            else if (null == defaultValue)
            {
                return(default(T));
            }

            var value = defaultValue();

            if (null == isEmpty || !isEmpty(value))
            {
                Set(key, value);
            }

            return(value);
        }
Пример #7
0
        public static T TryGet <T>(this ICacheClient cacheClient, string id, Func <T> getter)
            where T : class, IHasStringId
        {
            var cacheKey   = UrnId.Create <T>(id);
            var cachedItem = cacheClient.Get <CacheItem <T> >(cacheKey);

            if (cachedItem?.Item != null && (DateTime.UtcNow - cachedItem.CachedOn) < _maxTimeToCacheFor)
            {
                _log.LogDebug($"CacheHit for [{typeof(T).Name}].[{id}]");

                return(cachedItem.Item);
            }

            _log.LogDebug($"CacheMiss for [{typeof(T).Name}].[{id}]");

            var item = getter();

            if (!(item?.Id).IsNotNullOrEmpty())
            {
                cacheClient.TryRemove <T>(id);
                return(default);
Пример #8
0
        public override void EventProcessing(EventContext context)
        {
            if (Settings.Current.WebsiteMode == WebsiteMode.Dev)
            {
                return;
            }

            // Throttle errors by client ip address to no more than X every 5 minutes.
            var ri = context.Event.GetRequestInfo();

            if (ri == null || String.IsNullOrEmpty(ri.ClientIpAddress))
            {
                return;
            }

            string throttleCacheKey = String.Concat("bot:", ri.ClientIpAddress, ":", DateTime.Now.Floor(_throttlingPeriod).Ticks);
            var    requestCount     = _cacheClient.Get <int?>(throttleCacheKey);

            if (requestCount != null)
            {
                _cacheClient.Increment(throttleCacheKey, 1);
                requestCount++;
            }
            else
            {
                _cacheClient.Set(throttleCacheKey, 1, _throttlingPeriod);
                requestCount = 1;
            }

            if (requestCount < Settings.Current.BotThrottleLimit)
            {
                return;
            }

            Log.Info().Message("Bot throttle triggered. IP: {0} Time: {1} Project: {2}", ri.ClientIpAddress, DateTime.Now.Floor(_throttlingPeriod), context.Event.ProjectId).Project(context.Event.ProjectId).Write();
            // the throttle was triggered, go and delete all the errors that triggered the throttle to reduce bot noise in the system
            Task.Run(() => _eventRepository.RemoveAllByClientIpAndDateAsync(ri.ClientIpAddress, DateTime.Now.Floor(_throttlingPeriod), DateTime.Now.Ceiling(_throttlingPeriod)));
            context.IsCancelled = true;
        }
Пример #9
0
        public override bool Get <T>(string key, out T value)
        {
            try
            {
                var obj = _redisCacheClient.Get <T>(key);

                if (obj == null)
                {
                    value = default(T);
                    return(false);
                }

                value = (T)obj;
            }
            catch
            {
                value = default(T);
                return(false);
            }

            return(true);
        }
        public static long IncrementIf(this ICacheClient client, string key, uint value, TimeSpan timeToLive, bool shouldIncrement, long?startingValue = null)
        {
            if (!startingValue.HasValue)
            {
                startingValue = 0;
            }

            var count = client.Get <long?>(key);

            if (count.HasValue && !shouldIncrement)
            {
                return(count.Value);
            }

            if (count.HasValue)
            {
                return(client.Increment(key, value));
            }

            client.Set(key, startingValue.Value + value, timeToLive);
            return(startingValue.Value + value);
        }
Пример #11
0
        /// <summary>
        /// Tries to get a previously cached response for the specified invocation, returning false if not found.
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="request"> </param>
        /// <param name="cacheClient"></param>
        /// <param name="region"></param>
        /// <returns></returns>
        private static bool GetCachedResponse(IInvocation invocation, object request, ICacheClient cacheClient, string region)
        {
            if (!cacheClient.RegionExists(region))
            {
                return(false);
            }

            var strategy  = ResponseDataCachingStrategy.Get(invocation.Method);
            var cacheKeys = strategy.GetCacheKeys(request);

            if (cacheKeys == null || cacheKeys.Length == 0)
            {
                return(false);
            }

            var cacheData = new Dictionary <string, object>();

            foreach (var cacheKey in cacheKeys)
            {
                var data = cacheClient.Get(cacheKey, new CacheGetOptions(region));
                if (data != null)                 //Assume null is not a relevant cache entry.
                {
                    cacheData[cacheKey] = data;
                }
            }

            if (cacheData.Count > 0)
            {
                var response = strategy.ConstructResponse(request, cacheData);
                if (response != null)
                {
                    invocation.ReturnValue = response;
                    return(true);
                }
            }

            return(false);
        }
        public static IAuthSession GetUntypedSession(this ICacheClient cache,
                                                     IRequest httpReq = null, IResponse httpRes = null)
        {
            var sessionKey = GetSessionKey(httpReq);

            if (sessionKey != null)
            {
                var userSession = cache.Get <IAuthSession>(sessionKey);
                if (!Equals(userSession, default(AuthUserSession)))
                {
                    return(userSession);
                }
            }

            if (sessionKey == null)
            {
                SessionFeature.CreateSessionIds(httpReq, httpRes);
            }

            var unAuthorizedSession = (IAuthSession)typeof(AuthUserSession).CreateInstance();

            return(unAuthorizedSession);
        }
Пример #13
0
        public T AddOrGetExisting <T>(string key, Func <T> valueFactory, int expirationSeconds = 0)
        {
            T value = default(T);

            try
            {
                value = _redisClient.Get <T>(key);
            }
            catch
            {
                return(valueFactory());
            }

            if (value == null)
            {
                value = valueFactory();

                try
                {
                    if (expirationSeconds > 0)
                    {
                        _redisClient.Add(key, value, new TimeSpan(0, 0, expirationSeconds));
                    }
                    else
                    {
                        _redisClient.Add(key, value);
                    }
                }
                catch
                {
                }

                return(value);
            }

            return(value);
        }
        public static bool HasValidCache(this ICacheClient cacheClient, IRequest req, string cacheKey, DateTime?checkLastModified, out DateTime?lastModified)
        {
            lastModified = null;

            if (!HostContext.GetPlugin <HttpCacheFeature>().ShouldAddLastModifiedToOptimizedResults())
            {
                return(false);
            }

            var ticks = cacheClient.Get <long>(DateCacheKey(cacheKey));

            if (ticks > 0)
            {
                lastModified = new DateTime(ticks, DateTimeKind.Utc);
                if (checkLastModified == null)
                {
                    return(false);
                }

                return(checkLastModified.Value <= lastModified.Value);
            }

            return(false);
        }
        public void Resolve_with_no_CompressionType_does_not_cache_compressed_result()
        {
            var xmlResult = ContentCacheManager.Resolve(
                () => model,
                serializationContext,
                cacheClient,
                CacheKey,
                null);

            Assert.That(xmlResult, Is.EqualTo(xmlModel));

            var cachedResult = cacheClient.Get <ModelWithIdAndName>(CacheKey);

            ModelWithIdAndName.AssertIsEqual(model, cachedResult);

            var serializedCacheKey     = ContentCacheManager.GetSerializedCacheKey(CacheKey, MimeTypes.Xml);
            var serializedCachedResult = cacheClient.Get <string>(serializedCacheKey);

            Assert.That(serializedCachedResult, Is.EqualTo(xmlModel));

            AssertNoCompressedResults(cacheClient, serializedCacheKey);
        }
        public void Can_set_and_remove_entry()
        {
            var key = 1.ToUrn < Item > ();

            var item = Cache.Get <Item>(key);

            Assert.That(item, Is.Null);

            var whenNotExists = Cache.Set(key, new Item {
                Id = 1, Name = "Foo"
            });

            Assert.That(whenNotExists, Is.True);
            var whenExists = Cache.Set(key, new Item {
                Id = 1, Name = "Foo"
            });

            Assert.That(whenExists, Is.True);

            item = Cache.Get <Item>(key);
            Assert.That(item, Is.Not.Null);
            Assert.That(item.Name, Is.EqualTo("Foo"));

            whenExists = Cache.Remove(key);
            Assert.That(whenExists, Is.True);

            whenNotExists = Cache.Remove(key);
            Assert.That(whenNotExists, Is.False);
        }
Пример #17
0
        /// <summary>
        /// Cache get.
        /// </summary>
        private void Get(BenchmarkState state)
        {
            var idx = BenchmarkUtils.GetRandomInt(Dataset);

            _cache.Get(idx);
        }
 public static bool IsOverRequestLimit(this Organization organization, ICacheClient cacheClient, int apiThrottleLimit) {
     var cacheKey = String.Concat("api", ":", organization.Id, ":", DateTime.UtcNow.Floor(TimeSpan.FromMinutes(15)).Ticks);
     long? limit = cacheClient.Get<long?>(cacheKey);
     return limit.HasValue && limit.Value >= apiThrottleLimit;
 }
Пример #19
0
        protected async override Task <JobResult> RunInternalAsync(CancellationToken token)
        {
            QueueEntry <EventNotification> queueEntry = null;

            try {
                queueEntry = _queue.Dequeue();
            } catch (Exception ex) {
                if (!(ex is TimeoutException))
                {
                    return(JobResult.FromException(ex, "An error occurred while trying to dequeue the next EventNotification: {0}", ex.Message));
                }
            }
            if (queueEntry == null)
            {
                return(JobResult.Success);
            }

            var  eventNotification = queueEntry.Value;
            bool shouldLog         = eventNotification.Event.ProjectId != Settings.Current.InternalProjectId;
            int  emailsSent        = 0;

            Log.Trace().Message("Process notification: project={0} event={1} stack={2}", eventNotification.Event.ProjectId, eventNotification.Event.Id, eventNotification.Event.StackId).WriteIf(shouldLog);

            var project = _projectRepository.GetById(eventNotification.Event.ProjectId, true);

            if (project == null)
            {
                queueEntry.Abandon();
                return(JobResult.FailedWithMessage("Could not load project {0}.", eventNotification.Event.ProjectId));
            }
            Log.Trace().Message("Loaded project: name={0}", project.Name).WriteIf(shouldLog);

            var organization = _organizationRepository.GetById(project.OrganizationId, true);

            if (organization == null)
            {
                queueEntry.Abandon();
                return(JobResult.FailedWithMessage("Could not load organization {0}.", project.OrganizationId));
            }
            Log.Trace().Message("Loaded organization: name={0}", organization.Name).WriteIf(shouldLog);

            var stack = _stackRepository.GetById(eventNotification.Event.StackId);

            if (stack == null)
            {
                queueEntry.Abandon();
                return(JobResult.FailedWithMessage("Could not load stack {0}.", eventNotification.Event.StackId));
            }

            if (!organization.HasPremiumFeatures)
            {
                queueEntry.Complete();
                Log.Info().Message("Skipping \"{0}\" because organization \"{1}\" does not have premium features.", eventNotification.Event.Id, eventNotification.Event.OrganizationId).WriteIf(shouldLog);
                return(JobResult.Success);
            }

            if (stack.DisableNotifications || stack.IsHidden)
            {
                queueEntry.Complete();
                Log.Info().Message("Skipping \"{0}\" because stack \"{1}\" notifications are disabled or stack is hidden.", eventNotification.Event.Id, eventNotification.Event.StackId).WriteIf(shouldLog);
                return(JobResult.Success);
            }

            if (token.IsCancellationRequested)
            {
                queueEntry.Abandon();
                return(JobResult.Cancelled);
            }

            Log.Trace().Message("Loaded stack: title={0}", stack.Title).WriteIf(shouldLog);
            int totalOccurrences = stack.TotalOccurrences;

            // after the first 2 occurrences, don't send a notification for the same stack more then once every 30 minutes
            var lastTimeSent = _cacheClient.Get <DateTime>(String.Concat("notify:stack-throttle:", eventNotification.Event.StackId));

            if (totalOccurrences > 2 &&
                !eventNotification.IsRegression &&
                lastTimeSent != DateTime.MinValue &&
                lastTimeSent > DateTime.Now.AddMinutes(-30))
            {
                queueEntry.Complete();
                Log.Info().Message("Skipping message because of stack throttling: last sent={0} occurrences={1}", lastTimeSent, totalOccurrences).WriteIf(shouldLog);
                return(JobResult.Success);
            }

            // don't send more than 10 notifications for a given project every 30 minutes
            var    projectTimeWindow = TimeSpan.FromMinutes(30);
            string cacheKey          = String.Concat("notify:project-throttle:", eventNotification.Event.ProjectId, "-", DateTime.UtcNow.Floor(projectTimeWindow).Ticks);
            long   notificationCount = _cacheClient.Increment(cacheKey, 1, projectTimeWindow);

            if (notificationCount > 10 && !eventNotification.IsRegression)
            {
                queueEntry.Complete();
                Log.Info().Project(eventNotification.Event.ProjectId).Message("Skipping message because of project throttling: count={0}", notificationCount).WriteIf(shouldLog);
                return(JobResult.Success);
            }

            if (token.IsCancellationRequested)
            {
                queueEntry.Abandon();
                return(JobResult.Cancelled);
            }

            foreach (var kv in project.NotificationSettings)
            {
                var settings = kv.Value;
                Log.Trace().Message("Processing notification: user={0}", kv.Key).WriteIf(shouldLog);

                var user = _userRepository.GetById(kv.Key);
                if (user == null || String.IsNullOrEmpty(user.EmailAddress))
                {
                    Log.Error().Message("Could not load user {0} or blank email address {1}.", kv.Key, user != null ? user.EmailAddress : "").Write();
                    continue;
                }

                if (!user.IsEmailAddressVerified)
                {
                    Log.Info().Message("User {0} with email address {1} has not been verified.", kv.Key, user != null ? user.EmailAddress : "").WriteIf(shouldLog);
                    continue;
                }

                if (!user.EmailNotificationsEnabled)
                {
                    Log.Info().Message("User {0} with email address {1} has email notifications disabled.", kv.Key, user != null ? user.EmailAddress : "").WriteIf(shouldLog);
                    continue;
                }

                if (!user.OrganizationIds.Contains(project.OrganizationId))
                {
                    Log.Error().Message("Unauthorized user: project={0} user={1} organization={2} event={3}", project.Id, kv.Key, project.OrganizationId, eventNotification.Event.Id).Write();
                    continue;
                }

                Log.Trace().Message("Loaded user: email={0}", user.EmailAddress).WriteIf(shouldLog);

                bool shouldReportNewError      = settings.ReportNewErrors && eventNotification.IsNew && eventNotification.Event.IsError();;
                bool shouldReportCriticalError = settings.ReportCriticalErrors && eventNotification.IsCritical && eventNotification.Event.IsError();;
                bool shouldReportRegression    = settings.ReportEventRegressions && eventNotification.IsRegression;
                bool shouldReportNewEvent      = settings.ReportNewEvents && eventNotification.IsNew;
                bool shouldReportCriticalEvent = settings.ReportCriticalEvents && eventNotification.IsCritical;
                bool shouldReport = shouldReportNewError || shouldReportCriticalError || shouldReportRegression || shouldReportNewEvent || shouldReportCriticalEvent;

                Log.Trace().Message("Settings: newerror={0} criticalerror={1} regression={2} new={3} critical={4}",
                                    settings.ReportNewErrors, settings.ReportCriticalErrors,
                                    settings.ReportEventRegressions, settings.ReportNewEvents, settings.ReportCriticalEvents).WriteIf(shouldLog);
                Log.Trace().Message("Should process: newerror={0} criticalerror={1} regression={2} new={3} critical={4}",
                                    shouldReportNewError, shouldReportCriticalError,
                                    shouldReportRegression, shouldReportNewEvent, shouldReportCriticalEvent).WriteIf(shouldLog);

                var requestInfo = eventNotification.Event.GetRequestInfo();
                // check for known bots if the user has elected to not report them
                if (shouldReport && requestInfo != null && !String.IsNullOrEmpty(requestInfo.UserAgent))
                {
                    ClientInfo info = null;
                    try {
                        info = Parser.GetDefault().Parse(requestInfo.UserAgent);
                    } catch (Exception ex) {
                        Log.Warn().Project(eventNotification.Event.ProjectId).Message("Unable to parse user agent {0}. Exception: {1}",
                                                                                      requestInfo.UserAgent, ex.Message).Write();
                    }

                    var botPatterns = project.Configuration.Settings.ContainsKey(SettingsDictionary.KnownKeys.UserAgentBotPatterns)
                        ? project.Configuration.Settings.GetStringCollection(SettingsDictionary.KnownKeys.UserAgentBotPatterns).ToList()
                        : new List <string>();

                    if (info != null && info.Device.IsSpider || requestInfo.UserAgent.AnyWildcardMatches(botPatterns))
                    {
                        shouldReport = false;
                        Log.Info().Message("Skipping because event is from a bot \"{0}\".", requestInfo.UserAgent).WriteIf(shouldLog);
                    }
                }

                if (!shouldReport)
                {
                    continue;
                }

                var model = new EventNotificationModel(eventNotification)
                {
                    ProjectName      = project.Name,
                    TotalOccurrences = totalOccurrences
                };

                // don't send notifications in non-production mode to email addresses that are not on the outbound email list.
                if (Settings.Current.WebsiteMode != WebsiteMode.Production &&
                    !Settings.Current.AllowedOutboundAddresses.Contains(v => user.EmailAddress.ToLowerInvariant().Contains(v)))
                {
                    Log.Info().Message("Skipping because email is not on the outbound list and not in production mode.").WriteIf(shouldLog);
                    continue;
                }

                Log.Trace().Message("Sending email to {0}...", user.EmailAddress).Write();
                _mailer.SendNotice(user.EmailAddress, model);
                emailsSent++;
                Log.Trace().Message("Done sending email.").WriteIf(shouldLog);
            }

            // if we sent any emails, mark the last time a notification for this stack was sent.
            if (emailsSent > 0)
            {
                _cacheClient.Set(String.Concat("notify:stack-throttle:", eventNotification.Event.StackId), DateTime.Now, DateTime.Now.AddMinutes(15));
                Log.Info().Message("Notifications sent: event={0} stack={1} count={2}", eventNotification.Event.Id, eventNotification.Event.StackId, emailsSent).WriteIf(shouldLog);
            }

            queueEntry.Complete();

            return(JobResult.Success);
        }
        private static void AssertNoCompressedResults(
            ICacheClient cacheClient, string serializedCacheKey)
        {
            foreach (var compressionType in CompressionTypes.AllCompressionTypes)
            {
                var compressedCacheKey = ContentCacheManager.GetCompressedCacheKey(
                    serializedCacheKey, compressionType);

                var compressedCachedResult = cacheClient.Get<byte[]>(compressedCacheKey);
                Assert.That(compressedCachedResult, Is.Null);
            }
        }
Пример #21
0
 public T Get <T>(string key)
 {
     return(_client.Get <T>(key));
 }
Пример #22
0
        public void UpdateSession(ICacheClient cacheClient)
        {
            string sessionKey = SessionFeature.GetSessionKey(this.Id);

            CustomUserSession session = cacheClient.Get<CustomUserSession>(sessionKey);
            session.UserId = UserId;

            //The sesion  never expiered.
            cacheClient.Set(sessionKey, session);
        }
		/// <summary>
		/// Tries to get a previously cached response for the specified invocation, returning false if not found.
		/// </summary>
		/// <param name="invocation"></param>
		/// <param name="cacheClient"></param>
		/// <param name="cacheKey"></param>
		/// <param name="region"></param>
		/// <returns></returns>
		private static bool GetCachedResponse(IInvocation invocation, ICacheClient cacheClient, string cacheKey, string region)
		{
			if (cacheKey == null || !cacheClient.RegionExists(region))
				return false;

			// check cache
			var response = cacheClient.Get(cacheKey, new CacheGetOptions(region));
			if (response != null)
			{
				invocation.ReturnValue = response;
				return true;
			}
			return false;
		}
Пример #24
0
 public T Get <T>(string key)
 {
     return(cache.Get <T>(prefix + key));
 }
Пример #25
0
        public void CacheGet_PrimitiveKeyType_RequestIsRoutedToPrimaryNode(int key, int gridIdx)
        {
            var res = _cache.Get(key);

            Assert.AreEqual(key, res);
            Assert.AreEqual(gridIdx, GetClientRequestGridIndex());
        }
Пример #26
0
        /// <summary>
        /// Returns a serialized dto based on the mime-type, created using the factoryFn().
        /// If the correct mimeType is found, it will return the cached result.
        /// If a compressionType is set it will return a compressed version of the result.
        /// If no result is found, the dto is created by the factoryFn()
        /// The dto is set on the cacheClient[cacheKey] =&gt; factoryFn()
        /// e.g. urn:user:1 =&gt; dto
        /// The serialized dto is set on the cacheClient[cacheKey.mimeType] =&gt; serialized(factoryFn())
        /// e.g. urn:user:1.xml =&gt; xmlDto
        /// Finally, if a compressionType is specified, the compressed dto is set on the
        /// cacheClient[cacheKey.mimeType.compressionType] =&gt; compressed(serialized(factoryFn()))
        /// e.g. urn:user:1.xml.gzip =&gt; compressedXmlDto
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="factoryFn">The factory fn.</param>
        /// <param name="serializationContext">The serialization context.</param>
        /// <param name="compressionType">Type of the compression. -optional null means no compression</param>
        /// <param name="cacheClient">The cache client</param>
        /// <param name="cacheKey">The base cache key</param>
        /// <param name="expireCacheIn">How long to cache for, default is no expiration</param>
        /// <returns></returns>
        public static object Resolve <TResult>(
            Func <TResult> factoryFn,
            IRequestContext serializationContext,
            ICacheClient cacheClient,
            string cacheKey,
            TimeSpan?expireCacheIn)
            where TResult : class
        {
            factoryFn.ThrowIfNull("factoryFn");
            serializationContext.ThrowIfNull("mimeType");
            cacheClient.ThrowIfNull("cacheClient");
            cacheKey.ThrowIfNull("cacheKey");

            var    contentType = serializationContext.ResponseContentType;
            var    compressionType = serializationContext.CompressionType;
            string modifiers = null, jsonp = null;

            if (contentType == ContentType.Json)
            {
                jsonp = serializationContext.Get <IHttpRequest>().GetJsonpCallback();
                if (jsonp != null)
                {
                    modifiers = ".jsonp," + jsonp.SafeVarName();
                }
            }

            var cacheKeySerialized = GetSerializedCacheKey(cacheKey, contentType, modifiers);

            var doCompression = compressionType != null;

            var cacheKeySerializedZip = GetCompressedCacheKey(
                cacheKeySerialized, compressionType);

            if (doCompression)
            {
                var compressedResult = cacheClient.Get <byte[]>(cacheKeySerializedZip);
                if (compressedResult != null)
                {
                    return(new CompressedResult(
                               compressedResult,
                               compressionType,
                               contentType));
                }
            }
            else
            {
                var serializedResult = cacheClient.Get <string>(cacheKeySerialized);
                if (serializedResult != null)
                {
                    return(serializedResult);
                }
            }

            var dto = factoryFn();

            CacheSet(cacheClient, cacheKey, dto, expireCacheIn);

            var serializedDto = ContentSerializer <TResult> .ToSerializedString(dto, serializationContext);

            if (jsonp != null)
            {
                serializedDto = jsonp + "(" + serializedDto + ")";
            }

            CacheSet(cacheClient, cacheKeySerialized, serializedDto, expireCacheIn);

            if (doCompression)
            {
                var compressedSerializedDto = ContentSerializer <TResult> .ToCompressedBytes(
                    serializedDto, compressionType);

                CacheSet(cacheClient, cacheKeySerializedZip, compressedSerializedDto, expireCacheIn);

                return((compressedSerializedDto != null)
                                        ? new CompressedResult(compressedSerializedDto, compressionType, contentType)
                                        : null);
            }

            return(serializedDto);
        }
Пример #27
0
 public T Get <T>(string key) where T : class, new()
 {
     return(_cacheClient.Get <T>(key));
 }
        private static void AssertNoSerializedResults(
            ICacheClient cacheClient, string cacheKey)
        {
            foreach (var mimeType in ContentCacheManager.AllCachedMimeTypes)
            {
                var serializedCacheKey = ContentCacheManager.GetSerializedCacheKey(
                    cacheKey, mimeType);

                var serializedResult = cacheClient.Get<string>(serializedCacheKey);
                Assert.That(serializedResult, Is.Null);
            }
        }
Пример #29
0
 /// <summary>
 /// 取缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key">键</param>
 /// <returns></returns>
 public T Get <T>(string key)
 {
     return(cacheClient.Get <T>(key));
 }
    	/// <summary>
    	/// Tries to get a previously cached response for the specified invocation, returning false if not found.
    	/// </summary>
    	/// <param name="invocation"></param>
    	/// <param name="request"> </param>
    	/// <param name="cacheClient"></param>
    	/// <param name="region"></param>
    	/// <returns></returns>
    	private static bool GetCachedResponse(IInvocation invocation, object request, ICacheClient cacheClient, string region)
		{
			if (!cacheClient.RegionExists(region))
				return false;

			var strategy = ResponseDataCachingStrategy.Get(invocation.Method);
			var cacheKeys = strategy.GetCacheKeys(request);
			if (cacheKeys == null || cacheKeys.Length == 0)
				return false;

			var cacheData = new Dictionary<string, object>();
    		foreach (var cacheKey in cacheKeys)
    		{
				var data = cacheClient.Get(cacheKey, new CacheGetOptions(region));
				if (data != null) //Assume null is not a relevant cache entry.
					cacheData[cacheKey] = data;
    		}

			if (cacheData.Count > 0)
			{
				var response = strategy.ConstructResponse(request, cacheData);
				if (response != null)
				{
					invocation.ReturnValue = response;
					return true;
				}
			}

			return false;
		}
 public T Get <T>(string key)
 {
     return(_database.Get <T>(key));
 }
Пример #32
0
        public static Wrapper <T> FromCache <T>(this string urn, ICacheClient cache) where T : class, IHasGuidId
        {
            var cached = cache.Get <string>(urn);

            return(cached?.FromJson <Wrapper <T> >());
        }
Пример #33
0
 public T Get(string key) => client.Get <T>(keyPrefix + key);
 private static void AssertNoCachedResult(
     ICacheClient cacheClient, string cacheKey)
 {
     var cachedResult = cacheClient.Get<ModelWithIdAndName>(cacheKey);
     Assert.That(cachedResult, Is.Null);
 }
        private object ProcessNotification(IMessage <EventNotification> message)
        {
            int emailsSent         = 0;
            EventNotification data = message.GetBody();

            Log.Trace().Message("Process notification: project={0} event={1} stack={2}", data.Event.ProjectId, data.Event.Id, data.Event.StackId).Write();

            var project = _projectRepository.GetByIdCached(data.Event.ProjectId);

            if (project == null)
            {
                Log.Error().Message("Could not load project {0}.", data.Event.ProjectId).Write();
                return(null);
            }
            Log.Trace().Message("Loaded project: name={0}", project.Name).Write();

            var organization = _organizationRepository.GetByIdCached(project.OrganizationId);

            if (organization == null)
            {
                Log.Error().Message("Could not load organization {0}.", project.OrganizationId).Write();
                return(null);
            }
            Log.Trace().Message("Loaded organization: name={0}", organization.Name).Write();

            var stack = _stackRepository.GetById(data.Event.StackId);

            if (stack == null)
            {
                Log.Error().Message("Could not load stack {0}.", data.Event.StackId).Write();
                return(null);
            }

            if (!organization.HasPremiumFeatures)
            {
                Log.Trace().Message("Skipping because organization does not have premium features.").Write();
                return(null);
            }

            if (stack.DisableNotifications || stack.IsHidden)
            {
                Log.Trace().Message("Skipping because stack notifications are disabled or it's hidden.").Write();
                return(null);
            }

            Log.Trace().Message("Loaded stack: title={0}", stack.Title).Write();
            int totalOccurrences = stack.TotalOccurrences;

            // after the first 5 occurrences, don't send a notification for the same stack more then once every 15 minutes
            var lastTimeSent = _cacheClient.Get <DateTime>(String.Concat("NOTIFICATION_THROTTLE_", data.Event.StackId));

            if (totalOccurrences > 5 && !data.IsRegression && lastTimeSent != DateTime.MinValue &&
                lastTimeSent > DateTime.Now.AddMinutes(-15))
            {
                Log.Info().Message("Skipping message because of throttling: last sent={0} occurrences={1}", lastTimeSent, totalOccurrences).Write();
                return(null);
            }

            foreach (var kv in project.NotificationSettings)
            {
                var settings = kv.Value;
                Log.Trace().Message("Processing notification: user={0}", kv.Key).Write();

                var user = _userRepository.GetById(kv.Key);
                if (user == null || String.IsNullOrEmpty(user.EmailAddress))
                {
                    Log.Error().Message("Could not load user {0} or blank email address {1}.", kv.Key, user != null ? user.EmailAddress : "").Write();
                    continue;
                }

                if (!user.IsEmailAddressVerified)
                {
                    Log.Info().Message("User {0} with email address {1} has not been verified.", kv.Key, user != null ? user.EmailAddress : "").Write();
                    continue;
                }

                if (!user.EmailNotificationsEnabled)
                {
                    Log.Trace().Message("User {0} with email address {1} has email notifications disabled.", kv.Key, user != null ? user.EmailAddress : "").Write();
                    continue;
                }

                if (!user.OrganizationIds.Contains(project.OrganizationId))
                {
                    // TODO: Should this notification setting be deleted?
                    Log.Error().Message("Unauthorized user: project={0} user={1} organization={2} event={3}", project.Id, kv.Key,
                                        project.OrganizationId, data.Event.Id).Write();
                    continue;
                }

                Log.Trace().Message("Loaded user: email={0}", user.EmailAddress).Write();

                bool shouldReportOccurrence    = settings.Mode != NotificationMode.None;
                bool shouldReportCriticalError = settings.ReportCriticalErrors && data.IsCritical;
                bool shouldReportRegression    = settings.ReportRegressions && data.IsRegression;

                Log.Trace().Message("Settings: mode={0} critical={1} regression={2} 404={3} bots={4}",
                                    settings.Mode, settings.ReportCriticalErrors,
                                    settings.ReportRegressions, settings.Report404Errors,
                                    settings.ReportKnownBotErrors).Write();
                Log.Trace().Message("Should process: occurrence={0} critical={1} regression={2}",
                                    shouldReportOccurrence, shouldReportCriticalError,
                                    shouldReportRegression).Write();

                if (settings.Mode == NotificationMode.New && !data.IsNew)
                {
                    shouldReportOccurrence = false;
                    Log.Trace().Message("Skipping because message is not new.").Write();
                }

                // check for 404s if the user has elected to not report them
                if (shouldReportOccurrence && settings.Report404Errors == false && data.Event.IsNotFound)
                {
                    shouldReportOccurrence = false;
                    Log.Trace().Message("Skipping because message is 404.").Write();
                }

                var requestInfo = data.Event.GetRequestInfo();
                // check for known bots if the user has elected to not report them
                if (shouldReportOccurrence && settings.ReportKnownBotErrors == false &&
                    requestInfo != null && !String.IsNullOrEmpty(requestInfo.UserAgent))
                {
                    ClientInfo info = null;
                    try {
                        info = Parser.GetDefault().Parse(requestInfo.UserAgent);
                    } catch (Exception ex) {
                        Log.Warn().Project(data.Event.ProjectId).Message("Unable to parse user agent {0}. Exception: {1}",
                                                                         requestInfo.UserAgent, ex.Message).Write();
                    }

                    if (info != null && info.Device.IsSpider)
                    {
                        shouldReportOccurrence = false;
                        Log.Trace().Message("Skipping because message is bot.").Write();
                    }
                }

                // stack being set to send all will override all other settings
                if (!shouldReportOccurrence && !shouldReportCriticalError && !shouldReportRegression)
                {
                    continue;
                }

                // don't send notifications in non-production mode to email addresses that are not on the outbound email list.
                if (Settings.Current.WebsiteMode != WebsiteMode.Production &&
                    !Settings.Current.AllowedOutboundAddresses.Contains(v => user.EmailAddress.ToLowerInvariant().Contains(v)))
                {
                    Log.Trace().Message("Skipping because email is not on the outbound list and not in production mode.").Write();
                    continue;
                }

                Log.Trace().Message("Sending email to {0}...", user.EmailAddress).Write();
                _mailer.SendNotice(user.EmailAddress, data);
                emailsSent++;
                Log.Trace().Message("Done sending email.").Write();
            }

            // if we sent any emails, mark the last time a notification for this stack was sent.
            if (emailsSent > 0)
            {
                _cacheClient.Set(String.Concat("NOTIFICATION_THROTTLE_", data.Event.StackId), DateTime.Now, DateTime.Now.AddMinutes(15));
            }

            return(null);
        }
        public UserInfoRedis GetUserInfo(string ticket)
        {
            var obj = _cacheClient.Get <UserInfoRedis>(ticket);

            return(obj);
        }
        public static object ResolveFromCache(this ICacheClient cacheClient,
                                              string cacheKey,
                                              IRequest request)
        {
            DateTime?lastModified;
            var      checkModifiedSince = CheckModifiedSince(request);

            string modifiers = null;

            if (!request.ResponseContentType.IsBinary())
            {
                if (request.ResponseContentType == MimeTypes.Json)
                {
                    string jsonp = request.GetJsonpCallback();
                    if (jsonp != null)
                    {
                        modifiers = ".jsonp," + jsonp.SafeVarName();
                    }
                }

                var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, request.ResponseContentType, modifiers);

                var  compressionType = request.GetCompressionType();
                bool doCompression   = compressionType != null;
                if (doCompression)
                {
                    var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);

                    if (cacheClient.HasValidCache(request, cacheKeySerializedZip, checkModifiedSince, out lastModified))
                    {
                        return(HttpResult.NotModified());
                    }

                    if (request.Response.GetHeader(HttpHeaders.CacheControl) != null)
                    {
                        lastModified = null;
                    }

                    var compressedResult = cacheClient.Get <byte[]>(cacheKeySerializedZip);
                    if (compressedResult != null)
                    {
                        return(new CompressedResult(
                                   compressedResult,
                                   compressionType,
                                   request.ResponseContentType)
                        {
                            LastModified = lastModified,
                        });
                    }
                }
                else
                {
                    if (cacheClient.HasValidCache(request, cacheKeySerialized, checkModifiedSince, out lastModified))
                    {
                        return(HttpResult.NotModified());
                    }

                    var serializedResult = cacheClient.Get <string>(cacheKeySerialized);
                    if (serializedResult != null)
                    {
                        return(serializedResult);
                    }
                }
            }
            else
            {
                var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, request.ResponseContentType, modifiers);
                if (cacheClient.HasValidCache(request, cacheKeySerialized, checkModifiedSince, out lastModified))
                {
                    return(HttpResult.NotModified());
                }

                var serializedResult = cacheClient.Get <byte[]>(cacheKeySerialized);
                if (serializedResult != null)
                {
                    return(serializedResult);
                }
            }

            return(null);
        }
 public void AssertGetSetIntValue(ICacheClient cacheClient)
 {
     cacheClient.Set("test:intkey1", 1);
     var value = cacheClient.Get<int>("test:intkey1");
     Assert.That(value, Is.EqualTo(1));
 }