コード例 #1
0
        public async Task StoreStringTest()
        {
            using (MemcachedClient client = GetClient())
            {
                Assert.True(await client.StoreAsync(StoreMode.Set, "TestString", "Hello world!", DateTime.Now.AddSeconds(10)), "StoreString failed.");

                Assert.Equal("Hello world!", await client.GetValueAsync <string>("TestString"));
            }
        }
コード例 #2
0
        /// <summary>
        /// Stores value in the cache with expiration time.
        /// </summary>
        /// <typeparam name="T">the class type</typeparam>
        /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param>
        /// <param name="key">a unique value key.</param>
        /// <param name="value">a value to store.</param>
        /// <param name="timeout">expiration timeout in milliseconds.</param>
        /// <returns>stored value.</returns>
        public override async Task <T> StoreAsync <T>(string correlationId, string key, T value, long timeout)
        {
            CheckOpened(correlationId);

            timeout = timeout > 0 ? timeout : Timeout;

            var result = await _client.StoreAsync(Enyim.Caching.Memcached.StoreMode.Set, key, value, TimeSpan.FromMilliseconds(timeout));

            return(result ? value : default(T));
        }
コード例 #3
0
        protected Task <bool> StoreAsync(StoreMode mode = StoreMode.Set, string key = null, object value = null)
        {
            if (string.IsNullOrEmpty(key))
            {
                key = GetUniqueKey("store");
            }

            if (value == null)
            {
                value = GetRandomString();
            }
            return(_client.StoreAsync(mode, key, value, TimeSpan.MaxValue));
        }
コード例 #4
0
        public async Task ExpirationTestTimeSpan()
        {
            using (MemcachedClient client = GetClient())
            {
                var cacheKey = $"ExpirationTest-TimeSpan-{new Random().Next()}";
                Assert.True(await client.StoreAsync(StoreMode.Set, cacheKey, "ExpirationTest:TimeSpan", new TimeSpan(0, 0, 3)), "Expires:Timespan failed");
                Assert.Equal("ExpirationTest:TimeSpan", await client.GetValueAsync <string>(cacheKey));

                await Task.Delay(TimeSpan.FromSeconds(4));

                Assert.Null(await client.GetValueAsync <string>(cacheKey));
            }
        }
コード例 #5
0
        public async Task StoreObjectTest()
        {
            TestData td = new TestData();

            td.FieldA = "Hello";
            td.FieldB = "World";
            td.FieldC = 19810619;
            td.FieldD = true;

            using (MemcachedClient client = GetClient())
            {
                Assert.True(await client.StoreAsync(StoreMode.Set, TestObjectKey, td, DateTime.Now.AddSeconds(5)));
            }
        }
コード例 #6
0
        public async Task ExpirationTestDateTime()
        {
            using (MemcachedClient client = GetClient())
            {
                var      cacheKey  = $"Expires-DateTime-{new Random().Next()}";
                DateTime expiresAt = DateTime.Now.AddSeconds(3);
                Assert.True(await client.StoreAsync(StoreMode.Set, cacheKey, "Expires:DateTime", expiresAt), "Expires:DateTime failed");
                Assert.Equal("Expires:DateTime", await client.GetValueAsync <string>(cacheKey));

                await Task.Delay(TimeSpan.FromSeconds(4));

                Assert.Null(await client.GetValueAsync <string>(cacheKey));
            }
        }
コード例 #7
0
        public async Task FlushTest()
        {
            using (MemcachedClient client = GetClient())
            {
                for (int i = 0; i < 10; i++)
                {
                    string cacheKey = $"Hello_Flush_{i}";
                    Assert.True(await client.StoreAsync(StoreMode.Set, cacheKey, i, DateTime.Now.AddSeconds(30)));
                }

                await client.FlushAllAsync();

                for (int i = 0; i < 10; i++)
                {
                    string cacheKey = $"Hello_Flush_{i}";
                    Assert.Null(await client.GetValueAsync <string>(cacheKey));
                }
            }
        }
コード例 #8
0
        public override bool TryAcquireLock(string correlationId, string key, long ttl)
        {
            CheckOpened(correlationId);

            return(_client.StoreAsync(Enyim.Caching.Memcached.StoreMode.Add, key, "lock", TimeSpan.FromMilliseconds(ttl)).Result);
        }
コード例 #9
0
        public async Task <bool> AddAsync <T>(string key, T value)
        {
            _logger.Debug("Adding item to cache with key {0}", key);

            return(await _cache.StoreAsync(StoreMode.Set, key, value, TimeSpan.FromDays(90)));
        }