Use the Memory Object Cache System in the system

Source: Internet
Author: User

1. Concepts
In recent years, the memory capacity has been continuously increased and prices have been falling. The operating system can support larger address spaces, making full use of technology to improve system performance has become a hot spot.
From the database aspect, this solution can use "Memory Database", and another solution is to use the "Memory Object cache system ", save some small-volume and frequently-used data in the memory object cache system as key/value to reduce the performance degradation caused by database query access.
This article mainly describes the latter, but also describes the concept of "Memory Database" in this Chapter.
1.1 Memory Database
A memory database is a database that stores data in the memory for direct operations. Compared with the disk, the data read and write speed in the memory is several orders of magnitude higher. storing data in the memory can greatly improve the performance of applications compared to accessing data from the disk.
In database technology, there are currently two main methods to use a large amount of memory:
1) in traditional databases, increase the buffer pool, place all data involved in a transaction in the buffer pool, and organize the corresponding data structure for query and update processing, that is to say, the shared memory technology. The main goal of this method is to minimize disk access.
2) using the memory database technology (also called the primary database) technology, that is, simply re-designing a database management system, re-design the algorithms and data structures for query processing, concurrency control, and recovery to use the CPU cycle and memory more effectively. This technology almost puts the entire database into the memory, thus, a fundamental change has taken place.
1.2 Memory Object Cache System
The memory object cache system is used for dynamic Web applications to reduce database load. It caches data and objects in the memory to reduce the number of times the database is read, so as to provide dynamic, database-driven website speed.
Memcached is a high-performance distributed memory object cache system based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can write it in any language and communicate with the daemon through memcached protocol. However, it does not provide redundancy (for example, copying its hashmap entries). When a server S stops running or crashes, all key/value pairs stored on S will be lost.
Developed by Danga Interactive, Memcached improves the access speed of LiveJournal.com. The number of dynamic page views per second on the LJ is several thousand, and the number of users is 7 million. Memcached greatly reduces the database load and makes it easier to allocate resources for faster access.
2. Application scenarios
Memcached is generally used in combination with the database system. It is used to store data that is frequently read and can never improve performance.
The following describes several application instances in the call center system.
2.1 task enforceability check
For example, an outbound call task needs to run in the specified "task start time" and "task end time" and in the "enabled" status, in addition, the process of a single outbound call task needs to check the database regularly (for example, 1 minute) whether the task continues to run or stops running. When multiple outbound call tasks run in parallel, this query will burden the system.
If the Memory Object cache system is used, the status information of each task is stored as a key/value pair in the "task info table" of Memcached (key = task id, value: indicates whether a task can be run.) When you insert a task, insert a data entry into the "task status table" of Memcached; when the "task start time" and "task end time" or "status" change, the value of the "task info table" of Memcached is changed based on the key. When an outbound call task is in progress, you no longer need to query the database when the task is running. Generally, you only need to query Memcached to reduce access to the database.
2.2 calendar check
In Call Center routing, you sometimes need to provide routing functions based on the schedule, such as meeting a certain schedule (for example, the calendar of "October 1 00:00:00" to "October 7 23:59:59" each year) to A relay. A calendar can correspond to multiple entries. If you need to verify whether the current time meets the schedule before routing, You need to query the information of the two tables and check whether the schedule conditions are met. When many concurrent users make calls, this check will add a lot of load to the system. In this case, you can also use the Memory Object cache system to solve the problem.
Develop a timed trigger (once every minute) program to query multiple pieces of information in the calendar and calendar entry tables, compare the current time with a calendar, and save it to the "calendar" in Memcached, the key is the calendar key, and the value is the identifier of the current time in the current calendar.
The routing program checks the "calendar" in Memcached based on the schedule key. If A calendar is satisfied, the route is routed to A relay, and the database is basically no longer queried for the schedule.
3. Application taboos
3.1 do not use Memcached as a database
The primary purpose of Memcached is to speed up data response time. Otherwise, it takes a long time to build or restore data from other data sources. A typical example is to restore information from a database, especially when the information needs to be formatted or processed before it is displayed to the user. Memcached is designed to store information in the memory to avoid repeated execution of the same task each time data needs to be restored.
Do not use Memcached as the only source of information required to run the application. Data should always be available from other sources of information. In addition, remember that Memcached is only a key/value storage. You cannot query data or iterate the content to extract information.
3.2 do not use Memcached to store large volumes of data
The size of Memcached is not large, and it is mainly used for caching. Therefore, you should not store large volumes of data in Memcached. If the row data of a data table is stored in Memcached in a certain format, and the data volume of the table is large, the space is not allowed first, and the query is very troublesome. This type of data is suitable for storing in the database.
3.2 Memcached is not safe
To ensure optimal performance, Memcached does not provide any form of security, neither authentication nor encryption. This means that the access to the memcached server should be handled as follows: first, by placing them on the same private side of the Application Deployment environment, and second, if the security is required, UNIX is used.®Socket and only allow applications on the current host to access this Memcached server.
This sacrifices some flexibility and elasticity, as well as the ability of multiple machines across the network to share the RAM cache, but this is the only solution to ensure the security of memcached data in the current situation.
1. Introduction to Memcached
Memcached is a high-performance, distributed memory object cache system that reduces database loads in dynamic applications and improves access speeds. Developed by Danga Interactive, Memcached improves the access speed of LiveJournal.com. The number of dynamic page views per second on the LJ is several thousand, and the number of users is 7 million. Memcached greatly reduces the database load and makes it easier to allocate resources for faster access.
The latest version of Memcached is developed and designed in C language. It is an application software and runs on the server as the server of the cache server, the client needs to be written in a specific language to communicate with it for data caching and retrieval.
In the system, Memcached is usually installed and run on the server, and then the required data is cached, and all data is cached and accessed, and the replacement operation after the data is updated all needs to be performed by the program.
2. Install Memcached
2.1 install the main program
The general server uses Linux. The author only describes how to install Memcached on Windows. for installation on Linux, refer to other information on the Internet.
Windows Version: http://code.jellycan.com/memcached/
The latest version of win32 is 1.2.6. The download page is as follows:



Click “memcached-1.2.6-win32-bin.zip in the middle to go To the download page. After downloading the file, decompress it to drive D. After decompression, the Directory D: "memcached-1.2.6-win32-bin2-contains A memcached.exe.
In the Windows Command Line (cmd command to enter the command line) window to enter the directory, first run:
Memcached.exe-d install
The above line indicates that Memcached is installed as a service for normal operation. Run the following command to start Memcached. You can also specify the-l parameter to indicate the IP address to be started, and-m to indicate the cache size:
Memcached.exe-d start
If-m is specified, it indicates the number after the cache size is-m. The unit is M. For example:
Memcached.exe-l 127.0.0.1-m 32-d start
See the following example:

2.2 Java client Installation
For: https://github.com/gwhalin/Memcached-Java-Client
The download page is as follows:


 
Click "Downloads" in the area on the right. The pop-up download window is as follows:



The latest version is 2.5.2. Click javajava_memcached-release_2.5.2.zip to download it. Decompress the package. The directory structure is shown in:



In the application, you need to copy the java_memcached-release_2.5.2.jar package to a Java project.
3. Use Memcached
3.1 create a project
Create a test project named memcacheddemo in MyEclipse, src put the source code, bin put the classes file, lib put the jar package, and copy the java_memcached-release_2.5.2.jar to the lib directory, the directory structure is as follows:

3.2 SockIOPool class and common methods
SockIOPool is a socket connection pool class. The common method is as follows:
L setServers (String [] servers): sets the server information array;
L setWeights (String [] weights): sets an array of server weights;
L setInitConn (int count): sets the initial number of connections;
L setMinConn (int minConn): sets the minimum number of connections;
L setMaxConn (int maxConn): sets the maximum number of connections;
L setMaxIdle (long arg0): sets the maximum processing time;
L setMaintSleep (long arg0): Sleep Time of the main thread;
L initialize (): initializes the connection pool.
3.3 MemCachedClient class and common methods
The MemCachedClient class is used to operate the Memcached Memory Object cache system. The common methods are as follows:
L add (String key, Object value): add a key-value pair to the cache;
L add (String key, Object value, Date expires): add a key-value pair to the cache and set its timeout time;
L set (String key, Object value): set a key value in the cache;
L set (String key, Object value, Date expires): set a key value in the cache and set its timeout time;
L get (String key): get the value of a key.
L incr (String key): executes the + 1 operation for the value on a key;
L decr (String key): performs the-1 operation for the value on a key;
L replace (String key, String value): replace the value of a key with a new value;
L replace (String key, String value, Date expires): replace the value of a key with a new value and set its timeout time.
3.4 use instances
Create a Java class MemcachedTest in the source code directory of the memcacheddemo project. The code for this class is as follows:
Import java. util. Date;

Import com. danga. MemCached. MemCachedClient;
Import com. danga. MemCached. SockIOPool;

/**
* Use the memcached cache test class.
* @ Author a honey fruit
*/
Public class MemcachedTest {
// Create a globally unique instance
Protected static MemCachedClient mcc = new MemCachedClient ();

Protected static MemcachedTest memCached = new MemcachedTest ();

// Set the connection pool with the Cache Server
Static {
// Server list and weight
String [] servers = {"127.0.0.1: 11211 "};
Integer [] weights = {3 };

// Obtain the Instance Object of the socket connection pool
SockIOPool pool = SockIOPool. getInstance ();

// Set Server Information
Pool. setServers (servers );
Pool. setWeights (weights );

// Set the initial connection count, minimum and maximum connections, and maximum processing time.
Pool. setInitConn (5 );
Pool. setMinConn (5 );
Pool. setMaxConn (250 );
Pool. setMaxIdle (1000*60*60*6 );

// Set the sleep time of the main thread
Pool. setMaintSleep (30 );

// Set TCP Parameters and connection timeout
Pool. setNagle (false );
Pool. setSocketTO (3000 );
Pool. setSocketConnectTO (0 );

// Initialize the connection pool
Pool. initialize ();

// Compression settings. Data exceeding the specified size (unit: K) will be compressed.
Mcc. setCompressEnable (true );
Mcc. setCompressThreshold (64*1024 );
}

/**
* Protection constructor method, which cannot be instantiated
*/
Protected MemcachedTest (){

}

/**
* Obtain a unique instance.
* @ Return
*/
Public static MemcachedTest getInstance (){
Return memCached;
}

/**
* Add a specified value to the cache.
* @ Param key
* @ Param value
* @ Return: if the key does not exist in the cache and is successfully added, true is returned. Otherwise, false is returned.
*/
Public boolean add (String key, Object value ){
Return mcc. add (key, value );
}

/**
* Add a key-value pair to the cache.
* @ Param key
* @ Param value
* @ Param expires timeout
* @ Return: if the key does not exist in the cache and is successfully added, true is returned. Otherwise, false is returned.
*/
Public boolean add (String key, Object value, Date expires ){
Return mcc. add (key, value, expires );
}

/**
* To change the value of a key to a new value, you must first ensure that the key exists.
* @ Param key
* @ Param value
* @ Return returns true if the call is successful, and false if the call fails.
*/
Public boolean replace (String key, Object value ){
Return mcc. replace (key, value );
}

/**
* To change the value of a key to a new value, you must first ensure that the key exists.
* @ Param key
* @ Param value
* @ Param expires timeout
* @ Return returns true if the call is successful, and false if the call fails.
*/
Public boolean replace (String key, Object value, Date expires ){
Return mcc. replace (key, value, expires );
}

/**
* Add a specified value to the cache.
* @ Param key
* @ Param value
* @ Return returns true if the call succeeds. Otherwise, false is returned.
*/
Public boolean set (String key, Object value ){
Return mcc. set (key, value );
}

/**
* Add a specified value to the cache and set its timeout value.
* @ Param key
* @ Param value
* @ Param expires timeout
* @ Return returns true if the call succeeds. Otherwise, false is returned.
*/
Public boolean set (String key, Object value, int expires ){
Return mcc. set (key, value, expires );
}

/**
* Get the object based on the specified keyword.
* @ Param key
* @ Return returns the value.
*/
Public Object get (String key ){
Return mcc. get (key );
}

/**
* The value of the specified key is + 1, and the final value is returned.
* @ Param key
* @ Return returns the final value.
*/
Public long incr (String key ){
Return mcc. incr (key );
}

/**
* The value of the specified key is-1, and the final value is returned.
* @ Param key
* @ Return returns the final value.
*/
Public long decr (String key ){
Return mcc. decr (key );
}

/**
* Test Method
* @ Param args
*/
Public static void main (String [] args ){
MemcachedTest cache = MemcachedTest. getInstance ();
Cache. set ("count", 123 );
System. out. println ("count =" + cache. get ("count "));
Boolean flag = cache. add ("schedule_2", "0 ");
System. out. println ("flag =" + flag );
System. out. println ("schedule_2 =" + cache. get ("schedule_2 "));
}
}
The running result is:
Count = 123
Flag = true
Schedule_2 = 0

Author: ERDP Technical Architecture"

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.