sprnigboot集成Memcached

安装Memcached

下载地址
32位系统 1.2.5版本:http://static.jyshare.com/download/memcached-1.2.5-win32-bin.zip
32位系统 1.2.6版本:http://static.jyshare.com/download/memcached-1.2.6-win32-bin.zip
32位系统 1.4.4版本:http://static.jyshare.com/download/memcached-win32-1.4.4-14.zip
64位系统 1.4.4版本:http://static.jyshare.com/download/memcached-win64-1.4.4-14.zip
32位系统 1.4.5版本:http://static.jyshare.com/download/memcached-1.4.5-x86.zip
64位系统 1.4.5版本:http://static.jyshare.com/download/memcached-1.4.5-amd64.zip
解压,点击memcached.exe
在这里插入图片描述
参考文章
https://www.runoob.com/memcached/window-install-memcached.html

集成sprnigboot

pom

<!-- https://mvnrepository.com/artifact/net.spy/spymemcached -->
<dependency>
    <groupId>net.spy</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.12.3</version>
</dependency>

application.properties

memcache.ip=127.0.0.1
memcache.port=11211

添加一个MemcacheConfig配置类读取主机端口并构造一个MemcachedClient。

import java.io.IOException;
import java.net.InetSocketAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import net.spy.memcached.MemcachedClient;

@Configuration
public class MemcacheConfig {

    @Value("${memcache.ip}")
    private String ip;

    @Value("${memcache.port}")
    private int port;


    @Bean
    public MemcachedClient getClient() {
        MemcachedClient memcachedClient = null;
        try {
            memcachedClient  = new MemcachedClient(new InetSocketAddress(ip, port));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return memcachedClient;
    }
}

编写一个业务控制器,通过MemcachedClient实现对缓存的设置和读取。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;


@RestController
public class MemcacheController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private MemcachedClient memcachedClient;

    @GetMapping("/info")
    public String memcacheGetValue() throws InterruptedException {
        // 取出缓存
        Object value = memcachedClient.get("userName");
        logger.info("取出缓存 "+value);
        return "取出的值  "+value;
    }

    @GetMapping("/save")
    public String saveValue(@RequestParam String userName) throws InterruptedException {
        // 放入缓存, 过期时间为5000,单位为毫秒
        OperationFuture<Boolean> flag = memcachedClient.set("userName", 5000, userName);
        return "保存成功";
    }
}

参考文章
https://zhuanlan.zhihu.com/p/611172374

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值