Welcome everyone

centOS7 memcache 安装与使用

java 汪明鑫 577浏览 0评论
[root@xinyeshuaiqi ~]# yum install memcached

[root@xinyeshuaiqi ~]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

[root@xinyeshuaiqi ~]# systemctl start memcached 
[root@xinyeshuaiqi ~]# systemctl enable memcached
Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /usr/lib/systemd/system/memcached.service.

[root@xinyeshuaiqi ~]# memcached -h

[root@xinyeshuaiqi ~]# ps -ef |grep memcached
memcach+ 11991     1  0 15:52 ?        00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
root     12036 11833  0 15:54 pts/1    00:00:00 grep --color=auto memcached

 

PORT :Memcached用来运行的端口。

USER :Memcached服务的启动守护程序。

MAXCONN :用于将最大同时连接数设置为1024的值 。 对于繁忙的Web服务器,您可以根据需要增加任何数量。

CACHESIZE :将高速缓存大小内存。 对于繁忙的服务器,您最多可以增加4GB 。

选项 :设置服务器的IP地址,以便ApacheNginx Web服务器可以连接到它。

 

 

memcache 安装成功,用Java程序连接跑一下

先在application.properties配置memcache的ip和port

memcache.ip=
memcache.port=11211

 

定义一个memcache配置类

package pers.wmx.springbootfreemarkerdemo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author: wangmingxin03
 * @date: 2020-04-13
 */
@Component
public class MemcacheSource {
    @Value("${memcache.ip}")
    private String ip;

    @Value("${memcache.port}")
    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;
    }
}

 

package pers.wmx.springbootfreemarkerdemo;

import net.spy.memcached.MemcachedClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import pers.wmx.springbootfreemarkerdemo.config.MemcacheSource;

import javax.annotation.Resource;
import java.net.InetSocketAddress;

/**
 * @author: wangmingxin03
 * @date: 2020-04-13
 */
@Component
public class MemcachedRunner implements CommandLineRunner {

    @Resource
    private MemcacheSource memcacheSource;

    private MemcachedClient client = null;

    @Override
    public void run(String... args) throws Exception {
        client = new MemcachedClient(new InetSocketAddress(memcacheSource.getIp(),memcacheSource.getPort()));
    }

    public MemcachedClient getClient(){
        return client;
    }
}

 

跑一波单测

package pers.wmx.springbootfreemarkerdemo;

import lombok.extern.slf4j.Slf4j;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.ExecutionException;

/**
 * @author: wangmingxin03
 * @date: 2020-04-13
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MemcachedTest {

    @Autowired
    private MemcachedRunner memcachedRunner;

    @Test
    public void testMemcache() throws ExecutionException, InterruptedException {
        MemcachedClient memcachedClient = memcachedRunner.getClient();
        OperationFuture<Boolean> set = memcachedClient.set("hello", 1000, "world");
        System.out.println("set结果: " + set.get());

        System.out.println("get: " + memcachedClient.get("hello").toString());
    }


}

 

输出:

set结果: true
get: world

 

 

转载请注明:汪明鑫的个人博客 » centOS7 memcache 安装与使用

喜欢 (0)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz