Memcached Summary-"project experience"

Source: Internet
Author: User

Why do you use memcached?

The emergence of each technology must have its causes, why use memcached? Check the information on the Internet, understand that the emergence of memcache to solve the database of high concurrent access bottlenecks. When the data schema is built in single database mode, the database connection pool peaks up to 500, the program is not far away from the crash. Some small website development time does not pay attention to the performance, and so on the user accesses the quantity to be bigger, the program easily crashes.

If the client sends a request, the server accesses the relevant data from the database, which is expensive. If you take the same data from the database every time, it causes the database to be inefficient. However, if the first data are stored in memory, the next read directly from the memory, to the database is the burden.

So put a cache layer between the database and the Web, with the benefit of: 1. Reduce the burden on the database 2. Improve access speed.


What is memcached?

Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load.

Above is the official answer, my personal understanding is memcached like a temporary database, in the form of Key-value access to data, key will be converted into a hash algorithm hash-key, easy to find. Because it stores data in memory, near the CPU, the query data is very fast.




what are the similarities and differences between memcached and the cookie and session?

Cookies, session commonly used to save user information, SSO user information stored in a local cookie, sent to the session also save a copy, the security of the cookie is worse than the session. memcached can be used in the examination of the problem, the characteristics of the saved data: 1. Data Volume 2. Data is used multiple times. Session timeliness, access to the server at two points, it is more secure. Cookies can be used to address security issues through local cookie encryption, where the user's password that is accessed in the handwritten SSO is encrypted and the information that is encrypted is also accessed in the database. memcached can increase its security by placing it behind a firewall.


memcached Practical Walkthrough

1. Installing the memcached server

  1. download memcache windows stable version, unzip and put under a disk, such as in c:\r
  2. cmd   c : \memcached\memcached.exe-d Install   Install (please do not doubt that the installation is complete!) )
  3. re-enter: c:\memcached\memcached.exe-d start . Allocate 64M memory by default, using 11211 Port )

2.memcached Use

First, use the Telnet 127.0.0.1 11211 command to connect to the Memcache, and then enter stats directly to get the status of the current memcache. (If Telnet is not an internal or external command after entering Telnet, do the following.) )


Get the following information:


3. Client code


Console program:


<font size= "4" ><span style= "Font-family:simsun;" ><span style= "FONT-SIZE:18PX;" ><span style= "Font-family:simsun;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;             Using Memcached.clientlibrary;namespace memache{class Program {static void Main (string[] args) {              Add multiple cache server addresses string[] serverlist = {"192.168.24.247:11211", "192.168.24.175:11211"};              Initialize pool Sockiopool pond = sockiopool.getinstance (); Pool.              Setservers (serverlist); Set some parameter pool for memcached.              Initconnections = 3; Pool.              Minconnections = 3; Pool.                              MaxConnections = 5; Pool.              socketconnecttimeout = 1000; Pool.               Sockettimeout = 3000; Pool.              Maintenancesleep = 30; Pool.               Failover = true; Pool.              Nagle = false; Pool.           Initialize ();   Get the client instance memcachedclient mc = new Memcachedclient (); Mc.                           EnableCompression = false;              Console.WriteLine ("------------Test-----------"); BOOL FLAGS1 = MC.  Set ("user1", "My Value"); Stores the data to the cache server, where the string "My value" is cached, and key is "test" bool FLAGS2 = MC.              Set ("User2", "My Value"); BOOL FLAGS3 = MC.              Set ("User3", "My Value"); BOOL FLAGS4 = MC.              Set ("User4", "My Value"); BOOL FLAGS5 = MC.              Set ("User5", "My Value"); BOOL FLAGS6 = MC.             Set ("User6", "My Value"); if (MC.                  Keyexists ("user1"))//test cache exists with key test item {Console.WriteLine ("Test is Exists"); Console.WriteLine (MC. Get ("user1").  ToString ());              Gets the item with the key test in the cache} else {Console.WriteLine ("Test Not Exists");             }//Console.ReadLine (); BOOL flag = MC.  Delete ("user1"); Remove the key in the cache as testThe item if (MC.                  Keyexists ("user1")) {Console.WriteLine ("Test is Exists"); Console.WriteLine (MC. Get ("user1").              ToString ());              } else {//console.writeline ("Test not Exists"); } sockiopool.getinstance ().  Shutdown ();          Close the pool, close sockets console.readline (); }}}</span></span></span></font>

Program execution Results:

To start the memcached service, enter c:\memcached\memcached.exe-d start and execute.

Start the Program control console, memcached can use!



Summary

Learning is a list of knowledge, personal digestion, comparison, summed up weaving nets, actual combat process, the actual combat is the most able to test the way people learn how to do the project is very good.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Memcached Summary-"project experience"

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.