Example #1
0
        public static object CustomEnrichLogic(IHttpContextAccessor hca)
        {
            var ctx = hca.HttpContext;

            if (ctx == null)
            {
                return(null);
            }

            var httpContextCache = new HttpContextCache
            {
                IpAddress   = ctx.Connection.RemoteIpAddress.ToString(),
                Host        = ctx.Request.Host.ToString(),
                Path        = ctx.Request.Path.ToString(),
                IsHttps     = ctx.Request.IsHttps,
                Scheme      = ctx.Request.Scheme,
                Method      = ctx.Request.Method,
                ContentType = ctx.Request.ContentType,
                Protocol    = ctx.Request.Protocol,
                QueryString = ctx.Request.QueryString.ToString(),
                Query       = ctx.Request.Query.ToDictionary(x => x.Key, y => y.Value.ToString()),
                Headers     = ctx.Request.Headers.ToDictionary(x => x.Key, y => y.Value.ToString())
            };

            httpContextCache.Headers.Remove("Authorization");
            httpContextCache.Headers.Remove("Cookie");

            return(httpContextCache);
        }
        private static HttpContextCache CustomEnricher(IHttpContextAccessor context)
        {
            var ctx = context.HttpContext;

            if (context == null)
            {
                return(null);
            }

            var info = new HttpContextCache
            {
                IpAddress   = ctx.Connection.RemoteIpAddress.ToString(),
                Host        = ctx.Request.Host.ToString(),
                Path        = ctx.Request.Path.ToString(),
                IsHttps     = ctx.Request.IsHttps,
                Scheme      = ctx.Request.Scheme,
                Method      = ctx.Request.Method,
                ContentType = ctx.Request.ContentType,
                Protocol    = ctx.Request.Protocol,
                QueryString = ctx.Request.QueryString.ToString(),
                Query       = ctx.Request.Query.ToDictionary(x => x.Key, y => y.Value.ToString()),
                Headers     = ctx.Request.Headers.ToDictionary(x => x.Key, y => y.Value.ToString()),
                Cookies     = ctx.Request.Cookies.ToDictionary(x => x.Key, y => y.Value.ToString())
            };

            return(info);
        }
Example #3
0
        static void Main(string[] args)
        {
            ICacheStorage cacheStorage = new HttpContextCache();

            cacheStorage.Store("abc", "abc", new TimeSpan(0, 1, 0));

            Console.ReadKey();
        }
Example #4
0
        public void ContainsKeyTest()
        {
            GenerateFakeHttpContext();

            var cache = new HttpContextCache();

            Assert.IsTrue(cache.ContainsKey(TestKey));
        }
 public static MongoUnitOfWork Factory()
 {
     HttpContextCache c = new HttpContextCache();
     return c.Proxy("mongoserver", () =>
     {
         return new MongoUnitOfWork();
     });
     
 }
Example #6
0
        public void RetrieveTest()
        {
            GenerateFakeHttpContext();

            var cache     = new HttpContextCache();
            var retrieved = cache.Retrieve <string>(TestKey);

            Assert.AreEqual(TestValue, retrieved);
        }
Example #7
0
        public void ContainsKeyNonExistingTest()
        {
            var tmpKey = TestKey + "123";

            GenerateFakeHttpContext();

            var cache = new HttpContextCache();

            Assert.IsFalse(cache.ContainsKey(tmpKey));
        }
Example #8
0
        public void RetrieveNonExistingTest()
        {
            var tmpKey = TestKey + "123";

            GenerateFakeHttpContext();

            var cache     = new HttpContextCache();
            var retrieved = cache.Retrieve <string>(tmpKey);

            Assert.IsNull(retrieved);
        }
Example #9
0
        public void RemoveTest()
        {
            GenerateFakeHttpContext();

            var cache = new HttpContextCache();

            cache.Remove(TestKey);

            var retrieved = cache.Retrieve <string>(TestKey);

            Assert.IsNull(retrieved);
        }
Example #10
0
 public static TUser Extract <TUser>(this IIdentity identity)
     where TUser : IEntity
 {
     return(HttpContextCache.GetOrAdd("Olive.IPrincipal.ExtractedUser", () =>
     {
         var id = identity?.Name;
         if (id == null)
         {
             return default(TUser);
         }
         return Task.Factory.RunSync(() => Database.Instance.GetOrDefault <TUser>(id));
     }));
 }
Example #11
0
 public static TUser Extract <TUser>(this ClaimsPrincipal @this)
     where TUser : IEntity
 {
     return(HttpContextCache.GetOrAdd("Olive.Principal.ExtractedUser", () =>
     {
         var id = @this.GetId();
         if (id.IsEmpty())
         {
             return default(TUser);
         }
         return Task.Factory.RunSync(() => Context.Current.Database().GetOrDefault <TUser>(id));
     }));
 }
Example #12
0
        public void StoreTest()
        {
            const string expectedKey   = "test2";
            const string expectedValue = "test2";

            GenerateFakeHttpContext();

            var cache = new HttpContextCache();

            cache.Store(expectedKey, expectedValue);

            var retrieved = cache.Retrieve <string>(expectedKey);

            Assert.AreEqual(expectedValue, retrieved);
        }
        public void All()
        {
            var httpContextCache = new HttpContextCache <int, string>();

            using (HttpManager.OverrideContext("", "GET"))
            {
                // Put
                httpContextCache.Put(1, "TestDataA", TimeSpan.FromMinutes(1));
                httpContextCache.Put(2, "TestDataB", TimeSpan.FromMinutes(2));
                httpContextCache.Put(3, "TestDataC", TimeSpan.FromMinutes(3));
                // GetOrDefault
                Assert.Equals(httpContextCache.GetOrDefault(1), "TestDataA");
                Assert.Equals(httpContextCache.GetOrDefault(2), "TestDataB");
                Assert.Equals(httpContextCache.GetOrDefault(3), "TestDataC");
                Assert.Equals(httpContextCache.GetOrDefault(100), null);
                Assert.Equals(httpContextCache.GetOrDefault(100, "Default"), "Default");
                // GetOrCreate
                Assert.Equals(httpContextCache.GetOrCreate(
                                  101, () => "create 101", TimeSpan.FromSeconds(100)), "create 101");
                Assert.Equals(httpContextCache.GetOrDefault(101), "create 101");
                Assert.Equals(httpContextCache.GetOrCreate(
                                  101, () => "create 101 again", TimeSpan.FromSeconds(100)), "create 101");
                Assert.Equals(httpContextCache.GetOrDefault(101), "create 101");
                httpContextCache.Remove(101);
                // Remove
                httpContextCache.Remove(2);
                httpContextCache.Remove(3);
                Assert.Equals(httpContextCache.Count(), 1);
                Assert.Equals(httpContextCache.GetOrDefault(1), "TestDataA");
                Assert.Equals(httpContextCache.GetOrDefault(2), null);
                Assert.Equals(httpContextCache.GetOrDefault(3), null);
            }
            // Destory value after context end
            using (HttpManager.OverrideContext("", "GET"))
            {
                httpContextCache.Put(1, "TestDataA", TimeSpan.FromMinutes(1));
                Assert.Equals(httpContextCache.GetOrDefault(1), "TestDataA");
            }
            using (HttpManager.OverrideContext("", "GET"))
            {
                Assert.Equals(httpContextCache.GetOrDefault(1), null);
            }
        }
    public static object StandardEnricher(IHttpContextAccessor hca)
    {
        var ctx = hca.HttpContext;

        if (ctx == null)
        {
            return(null);
        }

        var httpContextCache = new HttpContextCache
        {
            IpAddress   = ctx.Connection.RemoteIpAddress?.ToString(),
            Host        = ctx.Request.Host.ToString(),
            Path        = ctx.Request.Path.ToString(),
            IsHttps     = ctx.Request.IsHttps,
            Scheme      = ctx.Request.Scheme,
            Method      = ctx.Request.Method,
            ContentType = ctx.Request.ContentType,
            Protocol    = ctx.Request.Protocol,
            QueryString = ctx.Request.QueryString.ToString(),
        };

        return(httpContextCache);
    }
 public SecondaryCacheEx(ICache cache)
 {
     FirstCache  = new HttpContextCache();
     SecondCache = cache;
 }
Example #16
0
        private static IBlendedCache GetCache()
        {
            var contextCache = new HttpContextCache();
            var volatileCache = new RuntimeMemoryCachingVolatileCache();
            var longTermCache = new DictionaryLongTermCache();
            var configuration = new BlendedCacheConfiguration()
                {
                    DefaultCacheTimeout = new DefaultCacheTimeout()
                    {
                        VolatileTimeoutInSeconds = 5,
                        LongTermTimeoutInSeconds = 10,
                    }
                }; // this could be driven by the web.config

            return new BlendedCache.BlendedCache(contextCache, volatileCache, _dictionaryLongTermCache, configuration);
        }