About distributed Cache memcached detailed

Source: Internet
Author: User
Tags memcached memcached windows

About distributed Cache memcached detailed

Libevent event mechanism

Memcached based on Libevent event processing, described in the relevant information, Libevent is a library, it will Linux Epoll, BSD-type operating system Kqueue and other event processing functions packaged into a unified interface. O (1) performance can be played even if the number of connections to the server increases.

memcached server, the cache data are key-value hash table memory storage, the maximum key is not more than 250 characters, the maximum value of the default does not exceed 1m, so restart the program and the server will cause data loss, but it will consume a lower CPU, Therefore, you can use a server that is large in general memory as the memcached server. After the content exceeds the size of the cache, the unused data is removed using LRU, with the least recent algorithm, and the default timeout is 30 days. memcached servers do not communicate with each other, distributed algorithms on the client, the client chooses to store the data to which memcached server, For example,. NET client, the default is to use key hashcode to model memcached server number of values, as the selection of the current Key-value storage server, of course, the system is large enough, you can write more complex and for specific projects to do the algorithm.

Now, based on the. NET client interface, you can write a demo code directly, as follows:

1 static void Main (string[] args)
2
3 {
4
5//Can be configured via app.config
6
7 string[] serverlist = {"127.0.0.1:11211"};
8
9//More as follows:
10
One//string[] serverlist = {"127.0.0.1:11211,192.168.1.1:11211"};
12
13
14
Sockiopool pool = sockiopool.getinstance ();
16
Pool.setservers (serverlist);
18
19
20
//pool.initconnections = 3;
22
//pool.minconnections = 3;
24
//pool.maxconnections = 5;
26
27
28
//pool.socketconnecttimeout = 1000;
30
//pool.sockettimeout = 3000;
32
33
34
//pool.maintenancesleep = 30;
36
Panax Notoginseng//pool.failover = true;
38
//pool.nagle = false;
40
41
42
Pool.initialize ();
44
45
46
memcachedclient mc = new Memcachedclient ();
48
Mc.enablecompression = false;
50
51
52
//mc.flushall ();
54
55
56
string input = "test of string";
58
Mc.set ("foo", input);
60
The string s = (string) mc.get ("foo");
62
63
64
Mc.add ("foo", "Foo2");
66
67
68
Datas d = new Datas ();
70
D.A = 2;
72
D.B = "222222";
74
75
76
//D.C = new Byte[1024*1024*2];
78
79
80
Bayi Mc.set ("CLASSV", D);
82
83
84
Datas D2 = (datas) mc.get ("CLASSV");
86
87
88
Console.readkey ();
90
91}


Different versions of the client, the interface is not the same, but the idea is basically the same, Sockiopool set the server list, the agent has some comments, you can refer to. Memcachedclient create an object, it can be used, it provides get,set,add,delete, and other methods, here need to note that if the same key already has a value, and then add will not be modified, the method will return false, indicating that the server was not inserted. Therefore, it is recommended to use set, like the server add value due to the maximum default 1m limit, so more than 1m, will also insert unsuccessful, return false

Below I take the Windows Memcached server and the. NET memcached client as an example, describes its installation and use.

I use the Windows memcached1.2.6 version, which can be downloaded in http://code.jellycan.com/memcached/, the HTTPS tutorial for. NET clients://sourceforge.net/ Open source client under projects/memcacheddotnet/, of course, can also refer to the Protocol interface of Memcached server, write the custom client.

Download Good memcached server, extract to such as d:memcached-1.2.6, in the cmd command console, input d:memcachedmemcached.exe-d Install installs the memcached Windows service, then enters d:memcachedmemcached.exe-d start to start the service, the client can connect, the default port is 11211, the maximum memory is 64m, generally will modify, Enter D:memcachedmemcached.exe-p 12345-m 1024-d start, where D-install is installed as Windows service, the maximum memory (MB) used by-M, the default 64m, and the ip,-c maximum concurrent connection of-L-bound Number, default 1024, but generally does not work, need to modify the registry, find Hkey_local_machinesystemcurrentcontrolsetservicesmemcached server in the registry, find ImagePath entry, After the "D:memcachedmemcached.exe"-D runservice Plus-P 11111-m 1024-c 2048, the port is changed to 11111 after the reboot and the maximum memory is 1024m.

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.