如何使用memcache

package cn.teacheredu.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

import net.spy.memcached.ConnectionObserver;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.transcoders.Transcoder;

public class SpyMemcachedManager {
	/**
     * memcached客户单实例.由Spring负责实例化
     */
    private MemcachedClient memcachedClient;
    public MemcachedClient getMemcachedClient() {
        return memcachedClient;
    }
    public void setMemcachedClient(MemcachedClient memcachedClient) {
        this.memcachedClient = memcachedClient;
    }
    public void addObserver(ConnectionObserver obs) {
        memcachedClient.addObserver(obs);
    }
    public void removeObserver(ConnectionObserver obs) {
        memcachedClient.removeObserver(obs);
    }
    
    /**
     * 
     * 加入缓存.
     * 
     * @param key key
     * @param value value
     * @param expire 过期时间
     * @return boolean
     */
    public boolean set(String key, Object value, int expire) {
        Future<Boolean> f = memcachedClient.set(key, expire, value);
        return getBooleanValue(f);
    }
    /**
     * 
     *从缓存中获取.
     * 
     * @param key key
     * @return Object
     */
    public Object get(String key) {
        return memcachedClient.get(key);
    }
    
    /**
     * 
     *从缓存中获取.
     * 
     * @param key key
     * @return Object
     */
    public Object asyncGet(String key) {
        Object obj = null;
        Future<Object> f = memcachedClient.asyncGet(key);
        try {
            obj = f.get(SpyMemcachedConstants.DEFAULT_TIMEOUT, SpyMemcachedConstants.DEFAULT_TIMEUNIT);
        } catch (Exception e) {
            f.cancel(false);
        }
        return obj;
    }
    /**
     * 
     * 加入缓存.
     * 
     * @param key key
     * @param value value
     * @param expire 过期时间
     * @return boolean
     */
    public boolean add(String key, Object value, int expire) {
        Future<Boolean> f = memcachedClient.add(key, expire, value);
        return getBooleanValue(f);
    }
    /**
     * 
     * 替换.
     * 
     * @param key key
     * @param value value
     * @param expire 过期时间
     * @return boolean
     */
    public boolean replace(String key, Object value, int expire) {
        Future<Boolean> f = memcachedClient.replace(key, expire, value);
        return getBooleanValue(f);
    }
    /**
     * 
     * 从缓存中删除.
     * 
     * @param key key
     * @return boolean
     */
    public boolean delete(String key) {
        Future<Boolean> f = memcachedClient.delete(key);
        return getBooleanValue(f);
    }
    /***
     * 
     * flush.
     * 
     * @return boolean
     */
    public boolean flush() {
        Future<Boolean> f = memcachedClient.flush();
        return getBooleanValue(f);
    }
    /**
     * 
     * 从缓存中获取.
     * 
     * @param keys keys
     * @return Map<String, Object>
     */
    public Map<String, Object> getMulti(Collection<String> keys) {
        return memcachedClient.getBulk(keys);
    }
    /**
     * 
     * 从缓存中获取.
     * 
     * @param keys keys
     * @return Map<String, Object>
     */
    public Map<String, Object> getMulti(String[] keys) {
        return memcachedClient.getBulk(keys);
    }
    /**
     * 
     * 从缓存中获取.
     * 
     * @param keys keys
     * @return Map<String, Object>
     */
    public Map<String, Object> asyncGetMulti(Collection<String> keys) {
        Map<String, Object> map = null;
        Future<Map<String, Object>> f = memcachedClient.asyncGetBulk(keys);
        try {
            map = f.get(SpyMemcachedConstants.DEFAULT_TIMEOUT, SpyMemcachedConstants.DEFAULT_TIMEUNIT);
        } catch (Exception e) {
            f.cancel(false);
        }
        return map;
    }
    /**
     * 
     * 从缓存中获取.
     * 
     * @param keys keys
     * @return Map<String, Object>
     */
    public Map<String, Object> asyncGetMulti(String[] keys) {
        Map<String, Object> map = null;
        Future<Map<String, Object>> f = memcachedClient.asyncGetBulk(keys);
        try {
            map = f.get(SpyMemcachedConstants.DEFAULT_TIMEOUT, SpyMemcachedConstants.DEFAULT_TIMEUNIT);
        } catch (Exception e) {
            f.cancel(false);
        }
        return map;
    }

    
    public long increment(String key, int by, long defaultValue, int expire) {
        return memcachedClient.incr(key, by, defaultValue, expire);
    }
    public long increment(String key, int by) {
        return memcachedClient.incr(key, by);
    }
    public long decrement(String key, int by, long defaultValue, int expire) {
        return memcachedClient.decr(key, by, defaultValue, expire);
    }
    public long decrement(String key, int by) {
        return memcachedClient.decr(key, by);
    }
    public long asyncIncrement(String key, int by) {
        Future<Long> f = memcachedClient.asyncIncr(key, by);
        return getLongValue(f);
    }
    public long asyncDecrement(String key, int by) {
        Future<Long> f = memcachedClient.asyncDecr(key, by);
        return getLongValue(f);
    }

    
    public void printStats() throws IOException {
        printStats(null);
    }
    public void printStats(OutputStream stream) throws IOException {
        Map<SocketAddress, Map<String, String>> statMap = memcachedClient.getStats();
        if (stream == null) {
            stream = System.out;
        }
        StringBuffer buf = new StringBuffer();
        Set<SocketAddress> addrSet = statMap.keySet();
        Iterator<SocketAddress> iter = addrSet.iterator();
        while (iter.hasNext()) {
            SocketAddress addr = iter.next();
            buf.append(addr.toString() + "/n");
            Map<String, String> stat = statMap.get(addr);
            Set<String> keys = stat.keySet();
            Iterator<String> keyIter = keys.iterator();
            while (keyIter.hasNext()) {
                String key = keyIter.next();
                String value = stat.get(key);
                buf.append("  key=" + key + ";value=" + value + "/n");
            }
            buf.append("/n");
        }
        stream.write(buf.toString().getBytes());
        stream.flush();
    }
    public Transcoder<Object> getTranscoder() {
        return memcachedClient.getTranscoder();
    }
    private long getLongValue(Future<Long> f) {
        try {
            Long l = f.get(SpyMemcachedConstants.DEFAULT_TIMEOUT, SpyMemcachedConstants.DEFAULT_TIMEUNIT);
            return l.longValue();
        } catch (Exception e) {
            f.cancel(false);
        }
        return -1;
    }
    private boolean getBooleanValue(Future<Boolean> f) {
        try {
            Boolean bool = f.get(SpyMemcachedConstants.DEFAULT_TIMEOUT, SpyMemcachedConstants.DEFAULT_TIMEUNIT);
            return bool.booleanValue();
        } catch (Exception e) {
            f.cancel(false);
            return false;
        }
    }
   
}


上面是memcache的工具类。

下面做个单元测试,说明几个需要注意的地方(我这里没有调用工具类的方法,而是直接调用的源码的方法,所以方法名会有差异)


package pms_test;

import net.spy.memcached.MemcachedClient;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import cn.teacheredu.utils.SpyMemcachedManager;

public class MemcacheT extends BaseSpringJunit{

private MemcachedClient memcachedClient;
	
	@Autowired
	SpyMemcachedManager spyMemcachedManager;
	
	@Before
	public void init(){
		this.memcachedClient = spyMemcachedManager.getMemcachedClient();
	}
	
	@Test
	public void testMemcache(){
		memcachedClient.set("count", 1000, 1);
		memcachedClient.add("count", 1000, 2);
		System.out.println(memcachedClient.get("count"));
	}
	
}


结果为:

1

结论:add方法会判断这个key是否存在,如果不存在的话才会插入,这点要注意,而set方法不会,只要是set调用的话,都会插入数据,这也是两者的区别



asyn前面的方法看名字就知道是异步方法,这里就不一一举例了


下面看看getBulk这个方法,也就是工具类的getMult()这个方法

package pms_test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.spy.memcached.MemcachedClient;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import cn.teacheredu.utils.SpyMemcachedManager;

public class MemcacheT extends BaseSpringJunit{

private MemcachedClient memcachedClient;
	
	@Autowired
	SpyMemcachedManager spyMemcachedManager;
	
	@Before
	public void init(){
		this.memcachedClient = spyMemcachedManager.getMemcachedClient();
	}
	
	@Test
	public void testMemcache(){
		memcachedClient.set("count1", 1000, 1);
		memcachedClient.set("count2", 1000, 2);
		memcachedClient.set("count3", 1000, 3);
		List<String> list = new ArrayList<String>();
		list.add("count1");	
		list.add("count2");
		list.add("count3");
		Map<String, Object> bulk = memcachedClient.getBulk(list);
		System.out.println(bulk.get("count1"));;
		System.out.println(bulk.get("count2"));;
		System.out.println(bulk.get("count3"));;
	}
	
}


结果如下:

2017-8-28 16:28:47 org.springframework.test.context.transaction.TransactionalTestExecutionListener startNewTransaction
信息: Began transaction (1): transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@675926d1]; rollback [true]
1
2
3

结论:通过调用getBulk()方法,可以传入很多的Key,返回的是一个map类型的数据


针对incr()这个方法(对应工具类的increment()),做出如下测试:


package pms_test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.spy.memcached.MemcachedClient;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import cn.teacheredu.utils.SpyMemcachedManager;

public class MemcacheT extends BaseSpringJunit{

private MemcachedClient memcachedClient;
	
	@Autowired
	SpyMemcachedManager spyMemcachedManager;
	
	@Before
	public void init(){
		this.memcachedClient = spyMemcachedManager.getMemcachedClient();
	}
	
	@Test
	public void testMemcache(){
		for(int i = 0;i<10;i++){
			long incr = memcachedClient.incr("count", 2, 1, 1000);	//四个参数分为对应:键,每次增长的增长量,默认值,最大时间(如果还不太懂可以直接看看源码)
			System.out.println(incr);
		}
	}
	
}


结果如下:

1
3
5
7
9
11
13
15
17
19

结论:递增方法
那么desc()这个方法肯定就是递减了,再次就不再阐述了。


如果还有不懂的地方可以在留言处留言,我看到了会第一时间回复

喜欢的观众姥爷记得点个赞或者关注一下,谢谢~


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值