memcache安装、配置、测试
今天拿到一台干净的linux虚拟器,安装memcache,首先安装libevent库。
wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz tar xvzf libevent-1.4.14b-stable.tar.gz ./configure make make install
然后,编译、安装、配置Memcached,执行如下命令行:
wget http://www.memcached.org/files/memcached-1.4.20.tar.gz tar xvzf memcached-1.4.20.tar.gz ln -s /usr/local/memcached-1.4.20 /usr/local/memcached ./configure --with-libevent=/usr/local/libevent/ make make install
安装过程中遇见这个问题:/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
解决类似问题的步骤:
1. 首先 find / -name libevent-1.4.so.2 找到缺少的链接文件到底在那儿。
2. LD_DEBUG=libs /usr/local/bin/memcached -v
3. 从Debug信息中就知道程序去哪里找链接库了。我这边程序去 trying file=/usr/lib/libevent-1.4.so.2 而我的链接库的实际存储位置是 /usr/local/lib/libevent-1.4.so.2
4. 做一个软连接 ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2
5. 搞定。
最后启动memcache :
/usr/local/bin/memcached -d -m 2014 -I 20m -u root -p 12800 -c 1024 -t20
写个小程序验证一下memecahe是否可以正常使用,输出都内容正确就OK
package com.testpai; import net.spy.memcached.AddrUtil; import net.spy.memcached.BinaryConnectionFactory; import net.spy.memcached.MemcachedClient; import net.spy.memcached.internal.OperationFuture; public class TestMemcached { public static void main(String[] args) throws Exception { String address = "10.6.2.95:12800"; MemcachedClient client = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(address)); String key = "magic_words"; int exp = 3600; String o = "hello"; // set OperationFuture<Boolean> setFuture = client.set(key, exp, o); if(setFuture.get()) { // get System.out.println(client.get(key)); // append client.replace(key, exp, " the world!"); System.out.println(client.get(key)); // prepend client.prepend(exp, key, "Stone, "); System.out.println(client.get(key)); // replace o = "This is a test for spymemcached."; OperationFuture<Boolean> replaceFuture = client.replace(key, exp, o); if(replaceFuture.get()) { System.out.println(client.get(key)); // delete client.delete(key); System.out.println(client.get(key)); } } client.shutdown(); } }
相关推荐
andyhuabing 2020-05-22
huolan 2020-01-08
andyhuabing 2013-04-01
xx0cw 2019-12-24
郗瑞强 2020-08-16
85590296 2020-07-22
jkzyx 2020-06-29
luotuo 2020-06-26
LinuxJob 2020-06-26
ol0 2020-06-26
清溪算法君老号 2020-06-25
86251043 2020-06-13
CSDN0BLOG 2020-06-09
ol0 2020-05-26
程序员俱乐部 2020-05-06
83530391 2020-05-05
ol0 2020-05-02
83530391 2020-04-09
85590296 2020-03-25