Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它经过在内存中缓存数据和对象来减小读取数据库的次数,从而提升动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,可是客户端能够用任何语言来编写,并经过memcached协议与守护进程通讯。
为了提升性能,memcached中保存的数据都存储在memcached内置的内存存储空间中。因为数据仅存在于内存中,所以重启memcached、重启操做系统会致使所有数据消失。另外,内容容量达到指定值以后,就基于LRU(Least Recently Used)算法自动删除不使用的缓存。memcached自己是为缓存而设计的服务器,所以并无过多考虑数据的永久性问题。html
参考教材https://www.runoob.com/memcached/window-install-memcached.htmljava
要先安装libevent库
sudo apt-get install libevent ibevent-devlinux
自动安装
sudo apt-get install memcachedgit
或者源代码的方式安装github
从其官方网站(http://memcached.org)下载memcached
解压源码 tar -zxvf memcached-1.x.x.tar.gz
进入目录
配置 ./configure --prefix=/path/to/memcached
编译 make && make test
安装 sudo make install算法
更多参考https://www.runoob.com/memcached/memcached-install.htmlspring
安装完成后memcached已经启动了,咱们先杀掉进程而后按照本身的方式做为后台服务启动。
/usr/bin/memcached -p 11211 -m 64m -d -u root
最后打开cmd,输入 telnet ip 11211来链接memcached进行测试。
注意: 个人自动安装后是在/usr/bin目录,实际状况要根据进程信息显示的目录为准。数据库
@Component
@ConfigurationProperties(prefix = "memcached")
public class MemcachedConfig {
private String ip;
private int port;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
@SpringBootApplication
public class MemcachedDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MemcachedDemoApplication.class, args);
}
final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private MemcachedConfig memcachedConfig;
@Bean(name = "memcachedClient")
public MemcachedClient getMemcachedClient(){
try {
MemcachedClient client = new MemcachedClient(new InetSocketAddress(memcachedConfig.getIp(), memcachedConfig.getPort()));
return client;
} catch (IOException e) {
logger.error("初始化MemcachedClient失败,{}",e.getMessage());
e.printStackTrace();
}
return null;
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class MemcachedDemoApplicationTests {
@Autowired
private MemcachedClient client;
@Test
public void contextLoads() {
client.set("testKey",1000,"testValue");
System.out.println("============testKey的值为:"+client.get("testKey"));
}
}
运行测试类MemcachedDemoApplicationTests中的contextLoads方法,控制台输出以下:ubuntu
2019-05-06 19:52:26.693 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/198.13.40.234:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2019-05-06 19:52:27.312 INFO 10348 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-06 19:52:27.668 INFO 10348 --- [ main] cn.sp.MemcachedDemoApplicationTests : Started MemcachedDemoApplicationTests in 2.966 seconds (JVM running for 4.14)
============testKey的值为:testValue
2019-05-06 19:52:28.052 INFO 10348 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2019-05-06 19:52:28.059 INFO net.spy.memcached.MemcachedConnection: Shut down memcached client
Memcached上手起来仍是比较简单,可是还有些其余命令须要熟悉,以及多台Memcached 的使用问题。
代码地址,点击这里。windows