本章学习 Mmecached 在 Spring Boot 中的使用教程。Memcached 与 Redis 各有好处。本文主要学习 Spring Boot 中如何应用集成 Mmecached

  • spring boot 1.5.x/2.x
  • memcached
  • jdk 1.8+

本项目源码下载

1 安装 memcached

window 下安装比较方便,直接 双击 exe 安装文件即可;

mac 下安装使用命令行安装

brew install libmemcached
brew install memcached
rew services start memcached

注意,如果遇到 update .... 需要等待很久的,就请按组合键 command+c 取消brew的更新,直接执行命令。

2 新建 Spring Boot Maven 示例工程项目

注意:是用来 IDEA 开发工具

  1. File > New > Project,如下图选择 Spring Initializr 然后点击 【Next】下一步
  2. 填写 GroupId(包名)、Artifact(项目名) 即可。点击 下一步

    groupId=com.fishpro

    artifactId=memcached
  3. 选择依赖 Spring Web Starter 前面打钩。
  4. 项目名设置为 spring-boot-study-memcached.

3 引入依赖 Pom

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/net.spy/spymemcached add by fishpro at 2019-08-07-->
<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

4 配置 Memcached 对 MemcachedClient进行初始化

MemcacheConfig 路径 src/main/java/com/fishpro/memcached/config/RedisController(路径.java)

package com.fishpro.memcached.config;

import net.spy.memcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.io.IOException;
import java.net.InetSocketAddress; @EnableCaching
@Configuration
public class MemcacheConfig extends CachingConfigurerSupport { @Value("${memcached.port}")
private Integer port;
@Value("${memcached.ip}")
private String ip;
@Bean
public MemcachedClient memcachedClient(){
try {
return new MemcachedClient(new InetSocketAddress(ip,port));
}catch (IOException e){
e.printStackTrace();
return null;
}
}
}

5 编写RestController测试代码

不太习惯使用单元测试,这里给出 RestController 的测试代码

方法 作用
set(key,value) 向key中添加值如果存就替换
add(key,value) 向key中添加值如果存在就不替换
replace(key,value) 替换缓存key的值为value
delete(key) 删除缓存key的值

注意生效时间单位秒

@RestController
public class UserController { @Autowired
private MemcachedClient memcachedClient; @RequestMapping("/test")
public String test(){
System.out.println("======set/get 方式演示===============================");
memcachedClient.set("FPCACHE",3,"THIS IS TEST 这是测试");
System.out.println("设置与读取 FPCACHE 值:"+memcachedClient.get("FPCACHE")); memcachedClient.set("FPCACHE",3,"使用SET添加到一个存在的值的缓存");
System.out.println("再次读取 FPCACHE 值:"+memcachedClient.get("FPCACHE")); System.out.println("======add 方式演示===============================");
memcachedClient.add("FPCACHE",3,"使用ADD添加到一个存在的值的缓存");
System.out.println("再次读取 FPCACHE 值:"+memcachedClient.get("FPCACHE")); memcachedClient.add("FPCACHE2",3,"使用ADD添加到新的缓存键FPCACHE2中");
System.out.println("再次读取 FPCACHE2 值:"+memcachedClient.get("FPCACHE2")); System.out.println("======replace 方式演示===============================");
memcachedClient.replace("FPCACHE",3,"使用Replace替换FPCACHE键对应的缓存值");
System.out.println("replace方式读取 FPCACHE 值:"+memcachedClient.get("FPCACHE")); try {
Thread.sleep(3001);
}catch (Exception ex){}
System.out.println("3秒过后再次获取缓存 FPCACHE: "+memcachedClient.get("FPCACHE")); System.out.println("======delete 方式演示===============================");
memcachedClient.delete("FPCACHE");
System.out.println("replace方式读取 FPCACHE 值:"+memcachedClient.get("FPCACHE")); return "";
}
}

6 运行示例

右键 MemcachedApplication 选择 Run MemcachedApplication 在浏览器中输入 http://localhost:8080/test

本项目源码下载


参考

官方 Memcached github

Spring Boot 缓存应用 Memcached 入门教程的更多相关文章

  1. Spring Boot 缓存应用 Ehcache 入门教程

    Ehcache 小巧轻便.具备持久化机制,不用担心JVM和服务器重启的数据丢失.经典案例就是著名的Hibernate的默认缓存策略就是用Ehcache,Liferay的缓存也是依赖Ehcache. 本 ...

  2. Spring Boot 2.0.1 入门教程

    简介 Spring Boot是Spring提供的一套基础配置环境,可以用来快速开发生产环境级别的产品.尤其适合开发微服务架构,省去了不少配置麻烦.比如用到Spring MVC时,只需把spring-b ...

  3. spring boot集成redis基础入门

    redis 支持持久化数据,不仅支持key-value类型的数据,还拥有list,set,zset,hash等数据结构的存储. 可以进行master-slave模式的数据备份 更多redis相关文档请 ...

  4. Spring Boot 2.x 快速入门(下)HelloWorld示例详解

    上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...

  5. Spring Boot 单元测试详解+实战教程

    Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring-boot-test:支持测试的核心内容. spring-b ...

  6. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

  7. Spring Boot缓存Ehcache

    Spring Boot 整合 Ehcache   修改 pom 文件 <!-- Spring Boot 缓存支持启动器 --> <dependency> <groupId ...

  8. 3步轻松搞定Spring Boot缓存

    作者:谭朝红 前言 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能.在Spring Boot应用程序中,我们可以通过Spring Caching来快速 ...

  9. Spring Boot:使用Memcached缓存

    综合概述 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统.Memcached基于内存的key-value存储,用来存储小块的任意数据,这些数据可以是数据库调用.API调用或者是页面 ...

随机推荐

  1. 搭建Springboot监控中心报错A attempt was made to call the method reactor.retry.Retry.retryMax(I)Lreactor/ret)

    服务器还没启动就报错,是因为jar包的版本没对上,看的视频是SpringBoot2.0 ,现在已经是2.1.7了 将spring-boot-admin-starter-server版本改为最新就ok了

  2. python 字符串的一些函数

    split()函数 split()      以 空格 为分割符分割字符串,返回列表 split('_')   以'_'为分割符分割字符串,返回列表 strip() 函数  去掉前后的空格 下面是字符 ...

  3. python如何将自己写的代码打包供他人使用

    背景: 利用setuptools来实现,所以先安装setuptools,其实Python2.7.9及之后的版本都自带安装setuptools了,无需在另外安装 如果没有安装setuptools的直接下 ...

  4. centos7安装Nginx 配置及反向代理

    Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.Ngin ...

  5. 搭建 Kubernetes 高可用集群

    使用 3 台阿里云服务器(k8s-master0, k8s-master1, k8s-master2)作为 master 节点搭建高可用集群,负载均衡用的是阿里云 SLB ,需要注意的是由于阿里云负载 ...

  6. [转] Go 的并发模式:Context

    [转] Go 的并发模式:Context tips:昨天看了飞雪无情的关于 Context 的文章,对 go 中 Context 有了一个初步的认识.今天看到一个 go 官方博客的关于 Context ...

  7. [python] VSCode+Jupyter 安装步骤以及注意事项

    1. 安装Python2. 安装Jupyter, pip install 安装Jupyter(若使用Anaconda,则需要将其添加到环境变量中)3. 将Python的Scripts文件夹添加到系统环 ...

  8. C语言 exit

    C语言 exit 在main函数中调用exit和return结果是一样的,但在子函数中调用return只是代表子函数终止了,在子函数中调用exit,那么程序终止. 案例 #include <st ...

  9. Springboot中定时任务的使用

    在springboot中已经集成了定时任务,只需要在启动类上加注解@EnableScheduling即可 例如: 添加类加上@Component注解,添加方法加上@Scheduler即可

  10. Python 多任务(线程) day2 (1)

    结论:多线程全局变量是共享的 (03) 因为多线程一般是配合使用,如果不共享,那么就要等到一个线程执行完,再把变量传递给另一个线程,就变成单线程了 但是如果多个线程同时需要修改一个全局变量,就会出现资 ...