Using FreeBSD and memcached to create a distributed cache server

Source: Internet
Author: User
Tags first string
1. Install memcached
Yihui # cd/usr/ports/databases/memcached

Yihui # make install clean

 

With pkg_info, you can see that the installed package list contains "memcached-1.2.3 high-performance distributed memory object Cache System"


2. Run memcached

Memcached <option>
-D runs in daemon mode (similar to background services in Windows). If this option is not specified, the console stops running memcached (CTRL + C)
-L specifies the IP address that can access the cache service. If this parameter is not specified, all local IP addresses are used. This is similar to the restrictions on website access in IIS.
-P <TCP portnum> specifies the TCP port used for listening. The default value is 11211.
-U root specifies the user name to run it. Generally, this option is not required. However, if you are currently logged on as the root user, you must specify this option. We recommend that you do not use this option to run it as the root user.
-U <UDP portnum> specifies the UDP port used for listening. The default value is 11211. Note that the value is in uppercase.
-M <num> specifies the maximum memory used. The unit is m and the default value is 64 m.

The simplest run: memcached-D, run in daemon mode, and listen to the tcp1_11 ports of all IP addresses.
The most common run: memcached-D-P 11211-M 2048, with 2 GB memory (custom), since it is used as a cache server, of course, the better the memory to specify!

Check whether it is running
PS-axu | grep memcached

Telnet 10.0.5.115 11211. If the connection succeeds, you can proceed to the next step.

Stop running: Sorry, I don't know how to stop it. I only use
PS-axu | grep memcached, get the process number, and then kill the process number forcibly.


3. Download the C # Client

  • Https://sourceforge.net/projects/memcacheddotnet/
  • Http://www.codeplex.com/EnyimMemcached/-client developed in. NET 2.0 keeping performance and extensibility in mind. (supports consistent hashing .)
  • Http://code.google.com/p/beitmemcached/-recommended to differentiate multiple instances and automatically serialize objects

Each of these three client APIS is a VS 2005 project. You 'd better download them and check them. You can also modify them, for example, change the hash algorithm in to a consistent hash algorithm (if you have changed the hash algorithm, please copy it to me ).

 

Sample Code (the downloaded code is similar to this one. I added a complex Class Object test ):

Beitmemcached sample code
Using system;
Using system. Collections. Generic;

Namespace Beit. memcached {
Class Example {

[Serializable]
Public class AB {
Public String;
Public int B;
Public ABS absss;
}
[Serializable]
Public class ABS {
Public String AAA = "";
}
Public static void main (string [] ARGs ){

// Initialize the server pool. You must specify the Instance name and server group.
// The instance name is mainly used to classify the data that the application needs to cache. This facilitates management and improves performance.
// For example, a session can have a group of memcached servers, and user data is stored in another group of servers.
// The server is a logical concept, and IP + port represents one.
Memcachedclient. Setup ("mycache", new string [] {"10.0.5.115: 11211 "});
Memcachedclient. Setup ("myothercache", new string [] {"10.0.5.115: 11212", "10.0.5.115: 11213 "});

Memcachedclient cache2 = memcachedclient. getinstance ("mycache ");

Cache2.sendreceievetimeout = 5000;
Cache2.minpoolsize = 1; // minimum socket connection pool size
Cache2.maxpoolsize = 5; // maximum socket connection pool size

Console. Out. writeline ("storing some items .");
Cache2.set ("mystring", "The quick brown fox jumped over the lazy dog .");
Cache2.set ("myarray", new string [] {"This is the first string.", "this is the second string ."});
Cache2.set ("myinteger", 4711 );
Cache2.set ("mydate", new datetime (2008, 02, 23 ));

AB obj = new AB ();
OBJ. A = "sdfasdfasfdda People's Republic of China ";
OBJ. B = 435;
OBJ. absss = new ABS ();
OBJ. absss. AAA = "adfasdfasdfas ";
Cache2.set ("myobj", OBJ );

// Get a string
String str2 = cache2.get ("mystring") as string;
String [] array2 = cache2.get ("myarray") as string [];
Object [] result2 = cache2.get (New String [] {"myinteger", "mydate"}); // obtain multiple key values at a time

AB obj2 = cache2.get ("myobj") as AB;

Console. writeline (str2 );
Console. writeline (array2 [0]);
Console. writeline (datetime) (result2 [1]). tostring ());
Console. writeline (obj2.a );
Console. writeline (obj2.absss. AAA );

Console. Readline ();
Return;
}
}
}

 

These articles may disappoint some of our friends. Due to the limited level of writing for the first time, please forgive me!

Now, I hope to attract everyone's interest. I can exchange and learn together here. I will also record my experience and experiences as I continue to learn!

In addition, I have had a deep understanding over the past few days that writing a blog can actually deepen my understanding and memory. Do you feel the same way?

 

A wave of essay (http://www.cnblogs.com/yihuiso), reprint please leave this information!

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.