初步了解
通过https://www.cnblogs.com/xrq730/p/4948707.html这个文章对memcache做了简单了解
官网wiki内容感觉有点太笼统,没看懂
本地安装
环境:mac 11.0.1
1、官网(https://memcached.org/downloads)下载最新稳定版本 memcached-1.6.9.tar.gz
2、解压到指定目录(/app/memcached-1.6.9)参考(https://github.com/memcached/memcached/wiki/Install)中的安装过程进行安装;
cd memcached-1.x.x
./configure --prefix=/usr/local/memcached
make && make test && sudo make install
异常情况
1、执行 ./configure --prefix=/usr/local/memcached 命令时提示:
checking for libevent directory... configure: error: libevent is required. You can get it from https://www.monkey.org/~provos/libevent/
If it's already installed, specify its path using --with-libevent=/dir/
1.1、安装libevent:官网(https://libevent.org/)下载稳定版本:libevent-2.1.12-stable.tar.gz
1.2、解压到指定目录(/app/libevent-2.1.12-stable),进入解压目录(/app/libevent-2.1.12-stable),参考(https://github.com/libevent/libevent)中的安装过程进行安装;
$ ./configure
$ make
$ make verify # (optional)
$ sudo make install
./configure 过成中提示
checking for openssl/ssl.h... no
configure: error: openssl is a must but can not be found. You should add the directory containing `openssl.pc' to the `PKG_CONFIG_PATH' environment variable, or set `CFLAGS' and `LDFLAGS' directly for openssl, or use `--disable-openssl' to disable support for openssl encryption
安装openssl然后重新执行安装命令,安装成功后可以在/usr/local/lib目录下看到libevent相关文件
1.3、安装openssl,下载openssl(git clone git://git.openssl.org/openssl.git),进入下载目录(/app/openssl)
1.4、执行安装命令
./Configure
make
make test
sudo make install
入门命令
https://www.runoob.com/memcached/memcached-get-data.html
启动命令:./memcached -d
帮助命令:./memcached -h
输出日志:./memcached -d -vv >> /tmp/memcached.log 2>&1
连接memcache: telnet localhost 11211 (telnet 连接之后可以执行 set、get、stats等命令)
Java代码demo
使用开源memcache客户端:https://github.com/gwhalin/Memcached-Java-Client/
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
public class MemcacheTest {
public static void main(String[] args) {
get("p");
}
private static void get(String key) {
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(new String[]{"localhost:11211"});
pool.initialize();
MemCachedClient memCachedClient = new MemCachedClient();
// memCachedClient.set(key, "hello world");
Object o = memCachedClient.get(key);
if (o != null) {
System.out.println(o.toString());
} else {
System.out.println(key + "= null");
}
}
}
maven 依赖:
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.2</version>
</dependency>
性能测试
本地环境默认参数设置的情况下:./memcache -d
读写速度约为5w/s
total 参数设置为 100w,执行时间约为20s
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
private static void batchTest(int total) {
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(new String[]{"localhost:11211"});
pool.setMaxConn(1000);
pool.setMinConn(20);
pool.initialize();
long start = System.currentTimeMillis();
CountDownLatch countDown = new CountDownLatch(total);
ExecutorService es = Executors.newFixedThreadPool(1000);
for (int i = 0; i < total; i++) {
es.submit(new Runnable() {
@Override
public void run() {
try {
MemCachedClient memCachedClient = new MemCachedClient();
Object o = memCachedClient.get(UUID.randomUUID().toString());
// System.out.println("执行结果=" + b);
} catch (Exception e) {
e.printStackTrace();
} finally {
countDown.countDown();
}
});
}
while (countDown.getCount() > 0) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("执行使用时间:" + (end - start));
es.shutdown();
}
}
待确认内容
flags 参数含义