• Memcached 的安装与使用;SpringBoot 整合 Memcached 代码详解


    Memcached 的安装与使用;SpringBoot 整合 Memcached 代码详解

    - Memcached 的下载安装:
    • 下载地址为:http://static.runoob.com/download/memcached-win64-1.4.4-14.zip
    • 安装:以管理员身份打开 CMD:执行下列命令安装 Memcached。
      在这里插入图片描述
    • Memcached 的启停:
      在这里插入图片描述
    - Memcached 的三种客户端介绍:
    • Memcached Client For Java:最早期客户端,稳定可靠,用户多。
    • SpyMemcached:效率更高。
    • Xmemcached:并发处理更好。
    - SpringBoot 未提供对 Memcached 的整合,需要使用硬编码方式实现客户端初始化管理。
    • 目录结构如下:
      在这里插入图片描述
    - 通过配置类注册 Bean:
    // XMemcachedConfig.java
    import net.rubyeye.xmemcached.MemcachedClient;
    import net.rubyeye.xmemcached.MemcachedClientBuilder;
    import net.rubyeye.xmemcached.XMemcachedClientBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class XMemcachedConfig {
        
        @Bean
        public MemcachedClient memcachedClient(){
            MemcachedClient memcachedClient = null;
            try {
                MemcachedClientBuilder builder = new XMemcachedClientBuilder("localhost:11211");
                memcachedClient = builder.build();
            }catch (Exception e){
                e.printStackTrace();
            }
            return memcachedClient;
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    - 实体类 Book.java
    package com.example.springboot.entity;
    
    import lombok.*;
    
    import java.io.Serializable;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Book implements Serializable {
    
        private String id;
        private String name;
        private String description;
        private Float price;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    - 在业务层使用 MemcachedClient 如下:
    // BookService.java
    import com.baomidou.mybatisplus.extension.service.IService;
    import com.example.springboot.entity.Book;
    
    public interface BookService extends IService<Book> {
        Book setCacheById(String id);
        Boolean checkCacheById(String id, String name);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    // BookServiceImpl.java
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.example.springboot.dao.BookDao;
    import com.example.springboot.entity.Book;
    import com.example.springboot.service.BookService;
    import net.rubyeye.xmemcached.MemcachedClient;
    import net.rubyeye.xmemcached.XMemcachedClient;
    import net.rubyeye.xmemcached.exception.MemcachedException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.concurrent.TimeoutException;
    
    @Service
    public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements BookService {
    
        @Autowired
        private MemcachedClient memcachedClient;
    
        @Override
        public Book setCacheById(String id) {
            try {
            	// 往 memcached 中放数据,第一个参数为key
            	// 第二个参数为过期时间(0表示永不过期)
            	// 第三个参数为要存放到缓存中的数据(需实现序列化)
                memcachedClient.set(id, 0, getById(id));
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (MemcachedException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        public Boolean checkCacheById(String id, String name) {
            Book cacheData = null;
            try {
            	// 根据key, 从缓存中取数据
                cacheData = memcachedClient.get(id);
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (MemcachedException e) {
                e.printStackTrace();
            }
            return name.equals(cacheData.getName());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    - 表现层 BookController.java 代码:
    import com.example.springboot.entity.Book;
    import com.example.springboot.service.BookService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @RequestMapping("/books")
    public class BookController {
    
        @Autowired
        private BookService bookService;
    
        @GetMapping("{id}")
        public Book getById(@PathVariable String id){
            return bookService.setCacheById(id);
        }
    
        @PostMapping
        public Boolean checkById(@RequestBody Book book){
            return bookService.checkCacheById(book.getId(), book.getName());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    - 通过 Postman 测试如下:
    • 往 Memcached 中放数据:
      在这里插入图片描述

    • 从 Memcached 中取数据:
      在这里插入图片描述

  • 相关阅读:
    【创建公共的swagger3组件】Spring Cloud 14
    python爬虫6
    iSpring SDK 9.7 AND iSpring SDK 10.2 AND iSpring SDK 8.7
    一个翻翻的小游戏。HTML式。
    labview下位机软件编程笔记
    dvwa-暴力破解(low-high)
    OpenFOAM:并行区域划分理解(Domain Decomposition)
    从私服上拉取jar包,就是拉取不下来
    SSM框架学习——SqlSession以及Spring与MyBatis整合
    金融壹账通拟7月4日香港上市:2年亏近30亿 市值蒸发超90%
  • 原文地址:https://blog.csdn.net/qq_38132105/article/details/126089385