首页 > 代码库 > Memcached实际项目应用

Memcached实际项目应用

在Memcached官方的GitHub上,可以找到这么一个文档——HowTo.txt

Howto=====Basic Example:==============Lets say you have 3 servers.  Server 1 and server 2 have 3GB of spaceand server 3 has 2GB of space for cache.  Here is how I would set upmy client.-----------------------------------------------------------------------------import com.danga.MemCached.*;public class MyClass {    // create a static client as most installs only need    // a single instance
// 创建一个MemCachedClient客户端,用于从MemcachedServer进行交互 protected static MemCachedClient mcc
= new MemCachedClient(); // set up connection pool once at class load static { // server list and weights
    // 因为一个客户端(例如你的Web 应用)可能会访问多个服务器。
    // 使用Java客户端事,默认Windows 版的Memcached服务的端口默认是11211 String
[] servers = { "server1.mydomain.com:1624", "server2.mydomain.com:1624", "server3.mydomain.com:1624" }; Integer[] weights = { 3, 3, 2 }; // grab an instance of our connection pool SockIOPool pool = SockIOPool.getInstance(); // set the servers and the weights pool.setServers( servers ); pool.setWeights( weights ); // set some TCP settings // disable nagle // set the read timeout to 3 secs // and don‘t set a connect timeout pool.setNagle( false ); pool.setSocketTO( 3000 ); pool.setSocketConnectTO( 0 ); // initialize the connection pool pool.initialize(); } // from here on down, you can call any of the client calls public static void examples() {
    // 存储数据到缓存服务器 mcc.set(
"foo", "This is a test String" );
    // 从服务器取数
String bar = mcc.get( "foo" ).toString(); }}-------------------------------------------------------------------------------Multi-client Example:=====================If you need to support multiple clients (i.e. Java, PHP, Perl, etc.)you need to make a few changes when you are setting things up:----------------------------------------------------------------- // use a compatible hashing algorithm pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );-------------------------------------------------------------------Serialization:==============For java "native types", which include:BooleanByteStringCharacterStringBufferStringBuilderShortLongDoubleFloatDateIntegerThe client will by default NOT use java serialization, and insteadwill serialize using the primitive values to save space.For other java objects, you have 2 options to serialize the java objects.one is make sure the class implements Serializable in order to be able to be stored with default object transcoder provided by this client;The other alternative is to write your own transcoder to do the serialization and deserialization by yourself, the following is simple example:-------------------------------------------------------------------------package com.schooner.MemCached;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;/** * {@link ObjectTransCoder} is the default TransCoder used to handle the * serialization and deserialization in memcached operations. * * @author Xingen Wang * @see AbstractTransCoder * @see TransCoder */public class ObjectTransCoder extends AbstractTransCoder { /* * (non-Javadoc) * * @see com.schooner.MemCached.TransCoder#decode(InputStream) */ public Object decode(final InputStream input) throws IOException { Object obj = null; ObjectInputStream ois = new ObjectInputStream(input); try { obj = ois.readObject(); } catch (ClassNotFoundException e) { throw new IOException(e.getMessage()); } ois.close(); return obj; } /* * (non-Javadoc) * * @see * com.schooner.MemCached.AbstractTransCoder#encode(java.io.OutputStream, * java.lang.Object) */ public void encode(final OutputStream output, final Object object) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(output); oos.writeObject(object); oos.close(); } public Object decode(byte[] input){ Object obj = null; try { ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(input)); obj = ois.readObject(); ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return obj; }}-----------------------------------------------------------------------------------------After that, you should set transcoder to your client:-----------------------------------------------------------------------------MemCachedClient mc = new MemCachedClient();mc.setTransCoder(new YourTransCoder());-----------------------------------------------------------------------------I would also recommend that if possible, classes should insteadimplement Externalizable as opposed to Serializable or write the transcoder by your self. This allows theauthor of the class to define how objects of that class shouldserialize. In practice at Meetup.com, we saw a 60% reduction in the sizeof our serialized objects by doing this. This means less data to eat upcache space and less data to transfer over the network.binary protocol:================In Schooner‘s implementation, binary protocol for memcached has been implemented, and due to our test performance increased about 5% overall, but you have to use memcached 1.4+ in the server side to support this features.The following code snipets shows how to use this feature: -----------------------------------------------------------------------------mc = new MemCachedClient(true, true);-----------------------------------------------------------------------------UDP protocol:=============In schooner‘s implementation, UDP protocol for memcached is also supported. While dueto our test, the performance is not that good as tcp protocol, we are still working on performance tuning.In our latest implementation, UDP protocol works in asynchronized mode.We recommend user memcached 1.4.4+ when UDP protocol is used. -----------------------------------------------------------------------------mc = new MemCachedClient(false, false);-----------------------------------------------------------------------------Other:======See the java docs.

上面的第一个例子中:

如果第一次执行examples()后,会在服务器上存储key为‘foo’的对象。

如果客户端执行完毕后,把examples()改为

public static void examples() {

     //   mcc.set( "foo", "This is a test String" );
     String bar = mcc.get( "foo" ).toString(); }

也就是不再设置key=‘foo‘的值,而是直接取数,这样也是可以取到的。



在实际开发中,一般用于和数据库交互前后。
如果是查询数据:
//1)从缓存服务器查询取数
//2)如果取到,就直接返回,不再从数据库取数。没有取到,才从数据库查询取数。
//3)取数完毕,将数据在缓存服务器存储一份。


// 如果是更新操作:
//1)直接对数据库进行更新操作
//2) 将数据在缓存服务器也更新。