Home  >  Article  >  Backend Development  >  Application of Memcached in .net

Application of Memcached in .net

黄舟
黄舟Original
2017-03-13 13:18:221338browse

1. Server installation and configuration

1) Extract the file to c:\memcached (you can also choose any location)

2) The command line enters 'c:\memcached\memcached.exe -d install'

##3) The command line enters 'c:\memcached\memcached.exe -d start', this command starts Memcached, the default listening port is 11211

4) You can view its help through memcached.exe -h

##2 , Client installation and reference



# For client operation server, we have a variety of Optional method, here, we choose to use dll. Because the dll method is faster to generate and avoids cumbersome settings in the configuration file, which is more convenient. For telnet commands, etc., we can use them on the server side to check data, check the cache hit rate, etc.

1. References about Dll

After downloading the Dll, add the reference as shown in the project

2, there may be problems

I am introducing log4net When working with Memcached.ClientLibray, it was introduced, and using was also added to the class file, but an error was reported when generating it. Later, it was found that it was a problem with the .net framework version. After changing the version from the version shown below to .net framework 4, it was generated. OK.

Three, in-depth understanding of Memcached mechanism

1, based on C/S architecture, simple protocol

1) C/S architecture, at this time memcached is the server. We can use various client programs to connect to the memcached server.

2) Memcached’s server-client communication does not use XML and other formats, but uses a simple text line-based protocol. Therefore, data can also be saved and obtained on memcached through telnet. .

2, event processing based on libevent

1) Libevent is an encapsulation of a cross-platform event processing interface that is compatible with these Operating system: Event processing of operating systems such as Windows/Linux/BSD/Solaris.

2) Memcached uses libevent to handle concurrent network connections, which can maintain the ability to respond quickly even in large concurrency situations.

3, built-in memory storage method

In order to improve performance, the data saved in memcached is stored in the built-in memory storage space of memcahced. Since the data only exists in memory, restarting memcached and restarting the operating system will cause all data to disappear. In addition, after the content capacity reaches the specified value, unused caches are automatically deleted based on the LRU (Least Recently Used) algorithm. Memcached itself is a server designed for caching, hiding and not thinking too much about the persistence of data.

4, client-based distributed

Although Memcached is a "distributed" cache server, the server does not have distributed functions. . Individual memcacheds do not communicate with each other to share information. So, how to carry out distribution depends entirely on the implementation of the client. If you are interested, you can download the hash algorithm from Baidu.



# Code experience:


4. Memcached and session

1)                                                                                                                            

##

2) The focus of the problems to be solved is different

5. Memcached and Cache

cache is used. The state management stuff in Net also feels quite powerful: 1. Cache dependencies: You can rely on files, databases, or combinations of dependencies, and it provides a notification mechanism for data expiration to facilitate us to update the data in time when it expires. 2. Use of notification mechanism: In the past, the polling mechanism was used to determine whether the data had expired. It was similar to looping to see if the data had been updated. If it had been updated, the cache was updated at this time. But I always feel that the polling mechanism is very strange. For example, when I boil a pot of water at home, I should not always check to see if the water is boiling, but I should check if the water is boiling and then a sound will tell me that the water is boiling. This is what the notification mechanism means. As for the principle of the notification mechanism, you can check the observer pattern. Among the 23 design patterns, this is used quite a lot.

When I first used memcached, due to cache reasons, I naturally thought of this notification mechanism. After checking, it seems that memcached does not have this. If you want to make an expired update yourself, Only polling mechanism can be used. But thinking about the usage scenarios of memcached, we will find that an expiration time is enough.

6. Applicable scenarios of memcached

1) Frequently changing and unstable data does not need to be stored in real time (such as users Online status, number of online people)

2) News from the portal website, etc., I think the static page of the page still cannot meet the requirements, and can be put in Memcached (with jQuery's Ajax request)

......

Seven, memcached security issues

## Since memcached does not have a built-in authentication mechanism, it is possible to log in using telnet to access the ip+port, and then inside The data becomes lambs to be slaughtered. In order to avoid being hacked, we can close the external access port on the memcached server and only allow local programs to access.

Eight, comparison of similar technologies

1,Redis

1) Support more Value type

## This 2) Specify: Redis will periodically write the updated data to the disk or write the modification operation to the additional record file, and on this basis master-slave (master-slave) synchronization

##3) Supports master-slave synchronization: data can be synchronized from the master server to any number of slave servers, and the slave server can be associated with other slave servers Main server. This allows Redis to perform single-level tree replication. Saving can write data intentionally or unintentionally. Since the publish/subscribe mechanism is fully implemented, when the slave database synchronizes the tree anywhere, it can subscribe to a channel and receive the complete message release record of the master server. Synchronization is helpful for scalability and data redundancy of read operations.

2, EHCACHE

# 1) The internal cache framework of the pure Java process has the characteristics of fast and hard -working. It is the default cacheProvider in Hibernate .

2) Open source Java distributed cache: Mainly for general cache, Java EE and lightweight containers. It features memory and disk storage, cache loaders, cache extensions, cache exception handlers, a gzip cache servlet filter, support for REST and SOAP APIs, and more.

. . . . . . . . .

The above is the content of Memcached application in .net. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Xamarin For EveryoneNext article:Xamarin For Everyone