Introduction to Distributed Cache usage Memcache

Source: Internet
Author: User
Tags log4net

Reference URL: http://www.jb51.net/article/66525.htm

Overview

Memcache is a set of open source distributed cache systems. Consists of a server and a client, running on one or more servers in a daemon (listener) mode, receiving client connections and operations at any time. Memcache primarily caches data objects into memory by maintaining a unified, huge hash table in memory. The simple thing is to call the data into memory and then read it from memory, which greatly improves the reading speed. Memcache stores objects into memory based on a hashmap that stores key/value pairs. Memcache is written in C, but the client can be written in any language and communicates with the daemon through the memcached protocol.

Characteristics:
• There is no limit to the amount of item data that can be saved in memcached, as long as the memory is sufficient.
memcached Single process in 32-bit systems with a maximum memory of 2G, if the 64-bit system is not limited, this is because the 32-bit system restricts the single process to use up to 2G of memory, to use more memory, can be divided into multiple ports to open multiple memcached processes.
• Up to 30 days of data expiration, set to permanent will also expire at this time, constant Realtime_maxdelta
• Single item maximum data is 1MB, more than 1MB data is not stored, constant Power_block 1048576 is controlled

Install memcache under Windows

After you understand some basic information about Memcache, try to install the Memcache service side under Windows.
1. First download the memcache installation file: "Install package". Installation package will have x64 and x86 two folders, according to the operating system select an open will find Memcached.exe. This file cannot be directly double-clicked to run the installation and needs to be installed by CMD.
2. Installation steps:

Steps:

1. Window +r: input cmd
2. Make G-Disk: Enter G:
3. Install the memcached for window 32/64 installation directory: Enter the CD ck\memcached_en32or64\x64
4. Install memcached: Enter memcached-d install
5. Start Service: Enter memcached-d start

After you start the service, you can see Memcached.exe in the Windows process.

memcached-d start|stop|shutdown|restart|uninstall|install Start | stop | shutdown | restart | uninstall | install.

The installation steps are not very complex. First: Locate the file (Memcached.exe) path. Second: memcached-d install is OK.

Basic Default parameter Description:

-P Listening Port
-L 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 Run as (only valid when running as root)
-m maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-c Maximum number of simultaneous connections, default is 1024
-F Block size growth factor, default is 1.25
-N Minimum allocated space, key+value+flags default is 48
-H Display Help

After the server operation is complete, we can telnet to the service under one of the services tests. (If you are prompted that the Telnet command does not exist, you need to go to the control Panel to turn on the Windows Tel Service feature, Win7 's open tel function steps are: "Control Panel", "Programs and Features", "turn window features on or off", Then find and tick tel related. Other Window System steps are similar. )

Test whether Telnet is running correctly telnet 172.21.0.192 11211

Press CTRL +] to start the callback function, or you will not see the input information. The callback function starts successfully as follows:

Then press ENTER to input the parameter stats:

Setup and test work is complete.

Instance Code

There are many C # versions of the memcached client program. Here we are using the Memcached.ClientLibrary.dll client call method, which requires two DLLs:

Memcached.ClientLibrary.dll (Memcached client class library)

Log4net.dll (Log4net is providing logging for memcached) DLL: "Click to download"

This two DLL is referenced in the project, and a series of configuration work is required after referencing Log4net.dll. In the previous blog, there is an introduction to log4net configuration.

"Log4net log configuration [included source]"

Note: Memcached.ClientLibrary.dll and Log4net.dll have version correspondence.

2. If you use the WinForm or console application to separate the Log4net profile, and write it in App. config, you need to set it up a little bit.

Log4net.config property "Copy to Output directory": "Copy Always". Otherwise, the corresponding configuration information can not be found in the bin directory to produce an error.

Memcache stores objects into memory based on a hashmap that stores key/value pairs. So we can understand the key-value pairs that are primarily in operation HashMap.

Here are some simple actions [Add, delete, change, check, set expiration Time]:

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 //参数设置string SockIOPoolName = "Test_SockIOPoolName";string[] MemcacheServiceList = { "172.21.0.192:11211" };//设置连接池SockIOPool SPool = SockIOPool.GetInstance(SockIOPoolName);SPool.SetServers(MemcacheServiceList);SPool.Initialize();//实例化ClientMemcachedClient MClient = new MemcachedClient();MClient.PoolName = SockIOPoolName;Console.WriteLine("1.创建memcache缓存Hello World");MClient.Add("Key1001", "Hello World");Console.WriteLine("2.查询缓存信息{0}", MClient.Get("Key1001"));Console.WriteLine("3.修改memcache缓存Hello World");MClient.Set("Key1001", "Hello World - 修改版");Console.WriteLine("4.查询缓存信息{0}", MClient.Get("Key1001"));if (MClient.KeyExists("Key1001")){ Console.WriteLine("5.删除memcache缓存"); MClient.Delete("Key1001");}if (MClient.KeyExists("Key1001")) Console.WriteLine(MClient.Get("Key1001"));else Console.WriteLine("6.删除已删除"); Student stud = new Student() { id = "10001", name = "张三" };MClient.Add("student", stud);Student Get_stud = MClient.Get("student") as Student;Console.WriteLine("6.缓存实体对象:{0} {1}", Get_stud.id, Get_stud.name);MClient.Add("Key1002", "我已设置过期时间1分钟", DateTime.Now.AddMinutes(1));while (true){ if (MClient.KeyExists("Key1002")) { Console.WriteLine("key:Key1002 Value:{0},当前时间:{1}", MClient.Get("Key1002"), DateTime.Now); Thread.Sleep(20000); } else { Console.WriteLine("key:Key1002 我已过期,当前时间:{0}", DateTime.Now); break; }}

Output Result:

memcached Cache expiration Mechanism:

Lazy Delete: It does not provide a mechanism for monitoring data expiration, but is lazy, when the query to a key data, if the expiration of the direct discard.

For example key key1002 on 2015-04-09 13:54:18 I set his value to: "I have set the expiration time to 1 minutes." His expiry time is 1 minutes. The data should expire at 2015-04-09 13:55:18, but the data will still be saved in memory, rather than when the client requests the data to determine whether the data is out of date. After expiration, the immediate deletion returns NULL. If the memory is full, the memcached will not use the expiration cache record for the longest time to delete, make room to continue to use.

memcached Distributed Storage

The following assumes that the memcached server has node1~node3 three, the application to save the key named "Tokyo", "Kanagawa", "Chiba", "Saitama", "Gunma" data.

First add "Tokyo" to the memcached. When "Tokyo" is passed to the client library, the client-implemented algorithm determines the memcached server that holds the data based on the "key". When the server is selected, it commands it to save "Tokyo" and its values.

Similarly, "Kanagawa", "Chiba", "Saitama", "Gunma" are the first to select the server and then save. Next, you get the saved data. The key "Tokyo" To get is also passed to the library. The function library selects the server according to the "key" by the same algorithm as when the data is saved. Using the same algorithm, you can select the same server as you saved, and then send a GET command. As long as the data is not deleted for some reason, the saved value can be obtained.

This allows the memcached to be distributed by saving different keys to different servers. memcached server, the key will be scattered, even if a memcached server failure can not connect, nor affect the other cache, the system can continue to run. (Reference: memcached comprehensive analysis)

?

1234567891011121314151617181920212223242526272829303132333435 //参数string[] MemcacheServiceList = { "172.21.0.192:11211", "172.21.0.28:11211", "172.21.13.246:11211" };//设置连接池SockIOPool SPool = SockIOPool.GetInstance();SPool.SetServers(MemcacheServiceList);SPool.Initialize();MemcachedClient MClient = new MemcachedClient();MClient.FlushAll();int count = 5;var time = Stopwatch.StartNew();for (int i = 0; i < count; i++){ MClient.Add(i.ToString(), "value" + i);}Console.WriteLine("memcached缓存创建成功。耗时:{0}",time.ElapsedTicks);time = Stopwatch.StartNew();for (int i = 0; i < count; i++){ if (MClient.KeyExists(i.ToString())) { Console.WriteLine("key:{0}.value:{1}", i, MClient.Get(i.ToString())); } else { Console.WriteLine("--------未能查询到数据key:{0}--------",i); }}Console.WriteLine("memcached缓存数据查询完成。耗时:{0}", time.ElapsedTicks);SPool.Shutdown();

Introduction to Distributed Cache usage Memcache

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.