Пример #1
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);
            }
        }
Пример #2
0
        public virtual void CASTest()
        {
            using (MemcachedClient client = GetClient())
            {
                // store the item
                var r1 = client.Cas(StoreMode.Set, "CasItem1", "foo");

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

                // 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.AreEqual(r1.Cas, r2.Cas, "Cas values do not match.");

                var r3 = client.Cas(StoreMode.Set, "CasItem1", "bar", r1.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'.");
            }
        }
Пример #3
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();
            }
        }
        private ErrorTypes AddToMCWithCas(string sKey, TaskResultData oTast, ulong cas)
        {
            ErrorTypes oError = ErrorTypes.NoError;

            try
            {
                using (MemcachedClient oMc = new MemcachedClient(m_oMcConfig))
                {
                    string sDataToStore = null;

                    XmlSerializer oXmlSerializer = new XmlSerializer(typeof(TaskResultData));
                    using (StringWriter oStringWriter = new StringWriter())
                    {
                        oXmlSerializer.Serialize(oStringWriter, oTast);
                        sDataToStore = oStringWriter.ToString();
                    }

                    if (cas != 0)
                    {
                        oMc.Cas(StoreMode.Set, sKey, sDataToStore, m_oTtl, cas);
                    }
                    else
                    {
                        oMc.Store(StoreMode.Set, sKey, sDataToStore, m_oTtl);
                    }
                }
            }
            catch
            {
                oError = ErrorTypes.TaskResult;
            }

            return(oError);
        }
Пример #5
0
        //cas test
        public void CasTest()
        {
            MemcachedClient client = GetClient();
            var             r1     = client.Cas(StoreMode.Set, "TestCas", "Done");

            HandleLogs("[Cmd=Cas]TestCas:" + client.Get("TestCas").ToString());
        }
Пример #6
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();
        }
Пример #7
0
 public static CasResult <bool> Cas(string key, object value, StoreMode mode = StoreMode.Set)
 {
     if (string.IsNullOrEmpty(key))
     {
         key = GetUniqueKey();
     }
     return(client.Cas(mode, key, value));
 }
Пример #8
0
        //test Prepend
        public void PrependTest()
        {
            MemcachedClient client = GetClient();
            var             r1     = client.Cas(StoreMode.Set, "CasPrepend", "ool");
            var             r2     = client.Prepend("CasPrepend", r1.Cas, new System.ArraySegment <byte>(new byte[] { (byte)'f' }));

            HandleLogs("[Cmd=Prepend]CasPrepend:" + client.Get("CasPrepend").ToString());
        }
Пример #9
0
        static void Main(string[] args)
        {
            //   log4net.Config.XmlConfigurator.Configure();


            var mbcc = new MemcachedClientConfiguration();

            mbcc.SocketPool.ReceiveTimeout = new TimeSpan(0, 0, 2);
            mbcc.SocketPool.DeadTimeout    = new TimeSpan(0, 0, 10);

            mbcc.AddServer("localhost:11211");

            var client = new MemcachedClient(mbcc);
            var item1  = client.Get("item10");



            if (item1 == null)
            {
                byte[] bytes = File.ReadAllBytes(@"d:\VPS_CV_DINH VAN TUAN.doc");

                var item10 = client.Cas(StoreMode.Set, "item10", bytes);
                Console.WriteLine("tao lai cache:" + item10);
            }
            else
            {
                //File.WriteAllBytes("Foo1.doc", (byte[])item1); // Requires System.IO
                Console.WriteLine("Gia tri tu cache: " + item1);
            }


            //var item2 = client.Cas(StoreMode.Set, "item2", 2);

            //var add1 = client.Cas(StoreMode.Add, "item1", 4);

            //Console.WriteLine(add1.Result);
            //Console.WriteLine(add1.Cas);
            //Console.WriteLine(add1.StatusCode);

            Console.WriteLine("item1 = " + item1);


            //mre.Set();

            //client.Sync("item1", item1.Cas, SyncMode.Mutation);
            Console.WriteLine("Changed");

            Console.ReadLine();
        }
Пример #10
0
        static void Main(string[] args)
        {
            MemcachedClient client = new MemcachedClient();



            client.Store(StoreMode.Set, "test", 10, TimeSpan.FromMinutes(20));

            client.Cas(StoreMode.Set, "t1", 1, TimeSpan.FromMinutes(20), 1);

            int val = client.Get <int>("test");

            int v = client.Get <int>("t1");

            Console.WriteLine(val);
            Console.WriteLine(v);

            Console.ReadLine();
        }
Пример #11
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();
        }
        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.");
            }
        }
Пример #13
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 bool CheckAndSet(string key, object value, ulong cas)
 {
     return(Execute(() => _client.Cas(StoreMode.Replace, key, new MemcachedValueWrapper(value), cas).Result));
 }
Пример #15
0
        private ErrorTypes AddToMCWithCas(string sKey, TaskResultData oTast, ulong cas)
        {
            ErrorTypes oError = ErrorTypes.NoError;
            try
            {
                using (MemcachedClient oMc = new MemcachedClient(m_oMcConfig))
                {
                    string sDataToStore = null;

                    XmlSerializer oXmlSerializer = new XmlSerializer(typeof(TaskResultData));
                    using (StringWriter oStringWriter = new StringWriter())
                    {
                        oXmlSerializer.Serialize(oStringWriter, oTast);
                        sDataToStore = oStringWriter.ToString();
                    }

                    if (cas != 0)
                        oMc.Cas(StoreMode.Set, sKey, sDataToStore, m_oTtl, cas);
                    else
                        oMc.Store(StoreMode.Set, sKey, sDataToStore, m_oTtl);
                }
            }
            catch
            {
                oError = ErrorTypes.TaskResult;
            }
                
            return oError;
        }
Пример #16
0
 public CasResult <bool> CheckAndSet(StoreMode mode, string key, object value)
 {
     return(_client.Cas(_modeLookup[mode], key, value));
 }
Пример #17
0
 public void Add(string key, object entity)
 {
     _client.Cas(StoreMode.Set, key, entity);
 }
Пример #18
0
 public bool CheckAndSet(string key, object value, ulong cas)
 {
     return(Execute <bool>(() => _client.Cas(StoreMode.Replace, key, value, cas).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();
            }
        }