Practice of memcached Cache System (1) Basic memcached and example programs

Source: Internet
Author: User

Respect knowledge, reprinted please indicate this article from: programming artist poechant csdn blog http://blog.csdn.net/potent

1. Definition of Cache
(1) narrow concept: it is a hardware device used for coordination between the CPU's relatively high-speed processing and the relative low-speed processing of the main memory (main memory.
(2) broad concept: it is used between two hardware with Large Speed differences to coordinate the structure of the data transmission speed differences between the two.
The narrow concept comes from the application scenarios of cache 1967 for a long time. Because the data processing speed of the CPU is much higher than that of the primary storage, there will be a high-speed cache device between the CPU and the primary storage, or even a multi-level cache device. The broad concept is a widely accepted definition. In the broad concept, cache is no longer limited to hardware or software. For example, it is used to coordinate the speed difference between relatively low-speed network transmission and relatively high-speed Disk transmission.

2. Essential Principles of Cache
It can be summarized in one sentence: the cache puts the data to be retrieved from the slow device in advance to the fast device.

3. Several types of Cache
(1) CPU cache: it is placed between the CPU and the primary memory to accelerate the relatively slow operation of the CPU on the primary memory.
(2) browser cache: it is placed between the client and the server to accelerate the relatively slow operation of the client on the server.
(3) server cache: it is placed between a network request and a local file to accelerate slow operations on local files by network requests.
(4) CDN: CDN is the content delivery network. node cache is set in different regions to accelerate the relatively slow operation on the service network.
(5) database cache
(6) OS cache: the buffer area in the memory for hard disk read/write.

4. What is memcached?

Memcached is a free open-source, high-performance, distributed memory object cache system. Memcached is a small block of data storage that constructs a key-value for a specific data (string or object) in memory.

5. Download memcached server software

Windows platform Version Download: http://splinedancer.com/memcached-win32/memcached-1.2.4-Win32-Preview-20080309_bin.zip

Linux platform Version Download: http://memcached.googlecode.com/files/memcached-1.4.10.tar.gz

6. Deploy memcached server on the server

The following uses Windows as an example:

Reference: http://www.codeforest.net/how-to-install-memcached-on-windows-machine

Decompress the downloaded Windows version to C:/memcached/

On the console, enter a command to install:

c:/memcached/memcached.exe  -d install

Start:

c:/memcached/memcached.exe -d  start

Or:

net start "memcached Server"

The default cache size is 64 mb. If not enough, open the registry and find:

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached  Server .

Modify the content:

“C:/memcached/memcached.exe” -d runservice -m 512

7. Download The memcached client API package

: Http://spymemcached.googlecode.com/files/memcached-2.5.jar

8. Compile a Java data class

package com.sinosuperman.memcached;import java.io.Serializable;public class User implements Serializable{ private static final long serialVersionUID = -372274003834027815L;String userId;public User(String userId) { super(); this.userId = userId; }public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } @Override public String toString() { // TODO Auto-generated method stub StringBuffer sb=new StringBuffer(); sb.append("userId="+this.userId); return sb.toString(); } } 

9. Compile a memcached Client

package com.sinosuperman.memcached;import java.io.IOException;import java.net.InetSocketAddress;import net.spy.memcached.MemcachedClient;public class TestMemcached {public static void main(String[] args) throws IOException {MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));for (int i = 1; i < 10; i++) {cache.set("T0001" + i, 3600, new User(i + ""));}User myObject = (User) cache.get("T00011");System.out.println("Get object from mem :" + myObject); } }

10. Run the test

The running result should be as follows:

2011-12-15 17:25:30.276 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue2011-12-15 17:25:30.292 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@c62080Get object from mem :userId=1

Respect knowledge, reprinted please indicate this article from: programming artist poechant csdn blog http://blog.csdn.net/poechant

-

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.