Exemple #1
0
        /// <summary>
        /// 如果该键不存在,则通过使用指定的函数将键/值对添加到 Memcached 中。 如果该键存在,则返回新值或现有值。
        /// </summary>
        /// <param name="key">要添加的元素的键。</param>
        /// <param name="valueFactory">用于为键生成值的函数。</param>
        /// <param name="expiresAt">过期时间,默认永久有效</param>
        /// <returns>键的值。 如果 Memcached 中已存在该键,则为该键的现有值;如果 Memcached 中不存在该键,则为新值。</returns>
        public static T GetOrAdd <T>(string key, Func <string, T> valueFactory, DateTime expiresAt = default(DateTime))
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (valueFactory == null)
            {
                throw new ArgumentNullException(nameof(valueFactory));
            }

            T local = default(T);
            IGetOperationResult <T> result = client.ExecuteGet <T>(key);

            if (result.Success && result.HasValue)
            {
                local = result.Value;
            }
            else
            {
                local = valueFactory(key);
                if (expiresAt == default(DateTime))
                {
                    Set(key, local);
                }
                else
                {
                    Set(key, local, expiresAt);
                }
            }
            return(local);
        }
        public bool TryGetValue(CacheKey key, out HttpResponseMessage response)
        {
            response = null;
            var operationResult = _memcachedClient.ExecuteGet <byte[]>(key.HashBase64);

            if (operationResult.HasValue)
            {
                var ms = new MemoryStream(operationResult.Value);
                response = _serializer.DeserializeToResponseAsync(ms).Result;
            }

            return(operationResult.HasValue);
        }
Exemple #3
0
        public bool TryGetValue(CacheKey key, out TimedEntityTagHeaderValue eTag)
        {
            eTag = null;
            var operationResult = _memcachedClient.ExecuteGet <string>(key.HashBase64);

            if (!operationResult.Success || !operationResult.HasValue ||
                string.IsNullOrEmpty(operationResult.Value))
            {
                return(false);
            }

            var value = operationResult.Value;

            if (!TimedEntityTagHeaderValue.TryParse(value, out eTag))
            {
                return(false);
            }

            return(true);
        }
Exemple #4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            btnstuff.Click += (s, args) =>
            {
                var melClient = new SmtpClient();
                melClient.Send("*****@*****.**", "*****@*****.**", "Bienvenue tygogoz", "contenu de test");
            };

            btnTestCache.Click += (s, args) =>
            {
                MemcachedClient mc = new MemcachedClient();
                if ((DateTime.Now - LastTime) > TimeSpan.FromSeconds(10))
                {
                    LastTime = DateTime.Now;
                    StoreOperationResult storeResults = (StoreOperationResult)mc.ExecuteStore(StoreMode.Set, "foo", LastTime.ToLongTimeString());
                }
                GetOperationResult getResults = (GetOperationResult)mc.ExecuteGet("foo");

                lblTestCache.Text = getResults.Value.ToString();
            };
        }
Exemple #5
0
    protected void btnMemLook(object sender, EventArgs e)
    {
        string myConn = ConfigurationManager.AppSettings["BeITMemcacheIP"].ToString();
        MemcachedClientConfiguration config = new MemcachedClientConfiguration();

        config.AddServer(myConn, 11211);
        config.Protocol = MemcachedProtocol.Binary;
        MemcachedClient client = new MemcachedClient(config);

        string keyval = key1.Text.Trim();
        Object obj    = new Object();
        var    getr   = client.ExecuteGet(keyval);

        if (client.TryGet(keyval, out obj))
        {
            Response.Write("<script>window.alert('" + obj + "');window.location.href='../Mem_RedisTest.aspx'</script>");
        }
        else
        {
            Response.Write("<script>window.alert('Key值不存在!');window.location.href='../Mem_RedisTest.aspx'</script>");
        }
    }
        private static long TestMemcachedRead(int length)
        {
            var client = new MemcachedClient();

            var sw = Stopwatch.StartNew();

            for (int i = 0; i < length; i++)
            {
                var result = client.ExecuteGet("asdfasdf" + i.ToString()).Value.ToString();
                if (result != stringToTest) throw new Exception();
            }

            sw.Stop();
            client.Dispose();
            return sw.ElapsedMilliseconds;
        }
Exemple #7
0
 public bool Contains(string cacheKey)
 {
     return(cacheClient.ExecuteGet(cacheKey).HasValue);
 }