首先部署Server端的Memcache plugin集成环境。目前支持的系统为Linux, Solaris, and OS X,不支持windows。文档地址:http://dev.mysql.com/doc/refman/5.6/en/innodb-memcached-setup.html
由于我采用的tar包安装的MySQL,所以在安装memcache plugin的时候需要先安装libevent包。
yum install libevent
即可。
然后,安装libmemcached所需要的表
将插件设置成随服务启动而启动的守护插件
重启MySQL服务,安装完成。默认访问端口为11211。
下面来验证一下安装,简单的可以采用telnet的方式发送memcached命令
然后通过sql,在demo_test表中查询数据:
再通过Java代码操作一下,我们采用xmemcached作为client api。官方地址:https://code.google.com/p/xmemcached。Maven依赖:
com.googlecode.xmemcached xmemcached1.4.1
代码如下:
/** * @param args * @author lihzh(OneCoder) * @blog http://www.coderli.com * @throws MemcachedException * @throws InterruptedException * @throws TimeoutException * @throws IOException * @date 2013 -3 -12 下午12:07:41 */ public static void main(String[] args) throws TimeoutException, InterruptedException, MemcachedException, IOException { MemcachedClient client = new XMemcachedClient("10.4.44.208" , 11211); // store a value for one hour(synchronously). client.set( "key", 3600, "onecoder"); // Retrieve a value.(synchronously). Object someObject = client.get( "key"); // Retrieve a value.(synchronously),operation timeout two seconds. someObject = client.get( "key", 2000); System. out.println(someObject); }
通过mysql客户端查询记录,成功存入:
这里测试的仅仅最基本的功能,如果想使用该功能,还需要做好传统数据表与memcache表的映射关系。具体可参考:http://dev.mysql.com/doc/refman/5.6/en/innodb-memcached-developing.html。
原文地址:MySQL5.6.10 NoSQL API访问方式体验, 感谢原作者分享。