I. If an official method is used, each parameter and server IP are configured in the Web. config. Such as:
<?xml version= "1.0"?><configuration> <configSections> <sectiongroup name= "enyim.com" > <section name= "memcached" type= "Enyim.Caching.Configuration.MemcachedClientSection, enyim.caching"/> </s ectiongroup> <section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler,log4net "/> </configSections> <enyim.com> <memcached pr Otocol= "Text" > <servers> <add address= "127.0.0.1" port= "11211"/> <add address= "127. 0.0.1 "port=" 11212 "/> <add address=" 127.0.0.1 "port=" 11213 "/> <add address=" 127.0.0.1 "port=" 1 1214 "/> </servers> <socketpool minpoolsize=", "maxpoolsize=", connectiontimeout= "00:00:05" dead timeout= "00:02:00"/> </memcached> </enyim.com> <system.web> <compilation debug= "true" ta rgetframework= "4.0"/> </system.web></configuration>
The generic class is used, and the component automatically calls the configuration inside the Web. config.
Using system;using system.collections.generic;using system.linq;using system.web;using Enyim.Caching;using enyim.caching.configuration;using enyim.caching.memcached;///<summary>///MemberHelper Summary description///</summary >public abstract class memberhelper{public memberhelper () {////TODO: Add constructor logic here//} #region Add cache///<summary>///Add cache (the key does not exist then add, there is replacement)///</summary>//<param name= "key" > Key </param>//<param name= "value" > Value </param>//<returns></returns> public static B Ool Addcache (String key, object value) {using (Memcachedclient MC = new Memcachedclient ()) { return MC. Store (Storemode.set, key, value); }} #endregion #region Add cache///<summary>///Add cache (add if key does not exist, replace if present)///</summary>//< param name= "key" > Key </param>//<param name= "value" > Value </param>///<param name= "minutes" > cache time (minutes) </param>///<returns></returns> public static bool Addcache (string key, object value, int minutes) {using (Memcachedclient MC = new Memcachedclient ()) {return MC. Store (Storemode.set, key, value, DateTime.Now.AddMinutes (minutes)); }} #endregion #region get cache//<summary>//Get Cache//</summary>//<param name= "key "> Keys </param>//<returns> return cache, not found returns null</returns> public static object GetCache (String key) {using (Memcachedclient MC = new Memcachedclient ()) {return MC. Get (key); }} #endregion #region whether the cache exists//<summary>///whether the cache exists///</summary>//<param Nam E= "key" > Keys </param>//<returns></returns> public static bool Isexists (string key) {u Sing (Memcachedclient mc = new Memcachedclient ()) {return MC. Get (key)! = NULL; }} #endregion #region Delete cache (False if key does not exist)///<summary>///delete cache (returns False if key does not exist)//< ;/summary>//<param name= "key" > Keys </param>//<returns> Success: True failed:false</returns> Publ IC static bool Delcache (string key) {using (Memcachedclient MC = new Memcachedclient ()) {RET Urn MC. Remove (key); }} #endregion #region empty cache//<summary>//Empty cache///</summary> public static void Flus Hcache () {using (Memcachedclient MC = new Memcachedclient ()) {MC. Flushall (); }} #endregion}
Two. If you do not want to configure the Web. config, use the following generic class.
Using system;using system.collections;using system.collections.generic;using system.linq;using System.Net;using System.web;using enyim.caching;using enyim.caching.configuration;using enyim.caching.memcached;///<summary> Memberhelper Summary description///</summary>public abstract class memberhelper{public memberhelper () {// TODO: Add constructor logic here//} #region Create memcache client///<summary>//Create Memcache client///</SU mmary>//<param name= "serverlist" > Service list </param>//<returns></returns> private Stati C memcachedclient Createserver (list<ipendpoint> serverlist) {memcachedclientconfiguration config = new Me Mcachedclientconfiguration ();//Create configuration parameter for (int i = 0; i < Serverlist.count; i++) {config. Servers.add (New System.Net.IPEndPoint (Ipaddress.parse (serverlist[i). Address.tostring ()), Serverlist[i]. Port);//Add service Node} config. Protocol = MemcachedprotocOl. Text; Config. Authentication.type = typeof (Plaintextauthenticator);//set Authentication Mode CONFIG. authentication.parameters["userName"] = "UID";//user Name parameter config. authentication.parameters["password"] = "pwd";//password parameter memcachedclient mac = new memcachedclient (config);//Create Client return mac; } #endregion #region Add cache///<summary>///Add cache (add if key does not exist, replace if present)///</summary>//<param Name= "serverlist" > Server list </param>//<param name= "key" > Key </param>///<param Name= "value" > Value </param>///<returns></returns> public static bool Addcache (list<ipendpoint> serverlist, S Tring key, Object value) {using (Memcachedclient MC = Createserver (serverlist)) {return Mc.s Tore (Storemode.set, key, value); }} #endregion #region Add cache///<summary>///Add cache (add if key does not exist, replace if present)///</summary>//< param name= "serverlist" > Server list </param&Gt <param name= "key" > Key </param>//<param name= "value" > Value </param>//<param name= "minute S "> Cache time (minutes) </param>//<returns></returns> public static bool Addcache (list<ipendpoint> Serverlist,string key, object value, int minutes) {using (Memcachedclient MC = Createserver (serverlist)) {return MC. Store (Storemode.set, key, value, DateTime.Now.AddMinutes (minutes)); }} #endregion #region get cache//<summary>//Get Cache//</summary>//<param name= "Ser Verlist "> Server list </param>//<param name=" key "> Key </param>///<returns> return cache, return null< if not found /returns> public static Object GetCache (list<ipendpoint> serverlist,string key) {using (MEMCACHEDCL ient mc = createserver (serverlist)) {return MC. Get (key); }} #endregion whether the cache exists for the #region//<summary>// </summary>//<param name= "serverlist" > Server list </param>//<param name= "key" > Key </param >//<returns></returns> public static bool Isexists (list<ipendpoint> serverlist,string key) {using (Memcachedclient MC = Createserver (serverlist)) {return MC. Get (key)! = NULL; }} #endregion #region Delete cache (False if key does not exist)///<summary>///delete cache (returns False if key does not exist)//</summ ary>//<param name= "serverlist" > Server list </param>//<param name= "key" > Key </param>///&L T;returns> success: True failure:false</returns> public static bool Delcache (list<ipendpoint> serverlist, string Key) {using (Memcachedclient MC = Createserver (serverlist)) {return MC. Remove (key); }} #endregion #region empty cache//<summary>//Empty cache///</summary>//<param name= "Ser Verlist "> Server list </param> public static void Flushcache (List<ipendpoint> serverlist) {using (Memcachedclient MC = Createserver ( serverlist)) {MC. Flushall (); }} #endregion}
Memcached generic class (based on enyim.com Memcached Client)