目录:
Mongodb
淘宝用户数据
游戏装备数据、游戏道具数据
直播数据、打赏数据、粉丝数据
物联网数据
其他数据......
服务端启动
mongod --dbpath=. . \dataldb.
客户端启动
mongo --host=127.0.0.1 --port=27017
新增

修改

删除


导入Mongodb驱动

配置客户端

客户端读写Mongodb

Springboot17MongodbApplicationTest.class
- package com.example.sprintboot_17_mongodb;
-
- import com.example.sprintboot_17_mongodb.domain.Book;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.mongodb.core.MongoTemplate;
-
- import java.util.List;
-
- @SpringBootTest
- class Sprintboot17MongodbApplicationTests {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Test
- void contextLoads() {
- Book book = new Book();
- book.setId(1);
- book.setName("sprintboot");
- book.setType("sprintboot");
- book.setDesctiption("sprintboot");
- mongoTemplate.save(book);
- }
-
- @Test
- void find() {
- List
all = mongoTemplate.findAll(Book.class); - System.out.println(all);
- }
-
- }
Book.class
- package com.example.sprintboot_17_mongodb.domain;
-
- import lombok.Data;
-
- @Data
- public class Book {
- private int id;
- private String name;
- private String type;
- private String desctiption;
- }
applicaiton.yml
- spring:
- data:
- mongodb:
- uri: mongodb://localhost/itheima
Elasticsearch是一个分布式全文搜索引擎


Windows版ES下载
Windows版ES安装与启动

创建/查询/删除索引



IK分词器
创建索引并指定规则

创建文档


查询文档

条件查询

册除文档

修改文档(全量修改)


修改文档(部分修改)

导入坐标

配置

客户端

SpringBoat平台并没有跟随ES的更新速度进行同步更新,ES提供了High Level Client操作ES
导入坐标

客户端

客户端改进

application.yml
- spring:
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
- # elasticsearch:
- # rest:
- # uris: http://localhost:9200
-
- mybatis-plus:
- global-config:
- db-config:
- table-prefix: tbl_
- id-type: auto
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
Book.class
- package com.example.sprintboot_18_es.domain;
-
- import lombok.Data;
-
- @Data
- public class Book {
- private Integer id;
- private String type;
- private String name;
- private String description;
- }
BookDao.interfacer
- package com.example.sprintboot_18_es.dao;
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.example.sprintboot_18_es.domain.Book;
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- public interface BookDao extends BaseMapper
{ -
- }
Sprintboot18EsApplicationTests.class
- package com.example.sprintboot_18_es;
-
- import org.apache.http.HttpHost;
- import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestClientBuilder;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.junit.jupiter.api.AfterAll;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
-
- import java.io.IOException;
-
- @SpringBootTest
- class Sprintboot18EsApplicationTests {
-
- // @Autowired
- // private BookDao bookDao;
- //
- // @Test
- // void contextLoads() {
- // bookDao.selectById(1);
- // }
-
-
- // @Autowired
- // private ElasticsearchRestTemplate template;
-
- @BeforeEach
- void setUp() {
- HttpHost host = HttpHost.create("http://localhost:9200");
- RestClientBuilder builder = RestClient.builder(host);
- client = new RestHighLevelClient(builder);
- }
-
- @AfterEach
- void tearDown() throws IOException {
- client.close();
- }
-
-
- private RestHighLevelClient client;
-
- // @Test
- // void testCreateClient() throws IOException {
- // HttpHost host = HttpHost.create("http://localhost:9200");
- // RestClientBuilder builder = RestClient.builder(host);
- // client = new RestHighLevelClient(builder);
- // client.close();
- // }
-
-
- @Test
- void testCreateIndex() throws IOException {
- CreateIndexRequest request = new CreateIndexRequest("books");
- client.indices().create(request, RequestOptions.DEFAULT);
- }
- }
创建索引


添加文档

批量添加文档

Sprintboot18EsAoolicationTests.class
- package com.example.sprintboot_18_es;
-
- import com.alibaba.fastjson.JSON;
- import com.example.sprintboot_18_es.dao.BookDao;
- import com.example.sprintboot_18_es.domain.Book;
- import com.oracle.xmlns.internal.webservices.jaxws_databinding.XmlOneway;
- import org.apache.http.HttpHost;
- import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
- import org.elasticsearch.action.bulk.BulkRequest;
- import org.elasticsearch.action.bulk.BulkResponse;
- import org.elasticsearch.action.index.IndexRequest;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestClientBuilder;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.elasticsearch.common.xcontent.XContentType;
- import org.junit.jupiter.api.AfterAll;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- import java.io.IOException;
- import java.util.List;
-
- @SpringBootTest
- class Sprintboot18EsApplicationTests {
-
- @Autowired
- private BookDao bookDao;
- //
- // @Test
- // void contextLoads() {
- // bookDao.selectById(1);
- // }
-
-
- // @Autowired
- // private ElasticsearchRestTemplate template;
-
- @BeforeEach
- void setUp() {
- HttpHost host = HttpHost.create("http://localhost:9200");
- RestClientBuilder builder = RestClient.builder(host);
- client = new RestHighLevelClient(builder);
- }
-
- @AfterEach
- void tearDown() throws IOException {
- client.close();
- }
-
-
- private RestHighLevelClient client;
-
-
- // @Test
- // void testCreateClient() throws IOException {
- // HttpHost host = HttpHost.create("http://localhost:9200");
- // RestClientBuilder builder = RestClient.builder(host);
- // client = new RestHighLevelClient(builder);
- // client.close();
- // }
-
-
- @Test
- void testCreateIndex() throws IOException {
- CreateIndexRequest request = new CreateIndexRequest("books");
- client.indices().create(request, RequestOptions.DEFAULT);
- }
-
- @Test
- void testCreateIndexByIK() throws IOException {
- CreateIndexRequest request = new CreateIndexRequest("books");
- String json = "";
- request.source(json, XContentType.JSON);
- client.indices().create(request, RequestOptions.DEFAULT);
- }
-
- @Test
- void testCreateDoc() throws IOException {
- Book book = bookDao.selectById(1);
- IndexRequest request = new IndexRequest("books").id(book.getId().toString());
- String json = JSON.toJSONString(book);
- request.source(json, XContentType.JSON);
- client.index(request, RequestOptions.DEFAULT);
- }
-
-
- @Test
- void testCreateDocAll() throws IOException {
- List
bookList = bookDao.selectList(null); - BulkRequest bulk = new BulkRequest();
-
- for (Book book : bookList) {
- IndexRequest request = new IndexRequest("books").id(book.getId().toString());
- String json = JSON.toJSONString(book);
- request.source(json, XContentType.JSON);
- bulk.add(request);
- }
-
- client.bulk(bulk, RequestOptions.DEFAULT);
- }
-
-
- }
按id查询文档

按条件查询文档

Sprintboot18EsAoolicationTests.class
- package com.example.sprintboot_18_es;
-
- import com.alibaba.fastjson.JSON;
- import com.example.sprintboot_18_es.dao.BookDao;
- import com.example.sprintboot_18_es.domain.Book;
- import com.oracle.xmlns.internal.webservices.jaxws_databinding.XmlOneway;
- import org.apache.http.HttpHost;
- import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
- import org.elasticsearch.action.bulk.BulkRequest;
- import org.elasticsearch.action.bulk.BulkResponse;
- import org.elasticsearch.action.get.GetRequest;
- import org.elasticsearch.action.get.GetResponse;
- import org.elasticsearch.action.index.IndexRequest;
- import org.elasticsearch.action.search.SearchRequest;
- import org.elasticsearch.action.search.SearchResponse;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestClientBuilder;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.elasticsearch.common.xcontent.XContentType;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.elasticsearch.search.SearchHit;
- import org.elasticsearch.search.SearchHits;
- import org.elasticsearch.search.builder.SearchSourceBuilder;
- import org.junit.jupiter.api.AfterAll;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- import java.io.IOException;
- import java.util.List;
-
- @SpringBootTest
- class Sprintboot18EsApplicationTests {
-
- @Autowired
- private BookDao bookDao;
- //
- // @Test
- // void contextLoads() {
- // bookDao.selectById(1);
- // }
-
-
- // @Autowired
- // private ElasticsearchRestTemplate template;
-
- @BeforeEach
- void setUp() {
- HttpHost host = HttpHost.create("http://localhost:9200");
- RestClientBuilder builder = RestClient.builder(host);
- client = new RestHighLevelClient(builder);
- }
-
- @AfterEach
- void tearDown() throws IOException {
- client.close();
- }
-
-
- private RestHighLevelClient client;
-
-
- // @Test
- // void testCreateClient() throws IOException {
- // HttpHost host = HttpHost.create("http://localhost:9200");
- // RestClientBuilder builder = RestClient.builder(host);
- // client = new RestHighLevelClient(builder);
- // client.close();
- // }
-
-
- @Test
- void testCreateIndex() throws IOException {
- CreateIndexRequest request = new CreateIndexRequest("books");
- client.indices().create(request, RequestOptions.DEFAULT);
- }
-
- @Test
- void testCreateIndexByIK() throws IOException {
- CreateIndexRequest request = new CreateIndexRequest("books");
- String json = "";
- request.source(json, XContentType.JSON);
- client.indices().create(request, RequestOptions.DEFAULT);
- }
-
- @Test
- void testCreateDoc() throws IOException {
- Book book = bookDao.selectById(1);
- IndexRequest request = new IndexRequest("books").id(book.getId().toString());
- String json = JSON.toJSONString(book);
- request.source(json, XContentType.JSON);
- client.index(request, RequestOptions.DEFAULT);
- }
-
-
- @Test
- void testCreateDocAll() throws IOException {
- List
bookList = bookDao.selectList(null); - BulkRequest bulk = new BulkRequest();
-
- for (Book book : bookList) {
- IndexRequest request = new IndexRequest("books").id(book.getId().toString());
- String json = JSON.toJSONString(book);
- request.source(json, XContentType.JSON);
- bulk.add(request);
- }
-
- client.bulk(bulk, RequestOptions.DEFAULT);
- }
-
- @Test
- void testGet() throws IOException {
- GetRequest request = new GetRequest("books", "1");
- GetResponse response = client.get(request, RequestOptions.DEFAULT);
- String json = response.getSourceAsString();
- System.out.println(json);
- }
-
- @Test
- void testSearch() throws IOException {
- SearchRequest request = new SearchRequest("books");
- SearchSourceBuilder builder = new SearchSourceBuilder();
- builder.query(QueryBuilders.termQuery("name", "spring"));
- request.source(builder);
- SearchResponse response = client.search(request, RequestOptions.DEFAULT);
- SearchHits hits = response.getHits();
- for (SearchHit hit : hits) {
- String source = hit.getSourceAsString();
- System.out.println(source);
- Book book = JSON.parseObject(source, Book.class);
- System.out.println(book);
- }
- }
-
-
- }
pom.xml
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.springframework.boot -
spring-boot-starter-parent -
2.5.4 -
-
-
com.example -
sprintboot_18_es -
0.0.1-SNAPSHOT -
sprintboot_18_es -
sprintboot_18_es -
-
1.8 -
-
-
-
org.springframework.boot -
spring-boot-starter -
-
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
com.baomidou -
mybatis-plus-boot-starter -
3.4.1 -
-
-
com.alibaba -
druid-spring-boot-starter -
1.2.6 -
-
-
mysql -
mysql-connector-java -
-
-
org.projectlombok -
lombok -
-
-
-
-
-
-
org.elasticsearch.client -
elasticsearch-rest-high-level-client -
-
-
-
com.alibaba -
fastjson -
1.2.78 -
-
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
paketobuildpacks/builder-jammy-base:latest -
-
-
-
-
-

BookController.class
- package com.example.sprintboot_19_cache.controller;
-
- import com.example.sprintboot_19_cache.domain.Book;
- import com.example.sprintboot_19_cache.service.BookService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
-
- @RestController
- @RequestMapping("/books")
- public class BookController {
- @Autowired
- private BookService bookService;
-
- @GetMapping("{id}")
- public Book getById(@PathVariable Integer id) {
- return bookService.getById(id);
- }
-
- @PostMapping
- public boolean save(@RequestBody Book book) {
- return bookService.save(book);
- }
-
- @PutMapping
- public boolean update(@RequestBody Book book) {
- return bookService.update(book);
- }
-
- @DeleteMapping("{id}")
- public boolean delete(@PathVariable Integer id) {
- return bookService.delete(id);
- }
-
- @GetMapping
- public List
getAll() { - return bookService.getAll();
- }
- }
MsgController.class
- package com.example.sprintboot_19_cache.controller;
-
- import com.example.sprintboot_19_cache.service.MsgService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- @RestController
- @RequestMapping("/msg")
- public class MsgController {
-
- @Autowired
- private MsgService msgService;
-
- @GetMapping("{tele}")
- public String getById(@PathVariable String tele) {
- return msgService.get(tele);
- }
-
- @PostMapping
- public boolean check(String tele, String code) {
- return msgService.check(tele, code);
- }
- }
BookDao.class
- package com.example.sprintboot_19_cache.dao;
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.example.sprintboot_19_cache.domain.Book;
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- public interface BookDao extends BaseMapper
{ - }
Book.class
- package com.example.sprintboot_19_cache.domain;
-
- import lombok.Data;
-
- @Data
- public class Book {
- private Integer id;
- private String type;
- private String name;
- private String description;
- }
BookServiceImpl.class
- package com.example.sprintboot_19_cache.service.impl;
-
- import com.example.sprintboot_19_cache.dao.BookDao;
- import com.example.sprintboot_19_cache.domain.Book;
- import com.example.sprintboot_19_cache.service.BookService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.HashMap;
- import java.util.List;
-
- @Service
- public class BookServiceImpl implements BookService {
- @Autowired
- private BookDao bookDao;
-
-
- private HashMap
cache = new HashMap(); -
- @Override
- public Book getById(Integer id) {
- Book book = cache.get(id);
- if (book == null) {
- Book queryBook = bookDao.selectById(id);
- cache.put(id, queryBook);
- return queryBook;
- }
- return cache.get(id);
- }
-
- @Override
- public boolean save(Book book) {
- return bookDao.insert(book) > 0;
- }
-
- @Override
- public boolean update(Book book) {
- return bookDao.updateById(book) > 0;
- }
-
- @Override
- public boolean delete(Integer id) {
- return bookDao.deleteById(id) > 0;
- }
-
- @Override
- public List
getAll() { - return bookDao.selectList(null);
- }
- }
MsgServiceImpl.class
- package com.example.sprintboot_19_cache.service.impl;
-
- import com.example.sprintboot_19_cache.service.MsgService;
- import org.springframework.stereotype.Service;
-
- import java.util.HashMap;
-
- @Service
- public class MsgServiceImpl implements MsgService {
-
- private HashMap
cache = new HashMap(); -
- @Override
- public String get(String tele) {
- String code = tele.substring(tele.length() - 6);
- cache.put(tele, code);
- return code;
- }
-
- @Override
- public boolean check(String tele, String code) {
- String queryCode = cache.get(tele);
- return code.equals(queryCode);
- }
- }
BookService.class
- package com.example.sprintboot_19_cache.service;
-
- import com.example.sprintboot_19_cache.domain.Book;
-
- import java.util.List;
-
- public interface BookService {
- public boolean save(Book book);
-
- public Book getById(Integer id);
-
- public boolean update(Book book);
-
- public boolean delete(Integer id);
-
- public List
getAll(); - }
MsgService.class
- package com.example.sprintboot_19_cache.service;
-
- public interface MsgService {
- public String get(String tele);
-
- public boolean check(String tele, String code);
- }
application.yml
- spring:
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
-
-
- mybatis-plus:
- global-config:
- db-config:
- table-prefix: tbl_
- id-type: auto
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
- server:
- port: 8080
-
pom.xml
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.springframework.boot -
spring-boot-starter-parent -
2.5.4 -
-
-
com.example -
sprintboot_19_cache -
0.0.1-SNAPSHOT -
sprintboot_19_cache -
sprintboot_19_cache -
-
1.8 -
-
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
org.projectlombok -
lombok -
-
-
com.baomidou -
mybatis-plus-boot-starter -
3.4.3 -
-
-
com.alibaba -
druid-spring-boot-starter -
1.2.6 -
-
-
mysql -
mysql-connector-java -
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
paketobuildpacks/builder-jammy-base:latest -
-
-
-
-
-
导入缓存技术对应的starter

设置当前操作的结果数据进入缓存
springBoot启用缓存的方式
缓存使用案例——手机验证码
需求
需求分析
开启缓存

业务层接口
业务层设置获取验证码操作,并存储缓存,手机号为key,验证码为value
业务层设置校验验证码操作,校验码通过缓存读取,返回校验结果


pom.xml
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.springframework.boot -
spring-boot-starter-parent -
2.5.4 -
-
-
com.example -
sprintboot_19_cache -
0.0.1-SNAPSHOT -
sprintboot_19_cache -
sprintboot_19_cache -
-
1.8 -
-
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
org.projectlombok -
lombok -
-
-
com.baomidou -
mybatis-plus-boot-starter -
3.4.3 -
-
-
com.alibaba -
druid-spring-boot-starter -
1.2.6 -
-
-
mysql -
mysql-connector-java -
-
-
org.springframework.boot -
spring-boot-starter-cache -
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
paketobuildpacks/builder-jammy-base:latest -
-
-
-
-
-
applicaiton.yml
- spring:
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
-
-
- mybatis-plus:
- global-config:
- db-config:
- table-prefix: tbl_
- id-type: auto
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
- server:
- port: 8080
-
CodeUtils.class
- package com.example.sprintboot_19_cache.utils;
-
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.stereotype.Component;
-
- @Component
- public class CodeUtils {
-
- private String[] patch = {"000000", "00000", "0000", "000", "00", "0", ""};
-
- public String generator(String tele) {
- int hash = tele.hashCode();
- int encryption = 20206666;
- long result = hash ^ encryption;
- long nowTime = System.currentTimeMillis();
- result = result ^ nowTime;
- long code = result % 1000000;
- code = code < 0 ? -code : code;
- String codeStr = code + "";
- int len = codeStr.length();
-
- return patch[len] + codeStr;
- }
-
-
- @Cacheable(value = "smsCode", key = "#tele")
- public String get(String tele) {
- return null;
- }
-
- public static void main(String[] args) {
- System.out.println(new CodeUtils().generator("15033657967"));
- }
- }
SMSCodeService.interface
- package com.example.sprintboot_19_cache.service;
-
- import com.example.sprintboot_19_cache.domain.SMSCode;
-
- public interface SMSCodeService {
- public String sendCodeToSMS(String tele);
-
- public boolean checkCode(SMSCode smsCode);
- }
SMSCodeServiceImpl.class
- package com.example.sprintboot_19_cache.service.impl;
-
- import com.example.sprintboot_19_cache.domain.SMSCode;
- import com.example.sprintboot_19_cache.service.SMSCodeService;
- import com.example.sprintboot_19_cache.utils.CodeUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cache.annotation.CachePut;
- import org.springframework.stereotype.Service;
-
- @Service
- public class SMSCodeServiceImpl implements SMSCodeService {
-
- @Autowired
- private CodeUtils codeUtils;
-
- @Override
- @CachePut(value = "smsCode", key = "#tele")
- public String sendCodeToSMS(String tele) {
- String code = codeUtils.generator(tele);
- return code;
- }
-
- @Override
- public boolean checkCode(SMSCode smsCode) {
- String code = smsCode.getCode();
- String cacheCode = codeUtils.get(smsCode.getTele());
- return code.equals(cacheCode);
- }
-
- }
SMSCode.class
- package com.example.sprintboot_19_cache.domain;
-
- import lombok.Data;
-
- @Data
- public class SMSCode {
- private String tele;
- private String code;
- }
SMSCodeController.class
- package com.example.sprintboot_19_cache.controller;
-
- import com.example.sprintboot_19_cache.domain.SMSCode;
- import com.example.sprintboot_19_cache.service.SMSCodeService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/sms")
- public class SMSCodeController {
-
- @Autowired
- private SMSCodeService smsCodeService;
-
- @GetMapping
- public String getCode(String tele) {
- String code = smsCodeService.sendCodeToSMS(tele);
- return code;
- }
-
- @PostMapping
- public boolean checkCode(SMSCode smsCode) {
- return smsCodeService.checkCode(smsCode);
- }
- }
加入Ehcache坐标(缓存供应商实现)

pom.xml
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.springframework.boot -
spring-boot-starter-parent -
2.5.4 -
-
-
com.example -
sprintboot_19_cache -
0.0.1-SNAPSHOT -
sprintboot_19_cache -
sprintboot_19_cache -
-
1.8 -
-
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
org.projectlombok -
lombok -
-
-
com.baomidou -
mybatis-plus-boot-starter -
3.4.3 -
-
-
com.alibaba -
druid-spring-boot-starter -
1.2.6 -
-
-
mysql -
mysql-connector-java -
-
-
org.springframework.boot -
spring-boot-starter-cache -
-
-
net.sf.ehcache -
ehcache -
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
paketobuildpacks/builder-jammy-base:latest -
-
-
-
-
-
缓存设定为使用Ehcache
applicaiton.yml
- spring:
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
- cache:
- type: ehcache
- ehcache:
- config: ehcache.xml
-
- mybatis-plus:
- global-config:
- db-config:
- table-prefix: tbl_
- id-type: auto
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
- server:
- port: 8080
-
提供ehcache配置文件ehcache.xml

ehcache.xml
- "1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance" - xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
- updateCheck="false">
-
"F:\ehcache"/> -
- eternal="false"
- diskPersistent="false"
- maxElementsInMemory="1000"
- overflowToDisk="false"
- timeToIdleSeconds="60"
- timeToLiveSeconds="60"
- memoryStoreEvictionPolicy="LRU"/>
-
-
- name="smsCode"
- eternal="false"
- diskPersistent="false"
- maxElementsInMemory="1000"
- overflowToDisk="false"
- timeToIdleSeconds="10"
- timeToLiveSeconds="10"
- memoryStoreEvictionPolicy="LRU"/>
17.数据淘汰策略
18.变更缓存供应商Redis
加入Redis坐标(缓存供应商实现)
pom.xml
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.springframework.boot -
spring-boot-starter-parent -
2.5.4 -
-
-
com.example -
sprintboot_19_cache -
0.0.1-SNAPSHOT -
sprintboot_19_cache -
sprintboot_19_cache -
-
1.8 -
-
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
org.projectlombok -
lombok -
-
-
com.baomidou -
mybatis-plus-boot-starter -
3.4.3 -
-
-
com.alibaba -
druid-spring-boot-starter -
1.2.6 -
-
-
mysql -
mysql-connector-java -
-
-
org.springframework.boot -
spring-boot-starter-cache -
-
-
net.sf.ehcache -
ehcache -
-
-
org.springframework.boot -
spring-boot-starter-data-redis -
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
paketobuildpacks/builder-jammy-base:latest -
-
-
-
-
-
配置Redis服务器,缓存设定为使用Redis.

设置Redis相关配置

applicaiton.yml
- #spring:
- # datasource:
- # druid:
- # driver-class-name: com.mysql.cj.jdbc.Driver
- # url: jdbc:mysql://localhost:3308/test_db
- # username: root
- # password: 666666
- # cache:
- # type: ehcache
- # ehcache:
- # config: ehcache.xml
-
- mybatis-plus:
- global-config:
- db-config:
- table-prefix: tbl_
- id-type: auto
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
- server:
- port: 8080
-
- spring:
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3308/test_db
- username: root
- password: 666666
- cache:
- type: redis
- redis:
- time-to-live: 10s
- cache-null-values: false
- # key-prefix: sms_
- # use-key-prefix: false
-
- redis:
- host: localhost
- port: 6379
19.memcached下载与安装
下载memcached
- 地址: https://www.runoob.com/memcached/window-install-memcached.html
安装memcached
- 使用管理员身份运行cmd指令
安装
- memcached.exe -d install
运行memcached
- 启动服务
- memcached.exe -d start
- 停止服务
- memcached.exe -d stop
20. 变更缓存供应商memcached
- memcached客户端选择
- Memcached client for Java:最早期客户端,稳定可靠,用户群广
- SpyMemcached:效率更高
- xmemcached:并发处理更好
- SpningBoot未提供对memcached的整合,需要使用硬编码方式实现客户端初始化管理
加入Xmemcache坐标(缓存供应商实现)

配置memcached服务器必要属性

创建读取属性配置信息类,加载配置

创建客户端配置类

配置memcached属性

-
相关阅读:
【Python入门级】#基础篇#文章目录概览汇总
jvm 内存结构 ^_^
「 程序员的理财与风险控制」让财富跟你一起持续成长:增额终身寿
更简洁的参数校验,使用 SpringBoot Validation 对参数进行校验
黑盒测试用例设计 - 等价类划分法
22071.11.28
AI量化策略会:可以直接上实盘的策略构建方法
vue 配置绕过跨域问题
jupyter notebook中查看python版本的解决方案
No matching version found for zr-map-ol@1.1.19.
-
原文地址:https://blog.csdn.net/qq_56444564/article/details/134480190