static void Main(string[] args)
        {
            Person p1 = new Person {
                Id = 1, Age = 10, Name = ""
            };
            Person p2 = new Person {
                Id = 2, Age = 11, Name = ""
            };
            Person p3 = new Person {
                Id = 3, Age = 12, Name = ""
            };
            ICachingService <int, Person> cache = new CachingService <int, Person>();

            //ICachingService<int, Person> cache = new CachingService<int, Person>(new LRUAlgorithm<int, Person>());
            //ICachingService<int, Person> cache = new CachingService<int, Person>(new MRUAlgorithm<int, Person>());
            cache.Add(1, p1);
            cache.Add(2, p2);
            cache.Add(3, p3);
            var rez = cache.Get(2);
            ICachingService <int, Person2> cache2 = new CachingService <int, Person2>();
            Person2 p4 = new Person2 {
                Id = 1, Age = 13, Name = ""
            };

            cache2.Add(1, p4);
            var rez2 = cache.Get(1);
            var rez3 = cache2.Get(1);
        }
Exemplo n.º 2
0
        public void RemovedItemCannotBeRetrievedFromCache()
        {
            var sut = new CachingService();

            sut.Add(TestKey, new object());
            Assert.NotNull(sut.Get <object>(TestKey));
            sut.Remove(TestKey);
            Assert.Null(sut.Get <object>(TestKey));
        }
        public void Get_WillReturnNullIfNoCacheBasedOnThatKey()
        {
            // arrange
            var key = "testKey";

            // act
            var result = _cachingService.Get(key);

            // assert
            Assert.AreEqual(null, result);
        }
        public ActionResult nationalRanking(int?id)
        {
            if (!id.HasValue || !(id >= 2009 && id <= KSU.maxPlayerSeason))
            {
                return(RedirectToAction("national-ranking", "nationalranking", new { id = KSU.maxPlayerSeason }));
            }

            int year = id.Value;

            // Get Dates
            List <double> pollDates = _dataBL.GetRankingDates(year);
            double        maxDate   = pollDates.Max();

            var cacheService = new CachingService();

            var cacheMaxDate = cacheService.Get <double>("chart" + year + "maxdate");

            if (cacheMaxDate < maxDate)
            {
                createChart(year, maxDate, pollDates);
            }

            NationalRankingModel ranking = new NationalRankingModel(new GameListModel(_gameBL.GamesBySeason(year)), year, cacheService.Get <string>("chart" + year + "imagemap"));

            return(View(ranking));
        }
    public KerberosReceiverSecurityToken ReadFromCache(string contextUsername)
    {
        IAppCache appCache = new CachingService();
        KerberosReceiverSecurityToken token = appCache.Get <KerberosReceiverSecurityToken>(contextUsername.ToLower());

        return(token);
    }
Exemplo n.º 6
0
        public void AddThenGetReturnsCachedObject()
        {
            var sut = new CachingService();

            sut.Add(TestKey, "testObject");
            Assert.AreEqual("testObject", sut.Get <string>(TestKey));
        }
        internal FileRecord Get(Guid id)
        {
            if (id == Guid.Empty)
            {
                return(null);
            }

            string cachingKey = "fsFileRecord:" + id.ToString();

            FileRecord fileRecord;

            fileRecord = _cachingService.Get <FileRecord>(cachingKey);
            if (fileRecord != null)
            {
                return(fileRecord);
            }

            List <CommandParameter> parameterList = new List <CommandParameter>();

            parameterList.Add(new CommandParameter("@id", id));

            List <FileRecord> fileRecordList =
                _dataBase.Select <FileRecord>("SELECT * FROM [File] Where Id = @id", parameterList);

            if (fileRecordList.Count == 0)
            {
                return(null);
            }

            fileRecord = fileRecordList[0];

            _cachingService.Set(cachingKey, fileRecord, _cachingExpiresIn);

            return(fileRecord);
        }
Exemplo n.º 8
0
        public void AddWithOffsetReturnsCachedItem()
        {
            var sut = new CachingService();

            sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1));
            Assert.AreEqual("testObject", sut.Get <string>(TestKey));
        }
Exemplo n.º 9
0
        public void AddWithPolicyReturnsCachedItem()
        {
            var sut = new CachingService();

            sut.Add(TestKey, "testObject", new CacheItemPolicy());
            Assert.AreEqual("testObject", sut.Get <string>(TestKey));
        }
Exemplo n.º 10
0
        public void GetNullKeyThrowsException()
        {
            var    sut = new CachingService();
            Action act = () => sut.Get <object>(null);

            act.ShouldThrow <ArgumentNullException>();
        }
Exemplo n.º 11
0
        public void AddWithSlidingReturnsCachedItem()
        {
            var sut = new CachingService();

            sut.Add(TestKey, "testObject", new TimeSpan(5000));
            Assert.AreEqual("testObject", sut.Get <string>(TestKey));
        }
Exemplo n.º 12
0
        public void Get_ReturnsNullWhenItemNotCached()
        {
            CachingService cachingService = new CachingService();
            string         response       = cachingService.Get("abc") as string;

            Assert.Null(response);
        }
Exemplo n.º 13
0
        public void GetEmptyKeyThrowsException()
        {
            var    sut = new CachingService();
            Action act = () => sut.Get <object>("");

            act.ShouldThrow <ArgumentOutOfRangeException>();
        }
Exemplo n.º 14
0
        public ActionResult ResetPwd()
        {
            MemberResetPasswordArgs args = RequestArgs <MemberResetPasswordArgs>();

            if (args == null)
            {
                return(FailedResult("参数无效。"));
            }

            //校验验证码

            string code = _cachingService.Get(args.mobilephone + "ValidationCode");

            if (string.IsNullOrEmpty(code) == true)
            {
                return(FailedResult("验证码已失效。"));
            }

            if (code.Equals(args.code) == false)
            {
                return(FailedResult("错误的验证码。"));
            }

            Member member = _memberManager.GetMemberByMobilePhone(args.mobilephone);

            if (member == null)
            {
                return(FailedResult("指定的会员不存在!"));
            }

            NormalResult result = _memberManager.UpdateMemberPassword(member.id, IOHelper.GetMD5HashFromString(args.password));

            return(ApiResult(result.Successful, "修改密码成功。"));
        }
Exemplo n.º 15
0
        public void GetWithClassTypeParamReturnsType()
        {
            var sut    = new CachingService();
            var cached = new EventArgs();

            sut.Add(TestKey, cached);
            Assert.AreEqual(cached, sut.Get <EventArgs>(TestKey));
        }
Exemplo n.º 16
0
        public void AddWithOffsetThatExpiresReturnsNull()
        {
            var sut = new CachingService();

            sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1));
            Thread.Sleep(1500);
            Assert.IsNull(sut.Get <string>(TestKey));
        }
Exemplo n.º 17
0
        public void AddWithSlidingThatExpiresReturnsNull()
        {
            var sut = new CachingService();

            sut.Add(TestKey, "testObject", new TimeSpan(750));
            Thread.Sleep(1500);
            Assert.IsNull(sut.Get <string>(TestKey));
        }
Exemplo n.º 18
0
        public void GetWithWrongStructTypeParamReturnsNull()
        {
            var sut    = new CachingService();
            var cached = new DateTime();

            sut.Add(TestKey, cached);
            Assert.AreEqual(new TimeSpan(), sut.Get <TimeSpan>(TestKey));
        }
Exemplo n.º 19
0
        public void GetWithNullableIntRetunsCachedNonNullableInt()
        {
            var       sut      = new CachingService();
            const int expected = 123;

            sut.Add(TestKey, expected);
            Assert.AreEqual(expected, sut.Get <int?>(TestKey));
        }
Exemplo n.º 20
0
        public void GetWithStructTypeParamReturnsType()
        {
            var sut    = new CachingService();
            var cached = new DateTime(2000, 1, 1);

            sut.Add(TestKey, cached);
            Assert.AreEqual(cached, sut.Get <DateTime>(TestKey));
        }
Exemplo n.º 21
0
        public void GetWithValueTypeParamReturnsType()
        {
            var       sut    = new CachingService();
            const int cached = 3;

            sut.Add(TestKey, cached);
            Assert.AreEqual(3, sut.Get <int>(TestKey));
        }
Exemplo n.º 22
0
        public void GetWithWrongClassTypeParamReturnsNull()
        {
            var sut    = new CachingService();
            var cached = new EventArgs();

            sut.Add(TestKey, cached);
            Assert.IsNull(sut.Get <ArgumentNullException>(TestKey));
        }
Exemplo n.º 23
0
        public void GetCachedNullableStructTypeParamReturnsType()
        {
            var      sut    = new CachingService();
            DateTime?cached = new DateTime();

            sut.Add(TestKey, cached);
            Assert.AreEqual(cached.Value, sut.Get <DateTime>(TestKey));
        }
Exemplo n.º 24
0
        public void CachingService_MemoryCacheProvider_ShouldCache()
        {
            var key = nameof(CachingService_MemoryCacheProvider_ShouldCache);

            Assert.IsNull(_cacheService.Get <TestObject>(key));

            _cacheService.Add(key, new TestObject {
                Name = "Test", Value = 1024
            });

            var val = _cacheService.Get <TestObject>(key);

            Assert.NotNull(val);
            Assert.AreEqual("Test", val.Name);
            Assert.AreEqual(1024, val.Value);
            // cacheService.Remove(key);
        }
Exemplo n.º 25
0
        public void Get_ReturnsNullWhenItemInvalidated()
        {
            CachingService cachingService = new CachingService();

            cachingService.Put("abc", "response", TimeSpan.FromSeconds(0));
            string response = cachingService.Get("abc") as string;

            Assert.Null(response);
        }
Exemplo n.º 26
0
        public void Put_NullResponseNotCached()
        {
            CachingService cachingService = new CachingService();

            cachingService.Put("abc", null, TimeSpan.FromSeconds(10));
            string response = cachingService.Get("abc") as string;

            Assert.Null(response);
        }
Exemplo n.º 27
0
 public void AddComplexObjectThenGetReturnsCachedObject()
 {
     var sut = new CachingService();
     sut.Add(TestKey, new ComplexTestObject());
     var actual = sut.Get<ComplexTestObject>(TestKey);
     var expected = new ComplexTestObject();
     Assert.AreEqual(ComplexTestObject.SomeMessage, ComplexTestObject.SomeMessage);
     Assert.AreEqual(expected.SomeItems, actual.SomeItems);
 }
Exemplo n.º 28
0
        public void Put_ResponseCached()
        {
            CachingService cachingService = new CachingService();

            cachingService.Put("abc", "response", TimeSpan.FromSeconds(10));
            string response = cachingService.Get("abc") as string;

            Assert.Equal("response", response);
        }
    public void DeleteFromCache(string contextUsername)
    {
        IAppCache appCache = new CachingService();
        KerberosReceiverSecurityToken token = appCache.Get <KerberosReceiverSecurityToken>(contextUsername.ToLower());

        if (token != null)
        {
            appCache.Remove(contextUsername.ToLower());
        }
    }
Exemplo n.º 30
0
        public void GetOrAddAndThenGetValueObjectReturnsCorrectType()
        {
            var        sut   = new CachingService();
            Func <int> fetch = () => 123;

            sut.GetOrAdd(TestKey, fetch);
            var actual = sut.Get <int>(TestKey);

            Assert.AreEqual(123, actual);
        }
Exemplo n.º 31
0
        public void GetOrAddAndThenGetWrongtypeObjectReturnsNull()
        {
            var sut = new CachingService();
            Func <ComplexTestObject> fetch = () => new ComplexTestObject();

            sut.GetOrAdd(TestKey, fetch);
            var actual = sut.Get <Exception>(TestKey);

            Assert.IsNull(actual);
        }
Exemplo n.º 32
0
 public void GetOrAddAndThenGetWrongtypeObjectReturnsNull()
 {
     var sut = new CachingService();
     Func<ComplexTestObject> fetch = () => new ComplexTestObject();
     sut.GetOrAdd(TestKey, fetch);
     var actual = sut.Get<Exception>(TestKey);
     Assert.IsNull(actual);
 }
Exemplo n.º 33
0
 public void RemovedItemCannotBeRetrievedFromCache()
 {
     var sut = new CachingService();
     sut.Add(TestKey, new object());
     Assert.NotNull(sut.Get<object>(TestKey));
     sut.Remove(TestKey);
     Assert.Null(sut.Get<object>(TestKey));
 }
Exemplo n.º 34
0
 public void AddWithOffsetThatExpiresReturnsNull()
 {
     var sut = new CachingService();
     sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1));
     Thread.Sleep(1500);
     Assert.IsNull(sut.Get<string>(TestKey));
 }
Exemplo n.º 35
0
 public void AddWithSlidingReturnsCachedItem()
 {
     var sut = new CachingService();
     sut.Add(TestKey, "testObject", new TimeSpan(5000));
     Assert.AreEqual("testObject", sut.Get<string>(TestKey));
 }
Exemplo n.º 36
0
 public void GetCachedNullableStructTypeParamReturnsType()
 {
     var sut = new CachingService();
     DateTime? cached = new DateTime();
     sut.Add(TestKey, cached);
     Assert.AreEqual(cached.Value, sut.Get<DateTime>(TestKey));
 }
Exemplo n.º 37
0
 public void GetNullKeyThrowsException()
 {
     var sut = new CachingService();
     Action act = () => sut.Get<object>(null);
     act.ShouldThrow<ArgumentNullException>();
 }
Exemplo n.º 38
0
 public void GetWithClassTypeParamReturnsType()
 {
     var sut = new CachingService();
     var cached = new EventArgs();
     sut.Add(TestKey, cached);
     Assert.AreEqual(cached, sut.Get<EventArgs>(TestKey));
 }
Exemplo n.º 39
0
 public void GetWithIntRetunsDefualtIfNotCached()
 {
     var sut = new CachingService();
     Assert.AreEqual(default(int), sut.Get<int>(TestKey));
 }
Exemplo n.º 40
0
 public void GetWithNullableIntRetunsCachedNonNullableInt()
 {
     var sut = new CachingService();
     const int expected = 123;
     sut.Add(TestKey, expected);
     Assert.AreEqual(expected, sut.Get<int?>(TestKey));
 }
Exemplo n.º 41
0
 public void GetWithStructTypeParamReturnsType()
 {
     var sut = new CachingService();
     var cached = new DateTime(2000, 1, 1);
     sut.Add(TestKey, cached);
     Assert.AreEqual(cached, sut.Get<DateTime>(TestKey));
 }
Exemplo n.º 42
0
 public void GetWithValueTypeParamReturnsType()
 {
     var sut = new CachingService();
     const int cached = 3;
     sut.Add(TestKey, cached);
     Assert.AreEqual(3, sut.Get<int>(TestKey));
 }
Exemplo n.º 43
0
 public void GetWithWrongClassTypeParamReturnsNull()
 {
     var sut = new CachingService();
     var cached = new EventArgs();
     sut.Add(TestKey, cached);
     Assert.IsNull(sut.Get<ArgumentNullException>(TestKey));
 }
Exemplo n.º 44
0
 public void GetWithWrongStructTypeParamReturnsNull()
 {
     var sut = new CachingService();
     var cached = new DateTime();
     sut.Add(TestKey, cached);
     Assert.AreEqual(new TimeSpan(), sut.Get<TimeSpan>(TestKey));
 }
Exemplo n.º 45
0
 public void GetOrAddAndThenGetValueObjectReturnsCorrectType()
 {
     var sut = new CachingService();
     Func<int> fetch = () => 123;
     sut.GetOrAdd(TestKey, fetch);
     var actual = sut.Get<int>(TestKey);
     Assert.AreEqual(123, actual);
 }
Exemplo n.º 46
0
 public void GetOrAddAndThenGetObjectReturnsCorrectType()
 {
     var sut = new CachingService();
     Func<ComplexTestObject> fetch = () => new ComplexTestObject();
     sut.GetOrAdd(TestKey, fetch);
     var actual = sut.Get<ComplexTestObject>(TestKey);
     Assert.IsNotNull(actual);
 }
Exemplo n.º 47
0
        public void GetOrAddWithPolicyWithCallbackOnRemovedReturnsTheOriginalCachedObject()
        {
            var sut = new CachingService();
            Func<int> fetch = () => 123;
            CacheEntryRemovedArguments removedCallbackArgs = null;
            CacheEntryRemovedCallback callback = (args) => removedCallbackArgs = args;

            sut.GetOrAdd(TestKey, fetch, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds(100), RemovedCallback = callback});
            var actual = sut.Get<int>(TestKey);

            sut.Remove(TestKey); //force removed callback to fire
            while(removedCallbackArgs == null)
                Thread.Sleep(500);

            Assert.AreEqual(123, removedCallbackArgs.CacheItem.Value);
        }
Exemplo n.º 48
0
 public void GetEmptyKeyThrowsException()
 {
     var sut = new CachingService();
     Action act = () => sut.Get<object>("");
     act.ShouldThrow<ArgumentOutOfRangeException>();
 }
Exemplo n.º 49
0
 public void GetOrAddWithPolicyAndThenGetValueObjectReturnsCorrectType()
 {
     var sut = new CachingService();
     Func<int> fetch = () => 123;
     sut.GetOrAdd(TestKey, fetch, new CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddHours(1), Priority = CacheItemPriority.NotRemovable});
     var actual = sut.Get<int>(TestKey);
     Assert.AreEqual(123, actual);
 }
Exemplo n.º 50
0
 public void AddWithSlidingThatExpiresReturnsNull()
 {
     var sut = new CachingService();
     sut.Add(TestKey, "testObject", new TimeSpan(750));
     Thread.Sleep(1500);
     Assert.IsNull(sut.Get<string>(TestKey));
 }
Exemplo n.º 51
0
 public void GetOrAddWithPolicyAndThenGetObjectReturnsCorrectType()
 {
     var sut = new CachingService();
     Func<ComplexTestObject> fetch = () => new ComplexTestObject();
     sut.GetOrAdd(TestKey, fetch, new CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddHours(1), Priority = CacheItemPriority.NotRemovable});
     var actual = sut.Get<ComplexTestObject>(TestKey);
     Assert.IsNotNull(actual);
 }
Exemplo n.º 52
0
 public void AddWithPolicyReturnsCachedItem()
 {
     var sut = new CachingService();
     sut.Add(TestKey, "testObject", new CacheItemPolicy());
     Assert.AreEqual("testObject", sut.Get<string>(TestKey));
 }
Exemplo n.º 53
0
        public void GetOrAddWithOffsetWillAddAndReturnCached()
        {
            var sut = new CachingService();

            var expectedFirst = sut.GetOrAdd(
                TestKey,
                () => new DateTime(2001, 01, 01),
                DateTimeOffset.Now.AddSeconds(5)
                );
            var expectedSecond = sut.Get<DateTime>(TestKey);

            Assert.AreEqual(2001, expectedFirst.Year);
            Assert.AreEqual(2001, expectedSecond.Year);
        }
Exemplo n.º 54
0
 public void AddThenGetReturnsCachedObject()
 {
     var sut = new CachingService();
     sut.Add(TestKey, "testObject");
     Assert.AreEqual("testObject", sut.Get<string>(TestKey));
 }
Exemplo n.º 55
0
 public void AddWithOffsetReturnsCachedItem()
 {
     var sut = new CachingService();
     sut.Add(TestKey, "testObject", DateTimeOffset.Now.AddSeconds(1));
     Assert.AreEqual("testObject", sut.Get<string>(TestKey));
 }