本章学习 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. NOIP2016普及组解题报告

    概述 \(NOIP2016\)普及组的前三题都比较简单,第四题也有很多的暴力分,相信参加了的各位\(OIer\)在\(2016\)年都取得了很好的成绩. 那么,我将会分析\(NOIP2016\)普及组 ...

  2. mysql学习笔记(1)

    以下笔记并不系统,只是针对遇到的问题和特别的点记录一下: 数据类型: 1.mysql小数存储数据类型 有float double decimal ,前两个不属于精确类型,不推荐使用,一般生产库亦不会使 ...

  3. HTML学习(9)头部

    HTML <head> 元素 <head> 元素包含了所有的头部标签元素.在 <head>元素中你可以插入脚本(scripts), 样式文件(CSS),及各种met ...

  4. 查看Oracle的SID的方式

    1  使用组合键“Win + R”打开运行对话框,在输入框中输入 regedit 并回车打开“注册表编辑器”. 2   在“注册表编辑器”对话框,依次展开 HKEY_LOCAL_MACHINE\SOF ...

  5. makecert生成证书

    是要命令 makecert -r -pe -n "cn=musetowc" -$ commercial -a sha1 -b 01/01/2020 -e 01/01/2050 -c ...

  6. 【网易官方】极客战记(codecombat)攻略-地牢-轰轰

    关卡连接: https://codecombat.163.com/play/level/pong-pong 挑战:使用迄今为止学到的所有编程技巧编写最短的解决方案! 简介: 单挑,这是特殊的挑战关卡! ...

  7. Go错误

    1. error package main import ( "errors" "fmt" ) func main() { /* error:内置的数据类型,内 ...

  8. Python - 定时动态获取IP代理池,存放在文件中

    定时功能通过module time + 死循环实现,因为time.sleep()会自动阻塞 get_ip_pool.py """ @__note__: while Tru ...

  9. JAVA基础学习(7)之函数

    7函数 7.1函数定义与调用 7.1.1函数定义 7.1.2函数调用 package com.study.main; public class ObjectStudy { public static ...

  10. Python(一)list tuple dict set

    这篇文章是为了复习之前学的python的数据结构: 原文链接:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a ...