edis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。——百度百科
二、Spring Boot 对 MongoDB 和 Redis 的支持
1、对 MongoDB 的支持
Spring 框架中提供了一个 Spring Data MongoDB 来对 MongoDB 数据库进行支持,并提供 Object/Document 映射注解支持、MongoDBTemplate 和 Repository 支持。
新建一个 Spring 项目,仍旧是用 IDEA 的项目新建向导,初始依赖选择 MongoDB 和 Web,项目的 POM 文件如下:
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.0.M2</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.pyc</groupId><artifactId>mongo</artifactId><version>0.0.1-SNAPSHOT</version><name>mongo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
使用 Spring Boot 对 MongoDB 的默认配置,因此无需编辑 application.properties。
package com.pyc.mongo.dao;import com.pyc.mongo.domain.Person;import org.springframework.data.mongodb.repository.MongoRepository;import org.springframework.data.mongodb.repository.Query;import java.util.List;publicinterfacePersonRepositoryextendsMongoRepository<Person, String>{// query by using function
Person findByName(String name);// query by using @Query,query parameter is constructed by json string@Query("{'age':?0}")
List<Person>withQueryFindByAge(Integer age);}
视图层控制器
package com.pyc.mongo.web;import com.pyc.mongo.dao.PersonRepository;import com.pyc.mongo.domain.Location;import com.pyc.mongo.domain.Person;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Collection;import java.util.LinkedHashSet;import java.util.List;@RestControllerpublicclassDataController{@Autowired
PersonRepository personRepository;// testing the function of save data into database@RequestMapping("/save")public Person save(){
Person p =newPerson("pyc",22);
Collection<Location> locations =newLinkedHashSet<Location>();
Location loc1 =newLocation("陆丰","2017");
Location loc2 =newLocation("肇庆","2018");
Location loc3 =newLocation("肇庆","2019");
Location loc4 =newLocation("肇庆","2020");
Location loc5 =newLocation("肇庆","2021");
locations.add(loc1);
locations.add(loc2);
locations.add(loc3);
locations.add(loc4);
locations.add(loc5);
p.setLocations(locations);return personRepository.save(p);}// testing query by using function@RequestMapping("/q1")public Person q1(String name){return personRepository.findByName(name);}// testing query by using @Query@RequestMapping("/q2")public List<Person>q2(Integer age){return personRepository.withQueryFindByAge(age);}}
入口类无需修改。
5、运行测试
先访问 localhost:8080/save 测试保存,浏览器页面如下:
用可视化管理工具连接数据库,查看数据库中的情况,在 test 数据库中新建了一个 Person 文档,文档的数据如下:
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.0.M2</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.pyc</groupId><artifactId>redis</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>redis</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url></repository></repositories><pluginRepositories><pluginRepository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url></pluginRepository></pluginRepositories></project>
使用 Spring Boot 对 Redis 的默认配置,因此无需编辑 application.properties 文件,需要的话可以自己添加关于日志的配置。