public void CASTest()
        {
            using (MemcachedClient client = GetClient())
            {
                // store the item
                var r1 = client.Store(StoreMode.Set, "CasItem1", "foo");

                Assert.IsTrue(r1, "Initial set failed.");

                // get back the item and check the cas value (it should match the cas from the set)
                var r2 = client.GetWithCas <string>("CasItem1");

                Assert.AreEqual(r2.Result, "foo", "Invalid data returned; expected 'foo'.");
                Assert.AreNotEqual(0, r2.Cas, "No cas value was returned.");

                var r3 = client.Cas(StoreMode.Set, "CasItem1", "bar", r2.Cas - 1);

                Assert.IsFalse(r3.Result, "foo", "Overwriting with 'bar' should have failed.");

                var r4 = client.Cas(StoreMode.Set, "CasItem1", "baz", r2.Cas);

                Assert.IsTrue(r4.Result, "foo", "Overwriting with 'baz' should have succeeded.");

                var r5 = client.GetWithCas <string>("CasItem1");
                Assert.AreEqual(r5.Result, "baz", "Invalid data returned; excpected 'baz'.");
            }
        }
Exemplo n.º 2
0
        public void CASTest()
        {
            using (MemcachedClient client = GetClient())
            {
                // store the item
                var r1 = client.Store(StoreMode.Set, "CasItem1", "foo");

                Assert.True(r1, "Initial set failed.");

                // get back the item and check the cas value (it should match the cas from the set)
                var r2 = client.GetWithCas <string>("CasItem1");

                Assert.Equal("foo", r2.Result);
                Assert.NotEqual((ulong)0, r2.Cas);

                var r3 = client.Cas(StoreMode.Set, "CasItem1", "bar", r2.Cas - 1);

                Assert.False(r3.Result, "Overwriting with 'bar' should have failed.");

                var r4 = client.Cas(StoreMode.Set, "CasItem1", "baz", r2.Cas);

                Assert.True(r4.Result, "Overwriting with 'baz' should have succeeded.");

                var r5 = client.GetWithCas <string>("CasItem1");
                Assert.Equal("baz", r5.Result);
            }
        }
        public object Get(string key, out ulong ucas)
        {
            var result = _client.GetWithCas <MemcachedValueWrapper>(key);

            if (result.Result != null)
            {
                ucas = result.Cas;
                return(result.Result);
            }

            ucas = default(ulong);
            return(null);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            MemcachedClientConfiguration mcConfig = new MemcachedClientConfiguration();

            mcConfig.AddServer("127.0.0.1:11211");//必须指定端口
            using (MemcachedClient client = new MemcachedClient(mcConfig))
            {
                //client.Store(Enyim.Caching.Memcached.StoreMode.Set, "name", "yzk");
                //string name=(string) client.Get("name");

                //client.Store(Enyim.Caching.Memcached.StoreMode.Set, "p1", new Person { Name="dalong",Age=18});

                //Person person=(Person) client.Get("p1");

                //Console.WriteLine(person.Name+","+person.Age);

                var cas = client.GetWithCas("name");
                Console.WriteLine("按任意键继续");
                Console.ReadKey();
                var res = client.Cas(Enyim.Caching.Memcached.StoreMode.Set, "name", cas.Result + "1",
                                     cas.Cas);
                if (res.Result)
                {
                    Console.WriteLine("修改成功" + cas.Result);
                }
                else
                {
                    Console.WriteLine("被别人改了" + cas.Result);
                }
            }

            Console.ReadKey();
        }
        private ErrorTypes GetFromMCWithCas(string sKey, out TaskResultData oTast, out ulong cas)
        {
            ErrorTypes oError = ErrorTypes.NoError;

            oTast = null;
            cas   = 0;
            try
            {
                using (MemcachedClient oMc = new MemcachedClient(m_oMcConfig))
                {
                    CasResult <string> oGetData = oMc.GetWithCas <string>(sKey);

                    if (oGetData.Result != null)
                    {
                        cas = oGetData.Cas;

                        XmlSerializer oXmlSerializer = new XmlSerializer(typeof(TaskResultData));
                        using (StringReader oStringReader = new StringReader(oGetData.Result))
                        {
                            oTast = (TaskResultData)oXmlSerializer.Deserialize(oStringReader);
                        }
                    }
                }
            }
            catch
            {
                oError = ErrorTypes.TaskResult;
            }

            return(oError);
        }
Exemplo n.º 6
0
        //Cas操作
        //Cas 类似数据库的乐观锁,但是我们使用Memcached一般只是用来缓存数据,一般是不需要使用Cas的
        public static void TestCas()
        {
            MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();

            memConfig.AddServer("127.0.0.1:11211");
            using (MemcachedClient memClient = new MemcachedClient(memConfig))
            {
                memClient.Store(Enyim.Caching.Memcached.StoreMode.Set, "Name", "shanzm");

                CasResult <string> nameWithCas = memClient.GetWithCas <string>("Name");
                Console.WriteLine($"读取的数据Name={nameWithCas.Result },Cas:{nameWithCas.Cas }"); //第一个运行的程序此时的Cas=1

                Console.WriteLine("你此时在源文件中点击运行,“003Cas操作.exe”,抢先运行,点击回车进行下面的修改");            //另外的程序读取Name的Cas=2
                Console.ReadKey();

                CasResult <bool> updateWithCas = memClient.Cas(StoreMode.Set, "Name", "shanzm修改版本", nameWithCas.Cas);

                if (updateWithCas.Result)
                {
                    Console.WriteLine("Name修改成功,现在的Cas:" + updateWithCas.Cas);//另外的程序此处的Cas为3
                }
                else
                {
                    Console.WriteLine("更新失败,被其他程序抢先了,现在的Cas:" + updateWithCas.Cas);//第一个运行的程序的Cas此时为0
                }
                Console.ReadKey();
            }
        }
Exemplo n.º 7
0
        //test GetWithCas
        public void GetWithCasTest()
        {
            MemcachedClient client = GetClient();
            var             r1     = client.Store(StoreMode.Set, "GetWithCasTest", "Done");
            var             r2     = client.GetWithCas <string>("GetWithCasTest");

            HandleLogs("[Cmd=GetWithCas]CasResult:" + r2.StatusCode);
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            Person p = new Person();
            MemcachedClientConfiguration mcConfig = new MemcachedClientConfiguration();

            mcConfig.AddServer("127.0.0.1:11211");//必须指定端口
            using (MemcachedClient client = new MemcachedClient(mcConfig))
            {
                var cas = client.GetWithCas("Name");
                Console.WriteLine("按任意键继续");
                Console.ReadKey();
                var res = client.Cas(Enyim.Caching.Memcached.StoreMode.Set, "Name", cas.Result + "1", cas.Cas);
                if (res.Result)
                {
                    Console.WriteLine("修改成功");
                }
                else
                {
                    Console.WriteLine("被别人改了");
                }
            }
            Console.ReadKey();
        }
Exemplo n.º 9
0
        public void AppendCASTest()
        {
            using (MemcachedClient client = GetClient())
            {
                // store the item
                var r1 = client.Cas(StoreMode.Set, "CasAppend", "foo");

                Assert.IsTrue(r1.Result, "Initial set failed.");
                Assert.AreNotEqual(r1.Cas, 0, "No cas value was returned.");

                var r2 = client.Append("CasAppend", r1.Cas, new System.ArraySegment <byte>(new byte[] { (byte)'l' }));

                Assert.IsTrue(r2.Result, "Append should have succeeded.");

                // get back the item and check the cas value (it should match the cas from the set)
                var r3 = client.GetWithCas <string>("CasAppend");

                Assert.AreEqual(r3.Result, "fool", "Invalid data returned; expected 'fool'.");
                Assert.AreEqual(r2.Cas, r3.Cas, "Cas values r2:r3 do not match.");

                var r4 = client.Append("CasAppend", r1.Cas, new System.ArraySegment <byte>(new byte[] { (byte)'l' }));
                Assert.IsFalse(r4.Result, "Append with invalid CAS should have failed.");
            }
        }
        public void AppendCASTest()
        {
            using (MemcachedClient client = GetClient())
            {
                // store the item
                var r1 = client.Cas(StoreMode.Set, "CasAppend", "foo");

                Assert.True(r1.Result, "Initial set failed.");
                Assert.NotEqual(r1.Cas, (ulong)0);

                var r2 = client.Append("CasAppend", r1.Cas, new System.ArraySegment <byte>(new byte[] { (byte)'l' }));

                Assert.True(r2.Result, "Append should have succeeded.");

                // get back the item and check the cas value (it should match the cas from the set)
                var r3 = client.GetWithCas <string>("CasAppend");

                Assert.Equal(r3.Result, "fool");
                Assert.Equal(r2.Cas, r3.Cas);

                var r4 = client.Append("CasAppend", r1.Cas, new System.ArraySegment <byte>(new byte[] { (byte)'l' }));
                Assert.False(r4.Result, "Append with invalid CAS should have failed.");
            }
        }
Exemplo n.º 11
0
        private ErrorTypes GetFromMCWithCas(string sKey, out TaskResultData oTast, out ulong cas)
        {
            ErrorTypes oError = ErrorTypes.NoError;
            oTast = null;
            cas = 0;
            try
            {
                using (MemcachedClient oMc = new MemcachedClient(m_oMcConfig))
                {
                    CasResult<string> oGetData = oMc.GetWithCas<string>(sKey);

                    if (oGetData.Result != null)
                    {
                        cas = oGetData.Cas;

                        XmlSerializer oXmlSerializer = new XmlSerializer(typeof(TaskResultData));
                        using (StringReader oStringReader = new StringReader(oGetData.Result))
                        {
                            oTast = (TaskResultData)oXmlSerializer.Deserialize(oStringReader);
                        }
                    }
                }
            }
            catch
            {
                oError = ErrorTypes.TaskResult;
            }

            return oError;
        }
Exemplo n.º 12
0
 public static CasResult <object> GetWithCas(string key)
 {
     return(client.GetWithCas(key));
 }
Exemplo n.º 13
0
 public object Get(string key)
 {
     return(_client.GetWithCas(key).Result);
 }
        public void Cas_Study()
        {
            using (var client = new MemcachedClient())
            {
                /* 
                 * 非常重要的CAS操作 
                 */

                client.Store(StoreMode.Set, "userid", "123456");
                var result1 = client.GetWithCas("userid");
                var result2 = client.Cas(StoreMode.Set, "userid", "6543321");
                Assert.True(result2.Result);
                var result3 = client.Cas(StoreMode.Set, "userid", "123456", result1.Cas);
                Assert.False(result3.Result);

                client.FlushAll();
            }
        }
        public void Gets_Study()
        {
            using (var client = new MemcachedClient())
            {
                /* 
                 * 非常重要的CAS操作 
                 */

                client.Store(StoreMode.Set, "userid", "123456");
                var result = client.GetWithCas("userid");

                Assert.AreEqual("123456", result.Result);
                Assert.Greater(result.Cas, 0u);

                client.FlushAll();
            }
        }