The actual use of memcached ——. NET in the code

Source: Internet
Author: User
Tags delete key failover set socket log4net

This article was taken from: http://blog.csdn.net/dyllove98/article/details/9115947

Memcached Installation:
============================================================================

1 Extracting files to c:\memcached

2 command line input ' c:\memcached\memcached.exe-d install '

3 command line input ' c:\memcached\memcached.exe-d start ', the command starts memcached, the default listener port is 11211

Note:

If the following error occurs

"Failed to install service or service already installed"

Workaround:

Install the administrator, first find out the original file of Cmd.exe

Right-click to run as Administrator, then OK (user under Win7).

If you are downloading a binary version, you can run it directly, and you can add parameters to set it up.


Common settings:
-P <num> Monitored ports
-L <ip_addr> connected IP address, default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown Close the running memcached service
-D Install memcached service
-d Uninstall Uninstall memcached service
-U <username> Run as <username> (only valid when run as root)
-M <num> maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-C <num> Maximum number of simultaneous connections, default is 1024
-F <factor> block size growth factor, default is 1.25
-N <bytes> Minimum allocated space, key+value+flags default is 48
-H Display Help


memcached. NET client Calls:
===============================================================================

One

2) NET memcached Client Library

: https://sourceforge.net/projects/memcacheddotnet


Memcached Client class Library
Where the client class library includes the following DLLs:

Memcached.ClientLibrary.dll
ICSharpCode.SharpZipLib.dll
Log4net.dll

Put this three DLL into the bin directory, referencing Memcached.ClientLibrary.dll in the project

Introduce namespaces using Memcached.clientlibrary when used

Second, memcached use log4net, so we first configure log4net (this link can not do)
Locate the configsections node in Web. config and add the following
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/>

In addition to the configsections node, add the following:

[CSharp]View Plaincopyprint?
  1. <log4net>
  2. <appender name="Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
  3. <param name="File" value="logfiles/"/>
  4. <param name="Appendtofile" value="true"/>
  5. <param name="maxsizerollbackups" value="ten"/>
  6. <param name="staticlogfilename" value="false"/>
  7. <param name="Datepattern" value="yyyy-mm-dd". txt""/>
  8. <param name="Rollingstyle" value="Date"/>
  9. <layout type="log4net. Layout.patternlayout ">
  10. <param name="Conversionpattern" value="%d{yyyy-mm-dd HH:mm:ss} [%thread]%-5level%logger%NDC-%message% NewLine "/>
  11. </layout>
  12. </appender>
  13. <appender name="Consoleappender" type= "log4net. Appender.consoleappender ">
  14. <layout type="log4net. Layout.patternlayout ">
  15. <param name="Conversionpattern" value="%d{yyyy-mm-dd HH:mm:ss} [%thread]%-5level%logger%NDC-%message% NewLine "/>
  16. </layout>
  17. </appender>
  18. <root>
  19. <level value="All"/>
  20. <appender-ref ref="Rollinglogfileappender"/>
  21. <appender-ref ref="Consoleappender"/>
  22. </root>
  23. <logger name="Memcached.clientlibrary" >
  24. <level value="WARN"/>
  25. </logger>
  26. </log4net>

Third, initialize the Sockiopool

Sockiopool is a socket connection pool provided by the memcached client, which, in layman's words, is the object that exchanges data with the memcached server side. Sockiopool is initialized once when the application is started.
(usually written in the static too structure is OK)

Server-side List

[CSharp]View Plaincopyprint?
  1. String[] ServerList = { "127.0.0.1:11211"};
  2. Initialize Pool
  3. Sockiopool sock = Sockiopool.getinstance ();
  4. Sock. Setservers (serverlist); //Add server list
  5. Sock. Initconnections = 3; //Set initial number of connection pools
  6. Sock. Minconnections = 3; //Set minimum number of connections
  7. Sock. MaxConnections = 5; //Set the maximum number of connections
  8. Sock. socketconnecttimeout = 1000;             //Set the socket timeout for the connection.
  9. Sock. Sockettimeout = 3000; //Set socket timeout read
  10. Sock. Maintenancesleep = 30; //Set the sleep time for the maintenance thread to run.             If set to 0, then the maintenance thread will not start;
  11. Gets or sets the fault flag for the pool.
  12. If this flag is set to true then the socket connection fails,
  13. An attempt is made to return a socket from another server if it exists.
  14. If set to false, a socket is obtained if one exists. Otherwise, NULL is returned if it cannot connect to the requested server.
  15. Sock.            Failover = true;             //If False, the Nagle algorithm is closed on all created sockets.
  16. Sock.             Nagle = false;
  17. Sock. Initialize ();


Iv. Use of memcached

[CSharp]View Plaincopyprint?
  1. memcachedclient mc = New Memcachedclient ();
  2. Mc. EnableCompression = true; //Whether to enable compressed data
  3. Mc. Set (Key,val); //Set key value
  4. Mc. Keyexists (key) //key is stored
  5. Mc. Get (key) //Get a key value
  6. Mc. Delete (key); //Delete key value
  7. How far a simple example
  8. Using System;
  9. Using System.Collections.Generic;
  10. Using System.Linq;
  11. Using System.Web;
  12. Using Memcached.clientlibrary;
  13. Namespace Cachedapp
  14. {
  15. public class Mcache
  16. {
  17. memcachedclient mc = New Memcachedclient (); Initializing a client
  18. static Mcache ()
  19. {
  20. string[] serverlist = { "172.18.30.33:11211"}; //Server list, can be multiple
  21. Sockiopool pool = sockiopool.getinstance ();
  22. //Modify the following parameters according to the actual situation
  23. Pool. Setservers (serverlist);
  24. Pool. Initconnections = 3;
  25. Pool. Minconnections = 3;
  26. Pool. MaxConnections = 5;
  27. Pool. socketconnecttimeout = 1000;
  28. Pool. Sockettimeout = 3000;
  29. Pool. Maintenancesleep = 30;
  30. Pool.  Failover = true;
  31. Pool.  Nagle = false;
  32. Pool. Initialize (); //Initialize the pool for memcache servers
  33. }
  34. public Object get (string key)
  35. {
  36. return MC.        Get (key);
  37. }
  38. public Object set (string key,string val)
  39. {
  40. return MC.  Set (Key,val);
  41. }
  42. }
  43. }

The actual use of memcached ——. NET in the code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.