Exemplo n.º 1
0
        public void Evict2()
        {
            var memCache = new MemCache(100); //Consider using mock

            memCache.Add("mykey1", 0, DateTime.MaxValue, new byte[] {});
            memCache.Add("mykey2", 0, DateTime.MaxValue, new byte[] {});
            var strategy = new RandomEvictionStrategy(memCache);

            strategy.EvictEntry();
            Assert.AreEqual(1, memCache.Keys.Count());
        }
Exemplo n.º 2
0
 public void If_I_Try_To_Add_An_Existent_Item_It_Must_Returns_False()
 {
     string itemInCache = "The quick brown fox jumps over the lazy dog";
     string anotherItem = "justanotheritem";
     string key = "MS'_favorite_pangram";
     ICache cache = new MemCache();
     //
     bool firstAddition = cache.Add<string>(itemInCache, key);
     bool secondAddition = cache.Add<string>(anotherItem, key);
     //
     Assert.IsFalse(secondAddition);
 }
Exemplo n.º 3
0
 public void If_I_Try_To_Add_A_Null_Item_To_Cache_It_Must_Throws_Exception()
 {
     string key = "justanotherkey";
     //
     ICache cache = new MemCache();
     cache.Add<string>(null, key);
 }
Exemplo n.º 4
0
        public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
        {
            try
            {
                var actionAuthentication = context.ActionContext.ActionDescriptor.GetCustomAttributes <AuthenticationAttribute>();
                //验证
                if (
                    (actionAuthentication.Count > 0 && actionAuthentication.Last().Authenticate == true) ||
                    (actionAuthentication.Count == 0 && AuthenticationConfig.GetAuthenticateNoAttribute() == true)
                    )
                {
                    var ticket = context.Request.Headers.GetValues(AuthenticationConfig.AuthenticationString).FirstOrDefault();
                    if (ticket == null)
                    {
                        throw new AuthenticationException("can not get  ticket !");
                    }
                    object obj = MemCache.Get(AuthenticationConfig.TicketKeyPrefix + ticket);
                    if (obj == null)
                    {
                        AuthorizationConfig.RemoveRoles(ticket);
                        throw new AuthenticationException("Ticket has Expired !");
                    }

                    if (AuthenticationConfig.GetRefreshTicket())
                    {
                        MemCache.Add(AuthenticationConfig.TicketKeyPrefix + ticket, obj, DateTime.Now.AddSeconds(AuthenticationConfig.GetTicketExpire()));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 5
0
 public void If_I_Try_To_Add_An_Item_With_Null_Key_It_Must_Throws_Exception()
 {
     string itemInCache = "The quick brown fox jumps over the lazy dog";
     //
     ICache cache = new MemCache();
     cache.Add<string>(itemInCache, null);
 }
Exemplo n.º 6
0
        public static UserInfo GetUser(string upn, bool clearCache = false)
        {
            if (clearCache)
            {
                MemCache.Clear(upn);
            }

            var result = (UserInfo)MemCache.Get(upn);

            if (result == null)
            {
                using (var ctx = new ClientContext(SharePointHelper.Url))
                {
                    var u = ctx.Web.EnsureUser(upn);

                    ctx.Load(u);
                    ctx.Load(u.Groups);
                    ctx.ExecuteQuery();

                    result = MapToModel(u);
                    MemCache.Add(upn, result);
                }
            }

            return(result);
        }
Exemplo n.º 7
0
        public static string CaptchaImage(this HtmlHelper helper, string guid, string id, int height, int width, string className, string inputid = "captcha-guid")
        {
            CaptchaImage image = new CaptchaImage {
                Height = height, Width = width
            };

            MemCache.Add(guid, image, TimeSpan.FromMinutes(5));

            StringBuilder stringBuilder = new StringBuilder(256);

            stringBuilder.Append("<input type=\"hidden\" id=\"" + inputid + "\" name=\"" + inputid + "\" value=\"");
            stringBuilder.Append(guid);
            stringBuilder.Append("\" />");
            stringBuilder.AppendLine();
            stringBuilder.Append("<img src=\"");
            string imgUrl = "/Captcha/Image";

            stringBuilder.Append(imgUrl + "?guid=" + guid);
            stringBuilder.Append("\" alt=\"CAPTCHA\" id=\"" + id + "\" width=\"");
            stringBuilder.Append(width);
            stringBuilder.Append("\" height=\"");
            stringBuilder.Append(height);
            stringBuilder.Append("\" class=\"");
            stringBuilder.Append(className);
            stringBuilder.Append("\" />");

            return(stringBuilder.ToString());
        }
Exemplo n.º 8
0
 public void Should_invalidate_cache()
 {
     _cache = new MemCache <int, string>(3, TimeSpan.MaxValue);
     _cache.Add(1, "a");
     _cache.Invalidate(1);
     Assert.Null(_cache.Get(1));
 }
Exemplo n.º 9
0
        public async Task Should_not_return_expired()
        {
            _cache = new MemCache <int, string>(3, TimeSpan.FromMilliseconds(49));
            _cache.Add(1, "a");
            await Task.Delay(50);

            Assert.Equal(null, _cache.Get(1));
        }
Exemplo n.º 10
0
        public static void RemoveCurrentCapcha(HttpContextBase ctx)
        {
            string       guid  = ctx.Request.Form["captcha-guid"];
            CaptchaImage image = Captcha.CaptchaImage.GetCachedCaptcha(guid);

            image.ResetText();
            MemCache.Add(guid, image, TimeSpan.FromMinutes(5));
        }
Exemplo n.º 11
0
 public async Task Should_renew_age_when_returned__when_configured(bool renewAge) {
     _cache = new MemCache<int, string>(3, TimeSpan.FromMilliseconds(49));
     _cache.AutoRenew = renewAge;
     _cache.Add(1, "a");
     await Task.Delay(25);
     _cache.Get(1);
     await Task.Delay(25);
     Assert.AreEqual(renewAge ? "a" : null, _cache.Get(1));
 }
Exemplo n.º 12
0
 public void If_I_Try_To_Add_An_Item_To_Cache_It_Must_Returns_True()
 {
     string itemInCache = "The quick brown fox jumps over the lazy dog";
     string key = "MS'_favorite_pangram";
     //
     ICache cache = new MemCache();
     bool result = cache.Add<string>(itemInCache, key);
     //
     Assert.IsTrue(result);
 }
Exemplo n.º 13
0
 public void If_I_Try_To_Add_A_KeyValuePair_Structure()
 {
     string itemInCache = "The quick brown fox jumps over the lazy dog";
     string key = "MS'_favorite_pangram";
     //
     ICache cache = new MemCache();
     bool result = cache.Add<string>(new KeyValuePair<string, string>(key, itemInCache));
     //
     Assert.IsTrue(result);
 }
Exemplo n.º 14
0
        public static void RemoveCurrentCapcha(string key)
        {
            CaptchaImage image = Captcha.CaptchaImage.GetCachedCaptcha(key);

            if (image != null)
            {
                image.ResetText();
                MemCache.Add(key, image, TimeSpan.FromMinutes(5));
            }
        }
Exemplo n.º 15
0
        public async Task Should_renew_age_when_returned__when_configured(bool renewAge)
        {
            _cache           = new MemCache <int, string>(3, TimeSpan.FromMilliseconds(49));
            _cache.AutoRenew = renewAge;
            _cache.Add(1, "a");
            await Task.Delay(25);

            _cache.Get(1);
            await Task.Delay(25);

            Assert.Equal(renewAge ? "a" : null, _cache.Get(1));
        }
Exemplo n.º 16
0
        public void EvictsEarliestAdded()
        {
            _cache.Add("key1", 0, DateTime.MaxValue, new byte[] { 0, 1, 2, 3, 4 });
            _cache.Add("key2", 0, DateTime.MaxValue, new byte[] { 0, 1, 2, 3, 4 });
            _cache.Add("key3", 0, DateTime.MaxValue, new byte[] { 0, 1, 2, 3, 4 });

            var keys = _cache.Keys.ToArray();

            Assert.AreEqual(2, keys.Length);
            Assert.IsTrue(keys.Contains("key2"));
            Assert.IsTrue(keys.Contains("key3"));
        }
Exemplo n.º 17
0
        public void EnsureAddStoreNotification()
        {
            _cache.Add("key", 123, new DateTime(1999, 1, 1), Encoding.ASCII.GetBytes("TestData"));

            var notification = GetNotification();

            var store = notification as StoreNotification;

            Assert.IsNotNull(store);
            Assert.AreEqual("key", store.Key);
            Assert.AreEqual("TestData", Encoding.ASCII.GetString(store.Data));
            Assert.AreEqual((ulong)123, store.Flags);
            Assert.AreEqual(StoreOperation.Add, store.Operation);
            Assert.AreEqual(new DateTime(1999, 1, 1), store.Expiry);
        }
Exemplo n.º 18
0
        public static void RegisterUserAndRole(object user, List <string> roles)
        {
            string   ticket  = Tools.ToShortMD5(Guid.NewGuid().ToString("N"));
            DateTime expired = DateTime.Now.AddSeconds(AuthenticationConfig.GetTicketExpire());

            HttpContext.Current.Response.Headers.Add(AuthenticationConfig.AuthenticationString, ticket);
            if (user != null)
            {
                MemCache.Add(AuthenticationConfig.TicketKeyPrefix + ticket, user, expired);
            }
            if (roles != null && roles.Count > 0)
            {
                AuthorizationConfig.AddRoles(ticket, roles);
            }
        }
Exemplo n.º 19
0
        public Response <object> CaptchaImage(int height, int width)
        {
            Response <object> result = new Response <object>();
            CaptchaImage      image  = new CaptchaImage {
                Height = height, Width = width
            };
            var guid = Guid.NewGuid().ToString("N");

            MemCache.Add(guid, image, TimeSpan.FromMinutes(5));
            result.data = new
            {
                captchaGuid     = guid,
                captchaimageSrc = $"https://www.postpony.com/Captcha/Image?guid={guid}"
            };
            return(result);
        }
Exemplo n.º 20
0
        public static List <UserInfo> GetUserByGroup(string groupName)
        {
            var result = (List <UserInfo>)MemCache.Get(groupName);

            if (result == null)
            {
                result = new List <UserInfo>();
                var users = EmailHelper.GetUsersInGroup(groupName);

                foreach (User usr in users)
                {
                    var ui = MapToModel(usr);

                    result.Add(ui);
                }

                MemCache.Add(groupName, result);
            }

            return(result);
        }
Exemplo n.º 21
0
 public void Should_add_simple_item()
 {
     _cache.Add(1, "1");
     Assert.Equal("1", _cache.Get(1));
 }
Exemplo n.º 22
0
 private void SetupValueKey(string key = "valuekey")
 {
     _cache.Add(key, 0, DateTime.MaxValue, Encoding.UTF8.GetBytes("1234567890"));
 }
Exemplo n.º 23
0
 public void If_I_Try_To_Add_Null_Key_Value_Pair_It_Must_Throws_Exception()
 {
     ICache cache = new MemCache();
     cache.Add<string>(null, null);
 }
 public static void SetDisplay(CaptchaDispalyType type)
 {
     MemCache.Add(GetId(type), "CaptchaDispaly", TimeSpan.FromMinutes(CaptchaShowTime));
 }
 /// <summary>
 /// 设置错误计数
 /// </summary>
 /// <param name="count"></param>
 public static void SetErrorCount(int count)
 {
     MemCache.Add(CacheKey + "_" + "ErrorCount" + "_" + Ip, count, TimeSpan.FromMinutes(CaptchaShowTime));
 }
Exemplo n.º 26
0
 internal static void AddRoles(string ticket, List <string> roles) => MemCache.Add(RolePrefix + ticket, roles);
Exemplo n.º 27
0
 public async Task Should_not_return_expired() {
     _cache = new MemCache<int, string>(3, TimeSpan.FromMilliseconds(49));
     _cache.Add(1, "a");
     await Task.Delay(50);
     Assert.AreEqual(null, _cache.Get(1));
 }