Java客户端调用memcached

memcached常用Java客户端有三种

1、Xmemcached

基于java nio实现的高性能可扩展的memcached客户端。它的主要特点:

  • 高性能,稳定可靠,已经在众多公司的众多项目里得到应用。
  • 功能完备:客户端分布式、权重、最新最完整的协议支持。
  • 可扩展,易于集成
  • 可动态增删memached节点
  • 客户端操作统计
  • NIO连接池
等等…………
详细参考https://code.google.com/archive/p/xmemcached/wikis/User_Guide_zh.wiki

2、spymemcached
  • 是一个采用Java开发的异步、单线程的Memcached客户端
详细参考 https://code.google.com/archive/p/spymemcached/

3、Memcached-Java-Client
  • 较早推出的memcached JAVA客户端API,应用广泛,运行比较稳定
详细参考https://github.com/gwhalin/Memcached-Java-Client
------------------------------------------------------------------------------------------------------------------------------------------------
Xmemcached使用:
import net.rubyeye.xmemcached.MemcachedClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MemcachedHelper {
    private static transient Logger log = LoggerFactory.getLogger(MemcachedHelper.class);
    private MemcachedClient memcachedClient;
    private boolean memcachedOpen;

    public boolean flushAll() {
        if (getMemcachedOpen()) {
            try {
                memcachedClient.flushAll();
                return true;
            } catch (Exception e) {
                log.error("MemcachedHelper flushAll:" + e.getMessage());
                return false;
            }
        }
        return false;
    }

    /**
     * 根据key得到value
     *
     * @param key
     * @return
     */
    public Object get(String key) {
        if (getMemcachedOpen()) {
            try {
                return memcachedClient.get(key);
            } catch (Exception e) {
                log.error("MemcachedHelper key is " + key);
            }
        }
        return null;
    }


    /**
     * 将key-value存入缓存
     *
     * @param key    存储的key名称,
     * @param value  实际存储的数据
     * @param expiry 是expire时间(单位秒),超过这个时间,memcached将这个数据替换出去,0表示永久存储(默认是一个月)
     * @return
     */
    public boolean set(String key, Object value, int expiry) {
        if (getMemcachedOpen()) {
            if (null != value) {
                try {
                    return memcachedClient.set(key, expiry, value);
                } catch (Exception e) {
                    log.error("MemcachedHelper key is " + key);
                }
            } else {
                log.error("MemcachedHelper key is " + key);
            }
        }
        return false;
    }

    /**
     * 将key-value存入缓存,永久存储(默认是一个月)
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, Object value) {
        return set(key, value, 0);
    }

    /**
     * 根据key删除value
     *
     * @param key
     * @return
     */
    public boolean delete(String key) {
        if (getMemcachedOpen()) {
            try {
                return memcachedClient.delete(key);
            } catch (Exception e) {
                log.error("MemcachedHelper key is " + key);
            }
        }
        return false;
    }

    public void setMemcachedClient(MemcachedClient memcachedClient) {
        this.memcachedClient = memcachedClient;
    }

    public void setMemcachedOpen(boolean memcachedOpen) {
        this.memcachedOpen = memcachedOpen;
    }

    public boolean getMemcachedOpen() {
        return memcachedOpen;
    }
}
Spring配置:


    
        
            
                
            
        
        
            
        
    
    
    
        
        
    

controller中使用:
import org.ghost.common.util.MemcachedHelper;
import org.ghost.web.response.ResponseResult;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;


@RequestMapping("/memcached")
@RestController
public class MemcachedController {
    @Autowired
    private MemcachedHelper memcachedHelper;


    @RequestMapping(value = "/get/{key}", method = RequestMethod.GET)
    public ResponseResult get(@PathVariable String key) {
        Object result = memcachedHelper.get(key);
        return getSucResultData(result);
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResponseResult get(@RequestParam Map condition) {
        String key = MapUtils.getString(condition, "key");
        Object result = memcachedHelper.get(key);
        return getSucResultData(result);
    }

    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public ResponseResult set(@RequestBody Map condition) {
        String key = MapUtils.getString(condition, "key");
        String value = MapUtils.getString(condition, "value");
        int time = MapUtils.getIntValue(condition, "time");

        boolean result = false;
        if (time <= 0) {
            result = memcachedHelper.set(key, value);
        } else {
            result = memcachedHelper.set(key, value, time);
        }

        if (true == result) {
            return getSucMessage();
        } else {
            return getFaultMessage();
        }
    }

    @RequestMapping(value = "/delete/{key}", method = RequestMethod.GET)
    public ResponseResult delete(@PathVariable String key) {
        boolean result = memcachedHelper.delete(key);
        if (true == result) {
            return getSucMessage();
        } else {
            return getFaultMessage();
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResponseResult delete(@RequestParam Map condition) {
        String key = MapUtils.getString(condition, "key");
        boolean result = memcachedHelper.delete(key);
        if (true == result) {
            return getSucMessage();
        } else {
            return getFaultMessage();
        }
    }

    @RequestMapping(value = "/flushall", method = RequestMethod.GET)
    public ResponseResult flushAll() {
        boolean result = memcachedHelper.flushAll();

        if (true == result) {
            return getSucMessage();
        } else {
            return getFaultMessage();
        }
    }
}

-------------------------------------------------------------------------------------------------------------------------------




你可能感兴趣的:(memcached)