PageRenderTime 83ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/DemoApp/Program.cs

https://github.com/thunderstumpges/EnyimMemcached
C# | 123 lines | 39 code | 29 blank | 55 comment | 1 complexity | dc374e6dffba3690df54271e4e548f25 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Enyim.Caching;
  5. using Enyim.Caching.Memcached;
  6. using System.Net;
  7. using Enyim.Caching.Configuration;
  8. using Membase;
  9. using Membase.Configuration;
  10. using System.Threading;
  11. namespace DemoApp
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. // create a MemcachedClient
  18. // in your application you can cache the client in a static variable or just recreate it every time
  19. // MemcachedClient mc = new MemcachedClient();
  20. // you can create another client using a different section from your app/web.config
  21. // this client instance can have different pool settings, key transformer, etc.
  22. // MemcachedClient mc2 = new MemcachedClient("memcached");
  23. // or just initialize the client from code
  24. MemcachedClientConfiguration config = new MemcachedClientConfiguration();
  25. config.Servers.Add(new IPEndPoint(IPAddress.Loopback, 11211));
  26. config.Protocol = MemcachedProtocol.Binary;
  27. config.Authentication.Type = typeof(PlainTextAuthenticator);
  28. config.Authentication.Parameters["userName"] = "demo";
  29. config.Authentication.Parameters["password"] = "demo";
  30. var mc = new MemcachedClient(config);
  31. for (var i = 0; i < 100; i++)
  32. mc.Store(StoreMode.Set, "Hello", "World");
  33. // simple multiget; please note that only 1.2.4 supports it (windows version is at 1.2.1)
  34. //List<string> keys = new List<string>();
  35. //for (int i = 1; i < 100; i++)
  36. //{
  37. // string k = "aaaa" + i + "--" + (i * 2);
  38. // keys.Add(k);
  39. // mc.Store(StoreMode.Set, k, i);
  40. //}
  41. //IDictionary<string, ulong> cas;
  42. //IDictionary<string, object> retvals = mc.Get(keys, out cas);
  43. //List<string> keys2 = new List<string>(keys);
  44. //keys2.RemoveRange(0, 50);
  45. //IDictionary<string, object> retvals2 = mc.Get(keys2, out cas);
  46. //retvals2 = mc.Get(keys2, out cas);
  47. //ServerStats ms = mc.Stats();
  48. // store a string in the cache
  49. //mc.Store(StoreMode.Set, "MyKey", "Hello World");
  50. // retrieve the item from the cache
  51. //Console.WriteLine(mc.Get("MyKey"));
  52. //Console.WriteLine(mc.Increment("num1", 1, 10));
  53. //Console.WriteLine(mc.Increment("num1", 1, 10));
  54. //Console.WriteLine(mc.Decrement("num1", 1, 14));
  55. //// store some other items
  56. //mc.Store(StoreMode.Set, "D1", 1234L);
  57. //mc.Store(StoreMode.Set, "D2", DateTime.Now);
  58. //mc.Store(StoreMode.Set, "D3", true);
  59. //mc.Store(StoreMode.Set, "D4", new Product());
  60. //mc.Store(StoreMode.Set, "D5", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  61. ////mc2.Store(StoreMode.Set, "D1", 1234L);
  62. ////mc2.Store(StoreMode.Set, "D2", DateTime.Now);
  63. ////mc2.Store(StoreMode.Set, "D3", true);
  64. ////mc2.Store(StoreMode.Set, "D4", new Product());
  65. //Console.WriteLine("D1: {0}", mc.Get("D1"));
  66. //Console.WriteLine("D2: {0}", mc.Get("D2"));
  67. //Console.WriteLine("D3: {0}", mc.Get("D3"));
  68. //Console.WriteLine("D4: {0}", mc.Get("D4"));
  69. //byte[] tmp = mc.Get<byte[]>("D5");
  70. //// delete them from the cache
  71. //mc.Remove("D1");
  72. //mc.Remove("D2");
  73. //mc.Remove("D3");
  74. //mc.Remove("D4");
  75. //ServerStats stats = mc.Stats();
  76. //Console.WriteLine(stats.GetValue(ServerStats.All, StatItem.ConnectionCount));
  77. //Console.WriteLine(stats.GetValue(ServerStats.All, StatItem.GetCount));
  78. //// add an item which is valid for 10 mins
  79. //mc.Store(StoreMode.Set, "D4", new Product(), new TimeSpan(0, 10, 0));
  80. Console.ReadLine();
  81. }
  82. // objects must be serializable to be able to store them in the cache
  83. [Serializable]
  84. class Product
  85. {
  86. public double Price = 1.24;
  87. public string Name = "Mineral Water";
  88. public override string ToString()
  89. {
  90. return String.Format("Product {{{0}: {1}}}", this.Name, this.Price);
  91. }
  92. }
  93. }
  94. }