Exemple #1
0
        // TODO: !!! routePattern implementation needs to be changed to Cas

        public void AddOrUpdate(CacheKey key, TimedEntityTagHeaderValue eTag)
        {
            // add item
            _memcachedClient.ExecuteStore(StoreMode.Set, key.HashBase64, eTag.ToString());

            // add route pattern if not there
            string keyForRoutePattern  = GetKeyForRoutePattern(key.RoutePattern);
            string keyForResourceUri   = GetKeyForResourceUri(key.ResourceUri);
            var    routePatternEntries = GetRoutePatternEntries(key.RoutePattern);
            var    resourceUriEntries  = GetResourceUriEntries(key.ResourceUri);


            if (!routePatternEntries.Contains(key.HashBase64))
            {
                var bytes = new List <byte>();
                foreach (var routePatternEntry in routePatternEntries)
                {
                    bytes.AddRange(new LengthedPrefixedString(routePatternEntry).ToByteArray());
                }
                bytes.AddRange(new LengthedPrefixedString(key.HashBase64).ToByteArray());
                _memcachedClient.ExecuteStore(StoreMode.Set, keyForRoutePattern, bytes.ToArray());
            }

            if (!resourceUriEntries.Contains(key.HashBase64))
            {
                var bytes = new List <byte>();
                foreach (var routePatternEntry in resourceUriEntries)
                {
                    bytes.AddRange(new LengthedPrefixedString(routePatternEntry).ToByteArray());
                }
                bytes.AddRange(new LengthedPrefixedString(key.HashBase64).ToByteArray());
                _memcachedClient.ExecuteStore(StoreMode.Set, keyForResourceUri, bytes.ToArray());
            }
        }
Exemple #2
0
        public T Add <T>(string cacheKey, T value, TimeSpan?expire = null, CacheItemRemovedCallback removeCallback = null)
        {
            var result = expire == null?cacheClient.ExecuteStore(StoreMode.Set, cacheKey, value)
                             : cacheClient.ExecuteStore(StoreMode.Set, cacheKey, value, expire.Value);

            return(value);
        }
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                LabelStatus.Text = "Storing";

                // Stores the same as an Enyim client, just that the nodes are already set through the _config object
                var res = _mem.ExecuteStore(StoreMode.Set, TextKey.Text, TextValue.Text);
                if (res.Success)
                {
                    #region UI Stuff

                    TextGetKey.Text  = TextKey.Text;
                    LabelStatus.Text = "Storing Success";
                    if (ProgressBarStatus.Value < 50)
                    {
                        ProgressBarStatus.Value = 50;
                    }

                    #endregion
                }
                else
                {
                    LabelStatus.Text = "Failed to store: " + res.Message;
                }
            }
            catch (Exception ex)
            {
                LabelStatus.Text = ex.Message;
            }
        }
        public void Put(object key, object value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key), "null key not allowed");
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value), "null value not allowed");
            }

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("setting value for item {0}", key);
            }

            /*bool returnOk = _client.Store(
             *  StoreMode.Set, KeyAsString(key),
             *  new DictionaryEntry(GetAlternateKeyHash(key), value),
             *  TimeSpan.FromSeconds(_expiry));*/

            var operationResult = _client.ExecuteStore(StoreMode.Set, KeyAsString(key), new DictionaryEntry(GetAlternateKeyHash(key), value),
                                                       TimeSpan.FromSeconds(_expiry));

            if (!operationResult.Success)
            {
                if (Log.IsWarnEnabled)
                {
                    Log.WarnFormat("could not save: {0} => {1} ({2})", key, value);
                }
            }
        }
        public void AddOrUpdate(CacheKey key, HttpResponseMessage response)
        {
            var ms = new MemoryStream();

            _serializer.SerializeAsync(TaskHelpers.FromResult(response), ms)
            .Wait();

            _memcachedClient.ExecuteStore(StoreMode.Set, key.HashBase64, ms.ToArray());
        }
Exemple #6
0
        protected IStoreOperationResult Store(StoreMode mode = StoreMode.Set, string key = null, object value = null)
        {
            if (string.IsNullOrEmpty(key))
            {
                key = GetUniqueKey("store");
            }

            if (value == null)
            {
                value = GetRandomString();
            }
            return(_client.ExecuteStore(mode, key, value));
        }
        private static void SetCache(string cacheKey, object value, DateTime expiresAt, StoreMode mode = StoreMode.Add)
        {
#if DEBUG
            var success = EnyimMemcachedClient.ExecuteStore(mode, cacheKey, value, expiresAt);
            if (success.Success)
            {
                return;
            }
            var message = success.Exception != null ? success.Exception.Message : success.Message;
            throw new Exception(string.Concat("写入缓存失败", cacheKey, " ", message));
#else
            EnyimMemcachedClient.Store(mode, cacheKey, value, expiresAt);
#endif
        }
Exemple #8
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 #9
0
    protected void btnMemadd(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 = key0.Text.Trim();
        object val    = value0.Text.Trim();
        double extime = double.Parse(expiret.Text.Trim());
        var    result = client.ExecuteStore(StoreMode.Set, keyval, val, DateTime.Now.AddMinutes(extime));

        if (!result.Success)
        {
            if (result.Exception != null)
            {
                throw new Exception(String.Format("Message:{0}, key:{1}",
                                                  result.Message,
                                                  keyval),
                                    result.Exception);
                //Response.Write("<script>window.alert('添加成功!');window.location.href='memcachedtest.aspx'</script>");
            }
            else
            {
                throw new Exception(
                          String.Format("Message:{0}, Code:{1}, Key:{2}",
                                        result.Message,
                                        result.StatusCode,
                                        keyval));
                //Response.Write("<script>window.alert('添加失败!');window.location.href='memcachedtest.aspx'</script>");
            }
        }
        else
        {
            Response.Write("<script>window.alert('添加成功!');window.location.href='../Mem_RedisTest.aspx'</script>");
        }
    }