memcached的数据导入导出(理论)

http://lists.danga.com/pipermail/memcached/2007-July/004802.html

That's right. Memcached is a cache, not a primary data store. If you 
need to persist items to a database, you should do it incrementally as 
you populate the cache. In our environment (with a few exceptions) we 
only ever populate the cache with data we've read from a database -- 
that is, everything in the cache is guaranteed to be backed by a 
persistent store because it comes from a persistent store in the first 
place. When we update an object, we update it in the database and delete 
it from the cache, so the next reader that needs it can pull it from the 
database. That last part may or may not be optimal for your application 
(it essentially means the cache is lazy-populated) but the first part is 
a good guideline: if it's not already backed by a persistent store, 
don't put it in the cache. Follow that and you'll never need to worry 
about whether the cache can be dumped to disk.

Dumping the cache to disk is dangerous in any event. Consider:

User updates his account information
Your code updates the database
Your code updates the cache
You dump the cache to disk and start working on the cache host
User updates his account information again
Your code updates the database
Your code doesn't update the cache (because it's offline at the moment)
You bring the cache back online and it restores its state
User views his account information and gets the old data from the cache

Obviously this race condition exists to some extent even without a 
persistent dump, but it's miniscule in comparison and is much easier to 
work around in the application code. On a busy site any persistent 
snapshot of the cache is pretty much guaranteed to be stale even before 
it's finished hitting the disk.

That said, if you want a persistent store with a memcached interface, 
Google "Tugela cache" and you might find that more to your liking.

从缓存服务器中读取数据:

package com.memcached.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import com.page.util.CurrentTime;

import net.rubyeye.xmemcached.KeyIterator;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;

/**
 * @description memcached的工具类,
 * @author BrinPage
 * @date 2012.07.19 19:12:00
 */

@SuppressWarnings("deprecation")
public class MemcachedTest{
    
    /**
     * 从缓存服务器上读取参数
     * @param ip_port MemcachedServer的IP地址和端口号
     * @return ArryList<String []>对象
     */
    @SuppressWarnings("finally")
    public List<String []> getMemcached(String ip_port){
        List<String []> list = new ArrayList<String []>();
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(ip_port));
        builder.setConnectionPoolSize(5);
        MemcachedClient memcachedClient = null;
        CurrentTime time = new CurrentTime();
        
        try {
            memcachedClient = builder.build();
            System.out.println(time.getCurrentTime());
            KeyIterator it = memcachedClient.getKeyIterator(AddrUtil.getOneAddress(ip_port));
            String key = null;
            int count = 0;
            while(it.hasNext())
            {
                key=it.next();
                count ++;
                String value [] = memcachedClient.get(key);
//                System.out.println(value.length);
                list.add(value);
            }
            System.out.println(time.getCurrentTime());
            memcachedClient.shutdown();
        } catch (MemcachedException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            return list;
        }
    }
    
    public static void main(String args[]){
        MemcachedTest test = new MemcachedTest();
        List<String []> list = new ArrayList<String []>();
        list = test.getMemcached("127.0.0.1:11211");
        for(int i = 0; i < list.size(); i ++){
            for(int j = 0; j < list.get(i).length; j ++){
                System.out.print(list.get(i)[j] + "\t");
            }
            System.out.println();
            System.out.println("*********************************************************");
        }
    }
    
}
原文地址:https://www.cnblogs.com/Jiphen/p/2599529.html